vision-camera-code-scanner icon indicating copy to clipboard operation
vision-camera-code-scanner copied to clipboard

Event Listener

Open umaraziz0 opened this issue 3 years ago • 3 comments

Hello, this might be a simple question, but I was wondering how I would implement an event listener that runs once a QR code is detected?

I plan to fire an async function that validates the scanned QR code to the database. I tried using useEffect / useMemo with the barcodes variable as a dependency, but it seems that it's reading a state change every time the camera moves? I'm not sure if it'll be good for performance.

Thanks in advance.

umaraziz0 avatar Apr 20 '22 03:04 umaraziz0

Just create your own hook that checks whether the object really changed?

SmartArray avatar Apr 28 '22 03:04 SmartArray

I ended up modifying the useFrameProcessor hook to only update the barcode value only when it detects one.

const frameProcessor = useFrameProcessor((frame) => {
    'worklet';

    const detectedBarcodes = scanBarcodes(frame, [BarcodeFormat.QR_CODE], {checkInverted: true});

    if (detectedBarcodes.length) runOnJS(setBarcodes)(detectedBarcodes);
  }, []);

not sure if this is the proper way to go, but it works for now. Anyone who might come up with a better solution, please feel free to comment.

umaraziz0 avatar Apr 28 '22 16:04 umaraziz0