react-nativereact-native-camera

react native vision camera freezing on taking a picture


I just need to take and store a picture. I am using react-native-vision-camera but when I take a picture, using .takePhoto() method. It freezes.

I am also using "qualityPrioritization: "speed".

I am testing it on an android emulator.

const CameraPage = () => {
  const devices = useCameraDevices()
  const device = devices.back
  const isFocused = useIsFocused()
  const camera = useRef(null)

  const onPressButton = async () => {
    console.log(camera.current)
    console.log(123)
    const photo = await camera.current.takePhoto({
      flash: 'off',
      qualityPrioritization: "speed"
    })

    console.log(photo)

  }
  
  if (device == null) return <View><Text>Loading</Text></View>
  return (
        <View style={{flex: 1}}>
          <Camera
          ref={camera}
          style={StyleSheet.absoluteFill}
          device={device}
          isActive={isFocused}
          photo={true}
          />
           <View style={styles.buttonContainer}>
            <TouchableOpacity 
              style={styles.camButton}
              onPress={onPressButton}
              >
                <Text>Click me</Text>
              </TouchableOpacity>
          </View>
        </View>

  ) 

Solution

  • I met the same problem, and here is the way I solve it.

    Conclusion

    If you just need a demo on an android emulator, you can use takeSnapshot(...) instead.

    Explanation

    Let's go back to the guide of takePhoto() of react-native-vision-camera.

    Right below it is takeSnapshot(), and there has a note that writes:

    if you care about speed, you can use the Camera's takeSnapshot(...) function (Android only) which simply takes a snapshot of the Camera View instead of actually taking a photo through the Camera lens.

    I guess the problem that react-native-vision-camera will freeze maybe because an android emulator doesn't have a real Camera lens.

    So I tried takeSnapshot(...) instead and it worked perfectly.

    That's all, hope this can help. 👍🏻