我正在使用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时,我可以看到实时摄像机。但是当我关闭它并再次打开它时,它会显示黑屏。奇怪的是,如果在显示黑屏时拍摄快照,我会得到正确的图像。
捕获
图像后,您应该停止预览并重新启动。
mCamera.stopPreview();
mCamera.startPreview();
试试吧!
尝试将其放入onStop()
函数中
if(mCameraView.isOpened()) {
mCameraView.stop();
mCameraView.destroy();
}