react-native-vision-camera: 🐛 App crash using QR scanner (useCodeScanner)
What’s happening?
The app crashes after scanning a QR code and navigate to another screen (using React Native Navigation) The QR code is successfully scanned but when I want to navigate to another page after (inside onCodeScanned callback) and the app sometimes crashes.
If I unmount the <Camera /> component before navigating to another page it also crash, example.
const [isActive, setIsActive] = React.useState(true);
const [isMount, setIsMount] = React.useState(true);
const device = useCameraDevice('back');
const codeScanner = useCodeScanner({
codeTypes: ['qr', 'ean-13'],
onCodeScanned: codes => {
setIsActive(false);
setIsMount(false); // <-- sometimes crash
props.navigation.navigate('ANOTHER_SCREEN'); // React native navigation
},
});
return isMount ? (
<Camera
style={styles.camera}
device={device}
isActive={isActive}
audio={false}
codeScanner={codeScanner}
/>
) : null;
Is this a mistake or am I doing it wrong?
Reproduceable Code
import React, {useEffect} from 'react';
import {StyleSheet, Text} from 'react-native';
import {
Camera,
useCameraDevice,
useCameraPermission,
useCodeScanner,
} from 'react-native-vision-camera';
export default function VisionScreen(props: any) {
const {hasPermission, requestPermission} = useCameraPermission();
const device = useCameraDevice('back');
useEffect(() => {
if (!hasPermission) {
console.log(requestPermission());
}
}, [hasPermission, requestPermission]);
const codeScanner = useCodeScanner({
codeTypes: ['qr', 'ean-13'],
onCodeScanned: codes => {
props.navigation.navigate('ANOTHER_SCREEN'); // <-- crash
},
});
if (device == null || !hasPermission) {
return <Text>HasPermission False</Text>;
}
return (
<Camera
style={styles.camera}
device={device}
isActive={true}
audio={false}
codeScanner={codeScanner}
/>
);
}
const styles = StyleSheet.create({
camera: {
width: 680,
height: 680,
},
});
Relevant log output
2024-01-02T22:18:53.980Z
693
Layer
FL_V_info: (BufferStateLayer)Name=SurfaceView - com.patrullaje/com.patrullaje.MainActivity@2b3fe50@0(BLAST)#0, Visible=1(Hiddenflag = 0, Alpha = 1.000000, [BufferInfo.mBuffer || mSidebandStream] = 2),Layer.flag=0x102
2024-01-02T22:18:53.982Z
636
libfacedetect
[FdDetectFace,122]: aeStable:1, brightValue:909, backlightPro:0, zoomRatio:0, handle: 0xf443c960, imgaddr: 0xe8de2000, imgw: 640, imgh: 360, imgs: 640
2024-01-02T22:18:53.983Z
636
FDV3
[find, 698]: src:640x360 detect:320 face_num=0 0.37 ms
2024-01-02T22:18:53.984Z
636
android.hardware.camera.provider@2.4-service
87, fp5510e_drv_set_pos: 167
2024-01-02T22:18:53.984Z
636
android.hardware.camera.provider@2.4-service
38, _fp5510e_write_dac_code: 167
2024-01-02T22:18:53.988Z
636
cmr_sensor
922, cmr_sns_thread_proc: evt 524548
2024-01-02T22:18:53.988Z
636
cmr_sensor
1328, cmr_sns_ioctl: cmd = 45, arg = 0xea8a7d08.
2024-01-02T22:18:53.988Z
636
gralloc4
unlockBuffer: id=28100000df7, vaddr=0xe4569000
2024-01-02T22:18:53.989Z
4925
BufferQueueProducer
[ImageReader-1280x720f23m2-4925-1](id:133d00000004,api:4,p:818,c:4925) queueBuffer: BufferQueue has been abandoned
2024-01-02T22:18:53.989Z
818
Surface
queueBuffer: error queuing buffer, -19
2024-01-02T22:18:53.989Z
818
Camera3-OutputStream
returnBufferCheckedLocked: Stream 2: Error queueing buffer to native window: No such device (-19)
2024-01-02T22:18:53.990Z
4925
CameraSession
Adding 1280 x 720 CodeScanner Output in Format #35...
2024-01-02T22:18:53.994Z
4925
CreateCaptureSession
Camera 0: Creating Capture Session #1002... Hardware Level: 0} | Outputs: [android.hardware.camera2.params.OutputConfiguration@fa22230e]
2024-01-02T22:18:53.994Z
4925
CreateCaptureSession
Using new API (>=28)
2024-01-02T22:18:53.995Z
818
Camera3-Device
waitUntilDrainedLocked: Camera 0: Waiting until idle (5000000000ns)
2024-01-02T22:18:53.996Z
636
awb_adpt
1503, awb_sprd_ctrl_calculation_v3_2: check handle success
2024-01-02T22:18:53.997Z
636
ATM
1102, atm_calc: smooth out frame_id 609 u4EVD 29696 smooth u4EVD 29849.599609 bv 909
2024-01-02T22:18:53.997Z
636
isp_blk_cmc10
266, _pm_cmc10_set_param: cmc_result: (2237 15214 16342; 15993 1635 16164; 16320 15718 1753).
2024-01-02T22:18:53.997Z
693
Layer
FL_V_info: (BufferStateLayer)Name=151dc61 com.patrullaje/com.patrullaje.MainActivity, Visible=1(Hiddenflag = 0, Alpha = 1.000000, [BufferInfo.mBuffer || mSidebandStream] = 2),Layer.flag=0x100
2024-01-02T22:18:53.997Z
693
Layer
FL_V_info: (BufferStateLayer)Name=SurfaceView - com.patrullaje/com.patrullaje.MainActivity@2b3fe50@0(BLAST)#0, Visible=1(Hiddenflag = 0, Alpha = 1.000000, [BufferInfo.mBuffer || mSidebandStream] = 2),Layer.flag=0x102
2024-01-02T22:18:53.997Z
636
ATM
1102, atm_calc: smooth out frame_id 610 u4EVD 29696 smooth u4EVD 29798.400391 bv 909
2024-01-02T22:18:53.997Z
636
isp_blk_cmc10
266, _pm_cmc10_set_param: cmc_result: (2237 15214 16342; 15993 1635 16164; 16320 15718 1753).
2024-01-02T22:18:53.998Z
636
Libae[FAE]
1409, face_luma_calc: NO FACE
2024-01-02T22:18:53.998Z
636
aft_sprd_lib
3406, trace_face_by_iou: old_roi (sx ex sy ey) = (0 0 0 0)
2024-01-02T22:18:53.998Z
636
aft_sprd_lib
3421, trace_face_by_iou: face_roi_iou 0.000000
2024-01-02T22:18:53.998Z
636
aft_sprd_lib
3803, aft_proc_log_print: afm move diff : 0,diff_thr :0,diff_cnt : 0,g_diff(x,y,z):(0.196250,0.133750,0.093750),aem move diff : (num : r,g,b,cnt) = (0,0,0,0), scene_change_flag 0
2024-01-02T22:18:54.000Z
636
cmr_oem
9495, camera_get_af_support: af_support 1
2024-01-02T22:18:54.001Z
636
awb_adpt
1788, awb_sprd_ctrl_calculation_v3_2: AWB result : (1517,1024,1361) 5288K , fram_count : 304 , camera_id : 0, wb mode : 0; AWB lib 64x64: (1517,1024,1361) 5288K.
2024-01-02T22:18:54.006Z
636
cmr_sensor
922, cmr_sns_thread_proc: evt 524548
2024-01-02T22:18:54.339Z
5182
DEBUG
#00 pc 00000000000ed984 /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/lib/arm64/libbarhopper_v3.so (BuildId: cf64ac3d5cfd71b04d3bc4112d678e48)
2024-01-02T22:18:54.339Z
5182
DEBUG
#01 pc 00000000000d065c /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/lib/arm64/libbarhopper_v3.so (BuildId: cf64ac3d5cfd71b04d3bc4112d678e48)
2024-01-02T22:18:54.340Z
5182
DEBUG
#02 pc 00000000000ca240 /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/lib/arm64/libbarhopper_v3.so (BuildId: cf64ac3d5cfd71b04d3bc4112d678e48)
2024-01-02T22:18:54.340Z
5182
DEBUG
#03 pc 00000000000c9f74 /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/lib/arm64/libbarhopper_v3.so (BuildId: cf64ac3d5cfd71b04d3bc4112d678e48)
2024-01-02T22:18:54.340Z
5182
DEBUG
#04 pc 00000000000ca4f0 /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/lib/arm64/libbarhopper_v3.so (BuildId: cf64ac3d5cfd71b04d3bc4112d678e48)
2024-01-02T22:18:54.340Z
5182
DEBUG
#05 pc 00000000000c8aa4 /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/lib/arm64/libbarhopper_v3.so (BuildId: cf64ac3d5cfd71b04d3bc4112d678e48)
2024-01-02T22:18:54.340Z
5182
DEBUG
#06 pc 00000000000c8b44 /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/lib/arm64/libbarhopper_v3.so (Java_com_google_android_libraries_barhopper_BarhopperV3_recognizeBufferNative+84) (BuildId: cf64ac3d5cfd71b04d3bc4112d678e48)
2024-01-02T22:18:54.340Z
5182
DEBUG
#07 pc 0000000000461554 /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+148) (BuildId: 12e00d030bcfeb51f978c01791e0cd24)
2024-01-02T22:18:54.340Z
5182
DEBUG
#08 pc 000000000020b02c /apex/com.android.art/lib64/libart.so (nterp_helper+7468) (BuildId: 12e00d030bcfeb51f978c01791e0cd24)
2024-01-02T22:18:54.340Z
5182
DEBUG
#09 pc 000000000024164c [anon:dalvik-classes6.dex extracted in memory from /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/base.apk!classes6.dex]
2024-01-02T22:18:54.340Z
5182
DEBUG
#10 pc 000000000020a254 /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 12e00d030bcfeb51f978c01791e0cd24)
2024-01-02T22:18:54.340Z
5182
DEBUG
#11 pc 00000000002f500c [anon:dalvik-classes6.dex extracted in memory from /data/app/~~9f9_OYifhXHTmjH5WPiaig==/com.patrullaje-bHBfKM2WH00-OorXnqPfzA==/base.apk!classes6.dex]
2024-01-02T22:18:54.340Z
5182
DEBUG
#12 pc 0000000002052568 /memfd:jit-cache (deleted)
Camera Device
{
"sensorOrientation": "landscape-right",
"hardwareLevel": "limited",
"maxZoom": 8,
"minZoom": 1,
"maxExposure": 300,
"supportsLowLightBoost": false,
"neutralZoom": 1,
"physicalDevices": [
"wide-angle-camera"
],
"supportsFocus": true,
"supportsRawCapture": false,
"isMultiCam": false,
"minExposure": -300,
"name": "BACK (0)",
"hasFlash": false,
"hasTorch": false,
"position": "back",
"id": "0"
}
Device
Samung Tab A7
VisionCamera Version
3.6.16
Can you reproduce this issue in the VisionCamera Example app?
Yes, I can reproduce the same issue in the Example app here
Additional information
- I am using Expo
- I have enabled Frame Processors (react-native-worklets-core)
- I have read the Troubleshooting Guide
- I agree to follow this project’s Code of Conduct
- I searched for similar issues in this repository and found none.
About this issue
- Original URL
- State: closed
- Created 6 months ago
- Reactions: 3
- Comments: 28 (7 by maintainers)
@mrousavy looks like this is it!! it did crash after a couple tries using
3.9.1
and we did not reproduce crashes (so far) with4.0.0-beta.10
. Awesome 🎉! thanks for your time and effort.Btw, after more testing we found out that the issue was clearly a race condition when an unmount of the component did interrupt an ongoing scan.
Working on a fix for this issue here: https://github.com/mrousavy/react-native-vision-camera/pull/2494 👀
Still in development, so don’t test yet. Long way to go, this is a really complicated issue (as you can see in the changes/Files of that PR)…
@mrousavy For me it only crashes if the View containing the Camera component is conditionally rendered, i.e. programmatically hidden or set to be visible, if the said container is always visible and never gets hidden it won’t crash.
{isCamVisible ? ( <View> <Camera style={StyleSheet.absoluteFill} device={device} isActive={isCamActive} codeScanner={codeScanner} /> </View> ) : null}
@mrousavy I share you the full adb logcat logs (I exported from Android Studio) and a video of what I was doing. If you need something else let me know.
The code I was using is this (with the version 3.7.0)
https://github.com/mrousavy/react-native-vision-camera/assets/50101944/b4c1ec60-a799-4463-8a52-4b2672947675
samsung-SM-X205-Android-13_2024-01-09_123256.logcat.json
For me the following workaround seems to work out:
I set Camera.isActive to false and wait a few moments before hiding the container
I just released a V4 beta, can you try if that fixes the bugs?
Same issue here.
Same stack trace as https://github.com/mrousavy/react-native-vision-camera/issues/2116
Same problem. Quick dirty workaround
opacity: 0