mrousavy / react-native-vision-camera

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

💭 Getting incorrect coordinates and bounds when using CodeScanner when scanning barcodes #3011

Open shehanGuruge opened 2 months ago

shehanGuruge commented 2 months ago

Question

I am using react-native-vision-camera v4.1.0, and I implemented the CodeScanner as per the docs. However every time I scan a barcode I am getting incorrect width which is in range of 3 - 10 in IOS. Hence it's impossible to map those values to the screen width and height and limit the scan area

What I tried

This is what I have done so far,

My Code Scanner looks like this

const codeScanner: CodeScanner = {
    codeTypes: ['ean-13'],
    onCodeScanned: (codes, frame) => {
      setScanFrame(frame);
      setCodeScannerHighlights(
        codes.map((code) => ({
          height: code.frame?.width ?? 0,
          width: code.frame?.height ?? 0,
          x: code.frame?.y ?? 0,
          y: code.frame?.x ?? 0,
        }))
      );
    },
  };

This is how I have set the codeScanner

   const device = useCameraDevice('back');
   <Camera
        style={StyleSheet.absoluteFill}
        device={device}
        isActive={true}
        codeScanner={codeScanner}
        onLayout={onLayout}
      />

I am also using onLayout to map the coordinates to the width and height of the camera view and limit the scan area like below

  const onLayout = (evt: LayoutChangeEvent) => {
    if (evt.nativeEvent.layout) {
      setLayout(evt.nativeEvent.layout);
    }
  };

This is the response I get

Scanned [{"value":"1234567890128","corners":[{"x":929.5000279190813,"y":801.097824131347},{"x":932.5000393631731,"y":801.0978240575837},{"y":397.90094443226377,"x":932.5000080310912},{"x":929.4999965869994,"y":397.90094450602703}],"frame":{"width":3.000011444091797,"x":929.5000076293945,"height":403.1969118118286,"y":397.9009437561035},"type":"ean-13"}] codes!

As you can see the frame width is always within 3 and 10 irrespective of how closer or away the phone/camera is from the code. Any way I could fix this? Any suggestions how I could limit the scan area using this library?

VisionCamera Version

4.0.1

Additional information

maintenance-hans[bot] commented 2 months ago

Guten Tag, Hans here.

[!NOTE] New features, bugfixes, updates and other improvements are all handled mostly by @mrousavy in his free time. To support @mrousavy, please consider 💖 sponsoring him on GitHub 💖. Sponsored issues will be prioritized.

JshGrn commented 2 months ago

Any luck with this? I am getting unexpected results from the bounds, getting negative values... Do you know what order the corners are by any chance? The docs are very unhelpful on this.