第二次使用时,CameraView黑色亮起

时间:2017-05-15 05:25:09

标签: android android-layout android-camera camera2 camera-view

我正在使用CameraView library来显示取景器并从中取出快照。 我有两个按钮,一个用于打开视图,另一个按钮用于关闭视图。

单击按钮将使用true和false参数调用以下方法:

public void showCameraLayout(boolean show) {
    cameraLoading.setVisibility(View.GONE);
    cameraClick.setVisibility(View.VISIBLE);
    ivGallery.setVisibility(View.VISIBLE);
    if (show) {
        showKeyboard(false);
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
            if (mCameraView != null) {
                mCameraView.addCallback(mCallback);
            }
            ViewGroup.LayoutParams params = cameraLayout.getLayoutParams();
            params.height = messagesView.getWidth() / 2;
            cameraLayout.setLayoutParams(params);
            mCameraView.start();
            safeToTakePicture = true;
        } else if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                Manifest.permission.CAMERA)) {
            ConfirmationDialogFragment
                    .newInstance(R.string.camera_permission_confirmation,
                            new String[]{Manifest.permission.CAMERA},
                            REQUEST_CAMERA_PERMISSION,
                            R.string.camera_permission_not_granted)
                    .show(getActivity().getSupportFragmentManager(), FRAGMENT_DIALOG);
        } else {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA},
                    REQUEST_CAMERA_PERMISSION);
        }

    } else {
        ViewGroup.LayoutParams params = cameraLayout.getLayoutParams();
        params.height = 0;
        cameraLayout.setLayoutParams(params);
        if (mCameraView != null) mCameraView.stop();
    }
}

问题是当第一次打开cameraLayout时,我可以看到实时相机。但当我关闭并再次打开它时,它会显示黑屏。 奇怪的是,如果在显示黑屏时拍摄了快照,我会得到一个合适的图像。

2 个答案:

答案 0 :(得分:0)

捕获图像后,您应该停止预览并重新开始。

mCamera.stopPreview();
mCamera.startPreview();
试试吧!

答案 1 :(得分:0)

尝试将其放在onStop()函数中

if(mCameraView.isOpened()) {
   mCameraView.stop();
   mCameraView.destroy();
}