mrousavy / react-native-vision-camera

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

❓ How to create frame for scanning QR code? #2342

Closed nobodyzzz01 closed 10 months ago

nobodyzzz01 commented 10 months ago

Question

Screenshot 2024-01-03 at 09 50 23 React native Version: 0.72.7

What I tried

I tried using frame processor but it didn't work And I tried to calculate the coordinates of the frame to receive the correct QR code but failed!!!

const codeScanner = useCodeScanner({ codeTypes: ['qr', 'ean-13'], onCodeScanned: (codes: Code[]) => { const pxDiff = Math.max(PixelRatio.get() - 1, 1);

  const qrCodeX = codes[0]?.frame?.x ?? 0;
  const qrCodeY = codes[0]?.frame?.y ?? 0;

  const qrCodeWidth = codes[0]?.frame?.width ?? 0;
  const qrCodeHeight = codes[0]?.frame?.height ?? 0;

  const isQRCodeInsideOverlay =
    qrCodeX >= PosX &&
    qrCodeY >= PosY &&
    qrCodeX + qrCodeWidth <= widthHole + PosX &&
    qrCodeY + qrCodeHeight <= heightHole + PosY;
  setIsFrame(isQRCodeInsideOverlay);
  if (isScannerActive) {
    scannerQRRef.current = codes.map(itemQR => itemQR.value).toString();
    console.log(`Scanned ${codes.length} codes!\n Data Scanned: ${codes}`);
    console.log(`Scanned ${scannerQRRef.current}`);
    setScannerActive(false);
    if (scannerQRRef.current) {
      makeQRScannerRequest(scannerQRRef.current);
    }
    scannerQRRef.current = null;
  } else {
    setScannerActive(false);
    console.log('Scanner can only be started within the frame area.');
    if (isScannerActive && !isQRCodeInsideOverlay) {
      Alert.alert(
        'ERROR',
        'Scanner can only be started within the frame area.',
      );
    }
  }
},

});

<ReanimatedCamera style={[StyleSheet.absoluteFill]} device={device} onError={onError} isActive={isActive} format={format} fps={fps} zoom={0} codeScanner={codeScanner} exposure={0} />

VisionCamera Version

3.6.8

Additional information

xulihang commented 10 months ago

I use react-native-svg to draw the overlay and pass a scan region argument to the frame processor to crop the image before processing. You can find an example here: https://github.com/tony-xlh/react-native-mrz-scanner/blob/main/src/screens/Scanner.tsx

nobodyzzz01 commented 10 months ago

I tried using regionOfInterest with coordinates taken from frame but it seems regionOfInterest doesn't work

abdelhakimrafik commented 10 months ago

@xulihang @mrousavy After investigating the Android codebase, I couldn't find any part where you handle the regionOfInterest for the CodeScanner

mrousavy commented 10 months ago

I documented this wrong - regionOfInterest in the built-in CodeScanner only works on iOS. On Android, this is not available. https://github.com/mrousavy/react-native-vision-camera/commit/9ecc09cfe46033aca410891e51058e82ee081b1f

If you want this behaviour on Android, use Frame Processors instead with a custom Frame Processor Plugin for QR code scanning, then crop the Frame before using some Frame Processor Plugin to resize/crop it.

mrousavy commented 9 months ago

Btw., if you use Frame Processors and need to resize the Frame, I just now released vision-camera-resize-plugin - a plugin to resize Frames to any size and convert them to any RGB layout (RGB, BGR, ARGB, BGRA, ...)! 🥳

thuydao commented 3 months ago

Btw., if you use Frame Processors and need to resize the Frame, I just now released vision-camera-resize-plugin - a plugin to resize Frames to any size and convert them to any RGB layout (RGB, BGR, ARGB, BGRA, ...)! 🥳

Which is type of resized? is it frame?