react-native(react-native-barcodescanner)扫码第一次进去的时候正常但是之后出现黑屏,在网上的说是新场景要pop掉(可能是我的版本和网上的版本不一样导致的),但是我pop掉了第二次进去还是黑屏,纠结了很久,猜测是调用了摄像头没有释放掉导致的,无奈只能动barcodescanner的源码了,看了该项目中的文件目录如下:
期间修改过react-native-barcodescanner 项目中的这三个文件很多其他的位置的代码(单例模式、去掉ReactBarcodeScannerView.java中setCameraType方法中的第一个if方法等),但是都不行,就不多说了,直接说在我的环境下修改的正确的方法:
修改ReactBarcodeScannerView.java中setCameraType方法,原始代码:
public void setCameraType(String type) {
if (mPrevCameraType.equals(type)) {
return;
}
stopCamera();
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
mCameraId = -1;
for (int cameraId = 0; cameraId < Camera.getNumberOfCameras(); cameraId++) {
Camera.getCameraInfo(cameraId,cameraInfo);
if (type.equals("back") && cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
mCameraId = cameraId;
break;
}
if (type.equals("front") && cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
mCameraId = cameraId;
break;
}
}
startCamera(mCameraId);
if (type.equals("back")) {
setFlash(torchModeIsEnabled());
}
mPrevCameraType = type;
}
public void setCameraType(String type) {
if (mPrevCameraType.equals(type)) {
stopCamera();
return;
}
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
mCameraId = -1;
for (int cameraId = 0; cameraId < Camera.getNumberOfCameras(); cameraId++) {
Camera.getCameraInfo(cameraId,cameraInfo);
if (type.equals("back") && cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
mCameraId = cameraId;
break;
}
if (type.equals("front") && cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
mCameraId = cameraId;
break;
}
}
startCamera(mCameraId);
if (type.equals("back")) {
setFlash(torchModeIsEnabled());
}
mPrevCameraType = type;
}