kybarg / react-barcode-reader

React component for reading barcode an QR codes from devices that are represented as keyboard to the system.
MIT License
86 stars 34 forks source link

Where is the demo ? #17

Closed MGMehdi closed 4 years ago

MGMehdi commented 4 years ago

Your demo link don't work...

pthacker commented 4 years ago

yes , and after importing it isnt working ie camera is not opening

MGMehdi commented 4 years ago

@pthacker I use zxing now

pthacker commented 4 years ago

i am trying to implement Zxing in react PWA, can't wrap my head around it..any suggestions?

MGMehdi commented 4 years ago

I didn't see that the link not working I change it

I don't know what is PWA but here is how I make it work in my reactjs app

import { BrowserBarcodeReader } from '@zxing/library';
import React, { Component } from 'react';
import './App.css';

const codeReader = new BrowserBarcodeReader();

export class App extends Component {

    constructor(props) {
        super(props)

        this.state = {
            result: '',
        }
    }

    handleStart = () => {
        if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
            navigator.mediaDevices.getUserMedia({ video: true })
            codeReader
                .listVideoInputDevices()
                .then(videoInputDevices => {
                    const firstDeviceId = videoInputDevices[0].deviceId;

                    codeReader
                        .decodeOnceFromVideoDevice(videoInputDevices[0].deviceId, 'video')
                        .then(result => this.setState({ result: result.text }))
                        .catch(err => console.error(err));
                })
                .catch(err => console.error(err));
        } else {
            alert("Votre navigateur ne supporte pas l'utilisation de la caméra")
        }
    }

    handleStop = () => {
        codeReader.reset();
    }

    render() {
        return (
            <div className="App">
                <div>
                    <input type="button" value="Start" onClick={this.handleStart} />
                    <input type="button" value="Stop" onClick={this.handleStop} />
                </div>

                <video
                    id="video"
                    width="400"
                    height="300"
                    style={{ "border": "1px solid gray" }}
                ></video>
                <div>
                    <p>{this.state.result}</p>
                </div>
                <div id="result"></div>
            </div>
        )
    }
}

export default App
stamahto commented 4 years ago

Works! Thank you so much for sharing your work. It helps a lot @MGMehdi

pthacker commented 4 years ago

thanks @MGMehdi