npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-qr-barcode-scanner

v2.0.0

Published

A simple React Component using the client's webcam to read barcodes and QR codes.

Downloads

32,983

Readme

React QR Barcode Scanner

This is a simple React component built in Typescript to provide a webcam-based barcode scanner using react-webcam and @zxing/library. This component works on Computers and Mobile Devices (iOS 11 and above and Android Phones).

Thanks to the initial repo: https://github.com/dashboardphilippines/react-webcam-barcode-scanner

Installation

npm i react-qr-barcode-scanner

Demo

Try a demo of the scanner here.

Usage in React:

import React from "react";
import BarcodeScannerComponent from "react-qr-barcode-scanner";

function App() {
  const [data, setData] = React.useState("Not Found");

  return (
    <>
      <BarcodeScannerComponent
        width={500}
        height={500}
        onUpdate={(err, result) => {
          if (result) setData(result.text);
          else setData("Not Found");
        }}
      />
      <p>{data}</p>
    </>
  );
}

export default App;

Props

onUpdate

Type: function, Required, Argument: error, result

Function that returns the result for every captured frame. Text from barcode can be accessed from result.getText() if there is a result.

onError

Type: function, Optional, Argument: error

If passed to the component, this function is called when there is an error with the camera (rather than with with reading the QR code, which would be passed to onUpdate). An example would be an error thrown when the user does not allow the required camera permission. This can be handled with an onError function similar to this:

const onError = (error) => {
  if (error.name === "NotAllowedError") {
    // Handle messaging in our app after the user chooses to not allow the camera permissions
  }
};

width

Type: number or string, Optional, Default: 100%

height

Type: number or string, Optional, Default: 100%

facingMode

Type: environment or user, Optional, Default: environment

user is the user-facing (front) camera, and environment is the rear camera.

torch

Type: boolean, Optional

Turn the camera flashlight on or off.

delay

Type: number, Optional, Default: 500

videoConstraints

Type: MediaTrackConstraints, Optional

stopStream

Type: boolean, Optional

This prop is a workaround for a bug where the browser freezes if the webcam component is unmounted or removed. See known issues for more about this issue.

Supported Barcode Formats

These formats are supported by ZXing:

| 1D product | 1D industrial | 2D | | ---------- | ------------- | ----------- | | UPC-A | Code 39 | QR Code | | UPC-E | Code 128 | Data Matrix | | EAN-8 | ITF | Aztec | | EAN-13 | RSS-14 | PDF 417 |

Known Issues

  • The camera can only be accessed over https or localhost

  • Browser compatibility is limited by react-webcam's usage of the Stream API: https://caniuse.com/stream. On iOS-Devices with iOS < 14.3 camera access works only in native Safari and not in other Browsers (Chrome, etc) or Apps that use an UIWebView or WKWebView. iOS 14.3 (released in December 2020) now supports WebRTC in 3rd party browsers as well.

  • There is a bug in the react-webcam package that causes the browser to freeze when the component is unmounted or removed, or the camera video constraints are changed (for example, switching cameras or navigating away from the screen with the camera component). Please see this thread regarding the reported issue: https://github.com/mozmorris/react-webcam/issues/244. As a workaround, react-qr-barcode-scanner allows passing a stopStream prop to stop the video streams when true is passed, allowing you to close the stream before unmounting the component or doing some other action that may cause the freeze. I found I needed to set a timeout to wait one tick before dismissing the modal in my use case to prevent the freeze. PRs to improve this issue are welcome!

    Example:

    const [stopStream, setStopStream] = useState(false)
    //...
    const dismissQrReader = () => {
      // Stop the QR Reader stream (fixes issue where the browser freezes when closing the modal) and then dismiss the modal one tick later
      setStopStream(true)
      setTimeout(() => closeModal(), 0)
    }
    //...
    <Modal>
      <BarcodeScanner onUpdate={onUpdate} stopStream={stopStream} />
      <button onClick={dismissQrReader}>
    </Modal>

Contributing

We welcome contributions to react-qr-barcode-scanner.

If you have an idea for a new feature or have discovered a bug, please open an issue.

Please yarn build in the root and docs_src directories before pushing changes.

Don't forget to add a title and a description explaining the issue you're trying to solve and your proposed solution.