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;
}
以上紅字的為修改的代碼位置