mgcrea / vision-camera-barcode-scanner

High performance barcode scanner for React Native using VisionCamera
MIT License
84 stars 11 forks source link

Crash on Android #4

Closed spsaucier closed 10 months ago

spsaucier commented 11 months ago

On my Android device (Moto G Stylus 2022), when I include this scanner in the Camera, it opens for a second or two, but then always crashes with the following message:

FATAL EXCEPTION: mrousavy/VisionCamera.video

com.facebook.jni.CppException: Compiling JS failed: 1:1:invalid empty parentheses '( )' Buffer size 3 starts with: 280a29

FATAL EXCEPTION: mrousavy/VisionCamera.video
Process: com.vehotechnologies.DriverDevelop, PID: 22428
com.facebook.jni.CppException: Compiling JS failed: 1:1:invalid empty parentheses '( )' Buffer size 3 starts with: 280a29
    at com.mrousavy.camera.frameprocessor.FrameProcessor.call(Native Method)
    at com.mrousavy.camera.core.VideoPipeline._init_$lambda-0(VideoPipeline.kt:107)
    at com.mrousavy.camera.core.VideoPipeline.$r8$lambda$Jc5WVk283o2kPLyelK6VJSU9jAE(Unknown Source:0)
    at com.mrousavy.camera.core.VideoPipeline$$ExternalSyntheticLambda0.onImageAvailable(Unknown Source:2)
    at android.media.ImageReader$1.run(ImageReader.java:837)
    at android.os.Handler.handleCallback(Handler.java:980)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loopOnce(Looper.java:238)
    at android.os.Looper.loop(Looper.java:357)
    at android.os.HandlerThread.run(HandlerThread.java:85)

Calling code:

  const device = useCameraDevice('back', {
    physicalDevices: ['wide-angle-camera'],
  });
  const isFocused = useIsFocused();
  const isActive = isFocused;

  const {
    props: cameraProps,
  } = useBarcodeScanner({
    fps: 5,
    onBarcodeScanned: barcodes => {
      'worklet';
      console.log(
        `Scanned ${barcodes.length} codes with values=${JSON.stringify(
          barcodes.map(({ value }) => value)
        )} !`
      );
    },
  });

...

            <Camera
              style={styles.preview}
              device={device}
              isActive={isActive}
              {...cameraProps}
              resizeMode="contain"
              enableFpsGraph
              orientation="landscape-right"
              pixelFormat="native"
              enableZoomGesture
              enableHighQualityPhotos={false}
              torch={useTorch ? 'on' : 'off'}
            />

With this same setup, Camera is working when I comment out the {...cameraProps} line in <Camera />.


Aside: I wasn't able to get the example app working to reproduce this, unfortunately -- I kept getting this error in Metro:

 BUNDLE  ./index.js

error: Error: Unable to resolve module @mgcrea/vision-camera-code-scanner from .../vision-camera-barcode-scanner/example/src/App.tsx: @mgcrea/vision-camera-code-scanner could not be found within the project or in these directories:
  node_modules
  ../../node_modules
  3 |   // onBarcodeDetected,
  4 |   useBarcodeScanner,
> 5 | } from '@mgcrea/vision-camera-code-scanner';
spsaucier commented 11 months ago

I saw the following response on another thread and will try it shortly.


          > Yes, that one works nicely. I had to upgrade react native to 0.72.5 though.

Well I just tried it on Android and it crashed the whole app after ~1 sec of code scanning. Guess I'll be staying with v2 a little longer.

Found what's wrong:

  1. If you need reanimated and vision-camera frame processor to play nicely with each other, you'll need latest version of vision-camera, react-native-worklets-core, and react-native-reanimated
  2. babel.config.js should look like this
    module.exports = {
    presets: ['module:metro-react-native-babel-preset'],
    plugins: [['react-native-worklets-core/plugin'], ['react-native-reanimated/plugin']],
    };

Originally posted by @zzz08900 in https://github.com/mrousavy/react-native-vision-camera/issues/1985#issuecomment-1759653565

celik75 commented 11 months ago

i have same problem also on ios, it works in debug mode but not in release mode. i get the same error when the camera starts

spsaucier commented 11 months ago

Edit: Updating to Reanimated v3 (latest) did fix the crash!

Now though it keeps giving this error repeatedly (no crash though): ERROR Frame Processor threw an error: Cannot read property 'map' of undefined

zzz08900 commented 11 months ago

And later I discovered there's still crash in release builds for Android if you use reanimated(whatever version) with vision camera v3 and frame processor. Debug builds works fine though.

The only valid solution as of now is to remove reanimated, which is not always possible.

It looks like react-native-worklets-core is still messed up by reanimated in some way. Needs more digging.

FYI you can make a patch-package for vision camera v2.15.x to fix the iOS17/iPhone 15 crash, and stick to old old vision-camera-code-scanner until v3 or react-native-worklets-core gains more compatibility.

I believe those two commits are what you need to make a patch for v2.15.x https://github.com/mrousavy/react-native-vision-camera/commit/503114b9d1c61d60fa3c69b3bd4cb084758fbf5b https://github.com/mrousavy/react-native-vision-camera/commit/2ae4439c844e62131f76d35ffeca76f5a1c7237f

Yeah, I already did it and I'm just playing with v3 to try things out.

celik75 commented 11 months ago

this was the fix for me: https://github.com/mrousavy/react-native-vision-camera/issues/1776#issuecomment-1757204827

mgcrea commented 10 months ago

I have update the lib to support the latest version of react-native-vision-camera https://github.com/mgcrea/vision-camera-barcode-scanner/releases/tag/0.6.1, please reopen if you encounter new issues.