使用自定义相机拍摄照片后图像失真

Image Is Being Distorted After Capturing Pictures From Custom Camera

我正在开发一个应用程序,我需要使用自定义相机,已成功实现但拍照后出现问题。拍完图我在另一个Activity那里显示图片,显示失真下面是我的代码,

相机Activity:

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {

private static final String TAG = "CameraActivity";
Context context;

Camera camera;

SurfaceView cameraSurfaceView;
SurfaceHolder cameraHolder;

ImageView capture;
Camera.Size bestSize;


Camera.PictureCallback jpegCallBack;
final int CAMERA_REQUEST_CODE = 122;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    context = this;

    capture = (ImageView) findViewById(R.id.iv_capture);
    cameraSurfaceView = (SurfaceView) findViewById(R.id.camera_surface_view);
    cameraHolder = cameraSurfaceView.getHolder();

    if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(CameraActivity.this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);

    } else {
        cameraHolder.addCallback(this);
        cameraHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    jpegCallBack = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            Intent intent = new Intent(context, ShowImageActivity.class);
            // sending image(data) in form of bytes to next activity
            intent.putExtra("capture", data);
            startActivity(intent);

        }
    };

    capture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            captureImage();
        }
    });

}

private void captureImage() {
    camera.takePicture(null, null, jpegCallBack);
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();

    Camera.Parameters parameters;
    parameters = camera.getParameters();

    camera.setDisplayOrientation(90);
    parameters.setPreviewFrameRate(30);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

    Camera.Size bestSize = null;
    List<Camera.Size> sizeList = camera.getParameters().getSupportedPreviewSizes();
    bestSize = sizeList.get(0);

    for (int i = 0; i < sizeList.size(); i++) {
        if ((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)) {
            bestSize = sizeList.get(i);
        }
    }
    parameters.setPreviewSize(bestSize.width, bestSize.height);



    camera.setParameters(parameters);

    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        e.printStackTrace();
    }

    camera.startPreview();
}



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {

        case CAMERA_REQUEST_CODE:

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                cameraHolder.addCallback(this);
                cameraHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            } else {
                Toast.makeText(context, "Please Give Camera Permission", Toast.LENGTH_SHORT).show();
            }

            break;

    }
}
}

ShowImageActivity,我在哪里显示捕获的图片:

public class ShowImageActivity extends AppCompatActivity {

private static final String TAG = "ShowImageActivity";
Context context;

ImageView capturedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_image);

    context = this;

    capturedImage = (ImageView) findViewById(R.id.iv_captured_image);
    Bundle extras = getIntent().getExtras();
    assert extras != null;
    byte[] bytes = extras.getByteArray("capture");

    if (bytes != null){

        // to decode the image from bytes

        Bitmap decodedBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);

        Bitmap rotateBitmap = rotate(decodedBitmap);

        capturedImage.setImageBitmap(rotateBitmap);

    }
}

private Bitmap rotate(Bitmap decodedBitmap) {

    // to rotate Image
    int width = decodedBitmap.getWidth();
    int height = decodedBitmap.getHeight();

    Matrix matrix = new Matrix();

    matrix.setRotate(90);

    return Bitmap.createBitmap(decodedBitmap,0,0,width,height,matrix,true);


}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(context, CameraActivity.class);
    startActivity(intent);
    finish();
}
}

activity_camera.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CameraActivity">

<SurfaceView
    android:id="@+id/camera_surface_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ImageView
    android:layout_alignParentBottom="true"
    android:id="@+id/iv_capture"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:src="@drawable/capture"
    android:layout_marginBottom="20dp"/>

提前致谢。

Image i want Just without click button 这是 The distorted image i am getting

通过从 parameters.setPreviewSize(bestSize.width, bestSize.height); 更改为 parameters.setPictureSize(bestSize.width, bestSize.height);

解决了错误