0

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

两个按钮单击都将调用以下带有 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 时,我可以看到实时摄像头。但是当我关闭它并再次打开它时,它显示黑屏。奇怪的是,如果在显示黑屏时拍摄快照,我会得到正确的图像。

4

2 回答 2

0

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

if(mCameraView.isOpened()) {
   mCameraView.stop();
   mCameraView.destroy();
}
于 2020-08-28T07:27:22.627 回答
0

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

mCamera.stopPreview();
mCamera.startPreview();

试试看!

于 2017-05-15T05:44:38.500 回答