preflower / react-barcode-scanner

A lightweight React scan library based on modern API
https://reactbarcodescanner.vercel.app/
MIT License
18 stars 4 forks source link

Stop Camera after Result #224

Closed as-85 closed 2 months ago

as-85 commented 2 months ago

I'm trying to stop the camera after the barcode has been scanned but I can't think of any way to do it.

I have created a simple page and included the JSX element BarcodeScanner. After the onCapture event has been executed, I want to switch off the camera.

preflower commented 2 months ago

currently u can use useStreamState to get stream and use stream?.getTracks().forEach(track => { track.stop() }) to stop camera; and i will rich useCamera API to support more scenarios in next version;

preflower commented 2 months ago

if u wanna stop camera when use BarcodeScanner, maybe u should remove BarcodeScanner component after onCaptured ?

as-85 commented 2 months ago

Sorry, I can't get it right. I have the following structure and apparently I am not able to program it:

I have an "Order" page. From here I jump to a second page "Scanner" and start the scan.

As soon as a barcode is recognized, I leave the page with the "router". However, the component remains "active".

I have now tried to access the stream via "useStreamState" so that I can end it as suggested, but unfortunately without success.

'use client';

import { useRouter } from 'next/navigation';
import { useRef } from 'react';
import { BarcodeScanner, useCamera, useStreamState } from 'react-barcode-scanner';

export default function ScannerPage() {
    const router = useRouter();

    const printValue = (value) => {
        console.log(value);
        router.back();
    };

    return (
        <BarcodeScanner
            options={{ formats: ['code_128', 'qr_code', 'data_matrix'] }}
            onCapture={printValue}
        />
    );
}

Can you "move" me in the right direction? Thank you.

preflower commented 2 months ago

This is a bug, i have fixed it in v2.0.1, please upgrade to latest version;

as-85 commented 2 months ago

Excellent, it works! Thanks for the update.