Skip to content

how correct stop video in react hook #19

Open
@PaloJeDobryClovek

Description

@PaloJeDobryClovek

Hello,
I have in react component, when is render I want run scanner, if not stop scanning barcode.

import React, {useRef, useEffect} from 'react';
import {BrowserMultiFormatReader, BarcodeFormat} from '@zxing/browser';
import DecodeHintType from "@zxing/library/cjs/core/DecodeHintType";
import {makeStyles} from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
    video: {
        width: '100%',
        height: '100%',
        maxHeight: '50vh',
        objectFit: 'cover',
    },
}));

export default function ScannerZxing({callback}) {
    const classes = useStyles();
    const video = useRef();
    useEffect(() => {
            const hints = new Map();
            const formats = [BarcodeFormat.QR_CODE, BarcodeFormat.CODE_128, BarcodeFormat.ITF];
            hints.set(DecodeHintType.POSSIBLE_FORMATS, formats);
            const codeReader = new BrowserMultiFormatReader(hints, {
                delayBetweenScanSuccess: 2000,
                delayBetweenScanAttempts: 600,
            });
            var controls = null;
            controls = codeReader
                .decodeFromVideoDevice(undefined, video.current, function (result, error) {
                    if (typeof result !== "undefined") {
                        callback(result); // redirect on different page and not render this
                    }
                })
            console.log(controls);

            return function cleanup() {
                console.log('clean');
                //controls.stop();
            };
    });

    return (
        <video
            className={classes.video}
            ref={video}
        ></video>
    );
}

but it now works correctly and after scan and render different page scanner still work, could you please write example in react?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions