QR Code Reader / Scanner in React (Html5-QRCode.js)

Поделиться
HTML-код
  • Опубликовано: 2 ноя 2024

Комментарии • 49

  • @EvenceMohauLanga-m1d
    @EvenceMohauLanga-m1d Год назад +13

    import React, { useEffect, useState } from 'react';
    import { Html5QrcodeScanner } from 'html5-qrcode';
    import './App.css';
    function App() {
    const [scanResult, setScanResult] = useState(null);
    const [manualSerialNumber, setManualSerialNumber] = useState('');
    useEffect(() => {
    const scanner = new Html5QrcodeScanner('reader', {
    qrbox: {
    width: 250,
    height: 250,
    },
    fps: 5,
    });
    let isScanning = true;
    scanner.render(success, error);
    function success(result) {
    if (isScanning) {
    scanner.clear();
    setScanResult(result);
    isScanning = false; // Set isScanning to false to stop further scanning
    }
    }
    function error(err) {
    console.warn(err);
    }
    }, []);
    function handleManualSerialNumberChange(event) {
    setManualSerialNumber(event.target.value);
    }
    return (

    QR Scanning Code
    {scanResult ? (

    Success: {scanResult}
    Serial Number: {scanResult.slice(-16)}

    ) : (


    Or enter the serial number manually:


    {manualSerialNumber && (
    Serial Number: {manualSerialNumber.slice(-16)}
    )}


    )}

    );
    }
    export default App;
    Hi...this code doesn't scan using a live camera but only when i upload the picture.....what might be the problem?

    • @Elizabeth-xl9iv
      @Elizabeth-xl9iv Год назад

      import React, { useEffect, useState } from 'react';
      import { Html5QrcodeScanner } from 'html5-qrcode';
      function App() {
      const [scanResult, setScanResult] = useState(null);
      const [manualSerialNumber, setManualSerialNumber] = useState('');
      useEffect(() => {
      let scanner;
      async function initializeScanner() {
      try {
      scanner = new Html5QrcodeScanner('reader', {
      qrbox: {
      width: 250,
      height: 250,
      },
      fps: 5,
      });
      scanner.render(success, error);
      } catch (error) {
      console.error('Error initializing scanner:', error);
      }
      }
      function success(result) {
      setScanResult(result);
      }
      function error(err) {
      console.warn(err);
      }
      initializeScanner();
      return () => {
      if (scanner) {
      scanner.clear();
      }
      };
      }, []);
      function handleManualSerialNumberChange(event) {
      setManualSerialNumber(event.target.value);
      }
      return (
      QR Scanning Code
      {scanResult ? (
      Success: {scanResult}
      Serial Number: {scanResult.slice(-16)}
      ) : (

      Or enter the serial number manually:

      {manualSerialNumber && (
      Serial Number: {manualSerialNumber.slice(-16)}
      )}

      )}
      );
      }
      export default App;
      u can use this code .

  • @ila6545
    @ila6545 2 месяца назад

    Good tutorial.I am loading this in webview for android app.But the cam is not opened for QR code.It triggers error.Also the qr code image path is not loaded.
    Any idea?

  • @teclyt_solutions3839
    @teclyt_solutions3839 Год назад +1

    very good tutorial helped me alot, thankyou

  • @shekherkaushik.18
    @shekherkaushik.18 6 месяцев назад +1

    thankyou so much it is very helpful 😇

    • @kovereduard637
      @kovereduard637 6 месяцев назад +1

      does it work for you?

    • @shekherkaushik.18
      @shekherkaushik.18 6 месяцев назад +1

      @@kovereduard637 yes, i used it in production and currently its working good 👍

    • @kovereduard637
      @kovereduard637 6 месяцев назад

      @@shekherkaushik.18 did you adjust the code or the one from the video is working? could you please provide me the code that worked for you?

    • @kovereduard637
      @kovereduard637 6 месяцев назад

      @@shekherkaushik.18 also did the camera part work?

    • @shekherkaushik.18
      @shekherkaushik.18 6 месяцев назад

      @@kovereduard637 yes actually applied some changes but i cant provide the modified code cause i am using it in my company's project....... sorry for that

  • @juryhc
    @juryhc Год назад +3

    Very good video tutorial, I have a question:
    is it possible to modify the interface and labels of the qr reader?

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      It seems there isn't a simple solution for doing that at the moment beyond styling the UI that is injected with CSS.
      However, if you are willing to write a bit more code, for now you could wait for the particular element you want to exist (e.g. change a label for) using the MutationObserver and then modify it. It is a bit of a workaround in terms of intercepting elements rather than modifying them within the library but it seems like the best solution for now.
      In case you are unfamiliar with it, here s a basic introduction: ruclips.net/video/onzJwmACYAc/видео.html

  • @parkj4442
    @parkj4442 Год назад +1

    hello. Thanks for the good solution.
    Is it possible to communicate with the server by binding data to QR code?
    Is it possible to scan the QR code and display a modal window rather than a results page?
    I've been thinking about solving this problem for several days.

  • @kakudaisukedev4890
    @kakudaisukedev4890 Год назад +1

    Thank you. Very useful !

  • @nanaana9757
    @nanaana9757 Месяц назад

    Hi, is it possible to use an external camera? Like cam scanner, web cam or something else

    • @leandroalonso9895
      @leandroalonso9895 Месяц назад

      Use Iriun WebCam. Installed on both the phone and the PC.

  • @MiyaIzumi-q5n
    @MiyaIzumi-q5n Год назад +2

    Is there any way to upload image as an identification card with a qr-code in it and scan it ? I tried it right now it doesn't recognize the qr-code in it

    • @OpenJavaScript
      @OpenJavaScript  Год назад

      Technically, this should be possible as long as the QR code is visible next to whatever else is on the ID card (as long as it's not another code).
      A possible option for increasing the chances of it scanning successfully is to increase the 'fps' value (frames per second) for detecting a code in the options object. This will mean it attempts to read the code more often but can start affecting perforance is you set it too high (a value up to 1000 is possible according to the docs).
      If there is some other code on the card and you want to limit it, you can add this property to the options object to limit detection to QR codes only:
      formatsToSupport: [ Html5QrcodeSupportedFormats.QR_CODE ]

  • @walidsabbidine7335
    @walidsabbidine7335 6 месяцев назад

    it works on mobile devices?

  • @yashpataliya7695
    @yashpataliya7695 6 месяцев назад

    thanks for the code
    but i want when i click on the turnon button then only camera start not before

  • @TanaTorn-n8j
    @TanaTorn-n8j 9 месяцев назад

    How to fix :Uncaught TypeError: Cannot read properties of null (reading 'videoWidth')

  • @jobygeorge3395
    @jobygeorge3395 6 месяцев назад

    Thanks Man.

  • @honfey
    @honfey Год назад

    Hi, can it scan barcode as well?

  • @hamadkhan3248
    @hamadkhan3248 3 месяца назад

    thanks

  • @cosmasdaniella2079
    @cosmasdaniella2079 10 месяцев назад

    This is a cool tutorial but for some reason I keep gettign this error "Uncaught HTML Element with id=reader not found".

    • @revanthkumarsake7035
      @revanthkumarsake7035 8 месяцев назад

      I am also faced same issue, I make sapareated QR code file , its resolved

  • @fahiyangtech
    @fahiyangtech Год назад

    Can with qrcode and barcode 128?

  • @lifeofstephen-yt
    @lifeofstephen-yt Год назад +3

    Why do you sound like you are being kidnapped lol

  • @raksmeyincomputing
    @raksmeyincomputing 8 месяцев назад

    Cool

  • @muradjadoon619
    @muradjadoon619 Год назад +5

    share your github repo link

    • @jhonuribe171
      @jhonuribe171 Год назад +1

      When I reload the page, the camera appears twice and I want to change the style

    • @onedimension
      @onedimension Год назад

      how u fixed this?@@jhonuribe171

    • @learning_trespasser
      @learning_trespasser Год назад

      @@jhonuribe171 did you solve it, I am also facing same problem

  • @sriharibolisetti3095
    @sriharibolisetti3095 7 месяцев назад

    is this working
    for react 18.17.0

    • @ochnico7658
      @ochnico7658 7 месяцев назад

      npm i html5-qrcode --legacy-peer-deps

    • @sriharibolisetti3095
      @sriharibolisetti3095 7 месяцев назад

      @@ochnico7658 and also 18.2.0 right?

  • @sriharibolisetti3095
    @sriharibolisetti3095 7 месяцев назад +1

    HTML Element with id=reader not found
    at handleError (localhost:4200/static/js/bundle.js:334531:58)
    at localhost:4200/static/js/bundle.js:334550:7
    at Object.invokeGuardedCallbackDev (localhost:4200/static/js/bundle.js:293939:20)
    at invokeGuardedCallback (localhost:4200/static/js/bundle.js:293996:35)
    at reportUncaughtErrorInDEV (localhost:4200/static/js/bundle.js:309929:9)
    at captureCommitPhaseError (localhost:4200/static/js/bundle.js:313591:9)
    at commitPassiveMountEffects_complete (localhost:4200/static/js/bundle.js:311658:13)
    at commitPassiveMountEffects_begin (localhost:4200/static/js/bundle.js:311646:11)
    at commitPassiveMountEffects (localhost:4200/static/js/bundle.js:311636:7)
    at flushPassiveEffectsImpl (localhost:4200/static/js/bundle.js:313519:7)
    i'm getting this error

    • @gorbachew5598
      @gorbachew5598 7 месяцев назад

      You need create div element with id = reader

  • @user-ev9ob2bv4z
    @user-ev9ob2bv4z 4 месяца назад

    console.log(scanResult) in useEffect no value

  • @Kratosgod217
    @Kratosgod217 Год назад +1

    Hello! am i the only one who gets 23 warnings like this one:
    WARNING in ./node_modules/html5-qrcode/esm/zxing-html5-qrcode-decoder.js
    Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
    Failed to parse source map from 'C:\Users\Juani\Desktop\StockScan\client
    ode_modules\src\zxing-html5-qrcode-decoder.ts' file: Error: ENOENT: no such file or directory, open 'C:\Users\Juani\Desktop\StockScan\client
    ode_modules\src\zxing-html5-qrcode-decoder.ts'
    ????
    Thanks for the video! ill keep trying to make it work

    • @agnesdaisy5186
      @agnesdaisy5186 11 месяцев назад

      please do install the needy dependency for it ...

    • @sylviojerry3038
      @sylviojerry3038 7 месяцев назад +1

      hey bro, I got the same problem, please tell me if you found how to solve that

    • @sylviojerry3038
      @sylviojerry3038 7 месяцев назад

      what do you mean ?@@agnesdaisy5186