mrousavy / react-native-vision-camera

📸 A powerful, high-performance React Native Camera library.
https://react-native-vision-camera.com
MIT License
7.57k stars 1.1k forks source link

Disable Frame processor on route change💭 #2949

Closed kssgarcia closed 5 months ago

kssgarcia commented 5 months ago

Question

I am making an app that use expo-router, what I want to know if there is a way to disable the useFrameProcessor because when I go to another route the processor still is running. By the way, I am using active property when I change the route. This is my Camera component:

<Camera
  style={StyleSheet.absoluteFillObject}
  ref={cameraRef}
  device={device}
  isActive={isActive}
  photo={true}
  format={format}
  fps={fps}
  orientation="portrait"
  photoQualityBalance="speed"
  // enableFpsGraph={true}
  frameProcessor={frameProcessor}
/>

What I tried

No response

VisionCamera Version

4.0.5

Additional information

gwendall commented 5 months ago

You could only run the camera if the screen is active. If the camera doesn't run, the frame processor should not either. So something like:

import { useNavigation } from 'expo-router';
...
const navigation = useNavigation();
const isScreenFocused = navigation.isFocused();
...
<Camera
   ...
   isActive={isScreenFocused}
/>
kssgarcia commented 5 months ago

I understood you that I should set the active property false when the screen is not focus, but I am already doing this,

<Camera
  style={StyleSheet.absoluteFillObject}
  ref={cameraRef}
  device={device}
  isActive={isActive}
  photo={true}
  format={format}
  fps={fps}
  orientation="portrait"
  photoQualityBalance="speed"
  // enableFpsGraph={true}
  frameProcessor={frameProcessor}
/>

the isActive={isActive} set this to false when the screen is not focus or when the app is in second plane. This seems to work fine, but when I use frameProcessor={frameProcessor} this is still running when the screen is not focus.

gwendall commented 5 months ago

Strange, maybe it's a regression on a new version? In this case you could pass isActive as a dependency of the frame processor and not run what's inside if it is false, like:

useFrameProcessor((frame) => {
  if (!isActive) return;
  ...
}, [isActive]);
kssgarcia commented 5 months ago

@gwendall you were right, ty. It seems that I was setting the isActive in a wrong way. Ty man