异常配置曲面创建曲面时出错

时间:2019-03-11 13:42:43

标签: android android-layout android-camera surfaceview

我正在使用表面视图来实现基本的相机API并单击图片,但是我在surfaceCreated方法上遇到了Exception configuring surface java.lang.RuntimeException: setParameters failed错误, 下面是我的CameraActivity.java

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {

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

static Camera camera = null;

private ImageView switchCamera, flashLight, capture;
SurfaceView cameraSurfaceView;
SurfaceHolder cameraHolder;
Camera.Size bestSize;
Camera.Parameters parameters;
int cameraID = 0;

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);
    flashLight = (ImageView) findViewById(R.id.iv_flash);
    switchCamera = (ImageView) findViewById(R.id.iv_switch_camera);

    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);
    }
    camera = Camera.open(cameraID);
    parameters = camera.getParameters();

}


@Override
protected void onResume() {
    super.onResume();

    camera = Camera.open(cameraID);

    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);
            intent.putExtra("CAMERA_ID", cameraID);
            startActivity(intent);
            camera.stopPreview();
            camera.release();

        }
    };

    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.setDisplayOrientation(90);
    parameters.setPreviewFrameRate(30);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

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

    bestSize = sizeList.get(0);

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

    parameters.setPictureSize(bestSize.width, bestSize.height);
    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) {

                camera = Camera.open();

                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 = 1; i < sizeList.size(); i++) {
                    if ((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)) {
                        bestSize = sizeList.get(i);
                    }
                }

                parameters.setPictureSize(bestSize.width, bestSize.height);
                parameters.setPreviewSize(bestSize.width, bestSize.height);

                camera.setParameters(parameters);

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

                camera.startPreview();

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

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

                finish();
            }

            break;
    }
}

我以前没有遇到过这个问题,但是现在我遇到了这个错误 我在所有地方都搜索了答案,但解决方案表明问题出在线程上,但我不想使用任何线程。 预先感谢您的帮助。

0 个答案:

没有答案