react-native-vision-camera: ๐ Unmounting the Camera component does not turn off the torch
What were you trying to do?
Hi,
I am currently using vision-camera in a project to read QR Codes (using โvision-camera-code-scannerโ for the FrameProcessor). The project also uses react-navigation.
The user can open a page dedicated to QR Code scanning, which only has the active Camera and a button to toggle the torch. If the torch is kept on and the user goes back (popping the page out of the navigation current stack), the torch stays on.
The only way to turn it off is then to reopen the page, or minimize the whole application.
I did not find any imperative API to use with a ref either.
Reproduceable Code
import React from 'react';
import {
TouchableOpacity,
} from 'react-native';
import {Camera, useCameraDevices} from 'react-native-vision-camera';
export default function QrPageExemple(props) {
const [cameraPermission, setCameraPermission] = React.useState("");
const [enableFlashlight, setEnableFlashLight] = React.useState(false);
const devices = useCameraDevices();
const device = devices.back;
React.useEffect(() => {
async function requestPermission() {
const status = await Camera.requestCameraPermission();
setCameraPermission(status);
}
requestPermission();
// Cleaning, doesn't work
return () => {
setEnableFlashLight(false);
};
}, []);
return (
<>
<Camera device={device} torch={enableFlashlight ? "on" : "off"} />
<TouchableOpacity
onPress={() => {
setEnableFlashLight(!enableFlashlight);
}}
></TouchableOpacity>
</>
);
}
What happened instead?
The torch stayed on after the Camera component was unmounted. Using a cleaning useEffect() hook to set the torch prop to false has no effect.
Relevant log output
No response
Device
Samsung Galaxy A51 (Android 12), untested on iOS
VisionCamera Version
2.13.2
Additional information
- I am using Expo
- 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 2 years ago
- Comments: 39 (15 by maintainers)
๐๐Hi Guys, I have workaround that android vision-camera make allow torch control by ref๐๐
Allow controlling torch by ref in android
Usage
you can get react-native-vision-camera+2.15.1.patch file at the link
how to use .patch file patch-package
Working On
After finish these jobs i will make a PR, until now you can fix your bug with this patch!! See ya ๐
Nice work! Seems good to me.
I will work on a fix soon
@markoprodanovic i will notice you by a comment that you can get rid of patch files ๐
I have found the issue: updateLifecycleState is called from onDetachedFromWindow and it assumes the hostLifecycleState is the indicator to update the Camera. However this is only bound to the actual current running host session. Removing the view from the tree will not cause it to be marked as โDESTROYEDโ. If I however force the lifecycle state to be set to DESTROYED from onDetachFromWindow the Camera will be released appropriately and the torch be turned off. I am not sure how this works for anyone correctly right now, but to me it looks like a general issue. @mrousavy I would be happy to open a PR if you agree with me
Iโm experiencing the same problem on v3 on iOS
@whtjs it was a mistake on my end and a misunderstanding of how to use patch-package. For context, I was overwriting the patch file by running my own
yarn patch-package react-native-vision-camera
. I was also working withv0.5.2
instead ofv0.5.1
. Once I sorted out those things it worked great! Thank you ๐Would be great to get this in an official release
Thank so much, it worked for me, but first I had to patch the patch-package ๐ because I had a buffer bug https://github.com/ds300/patch-package/issues/166
I used this patch https://gist.github.com/brussee/e382ed12ca007a88170289e54b526063 but with
maxBuffer: 1024 * 1024 * 1000,
Thank you always for your dedication. Hope i can help you!
It was helped me=) Look like (in CameraView.kt):