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-barcode-detection

v0.0.15

Published

Easy-to-use React component for detecting barcodes

Downloads

106

Readme

React-barcode-detection ci nycrc config on GitHub

React-barcode-detection provides an easy-to-use component that lets your app capture barcodes of various formats, including 2D varieties such as QR code. It utilizes the new Barcode Detection API. On platforms that lack support, the library provides fallbacks for scanning QR code.

Video capture functionality is provided by react-media-capture.

Insallation

npm install --save-dev react-barcode-detection

If you're using npm 14.x, you'll need to manually install the peer dependency react-seq.

Basic usage

import { useState } from 'react;
import { BarcodeScanner } from 'react-barcode-detection';

function QRScreen() {
  const [ data, setData ] = useState();
  return (
    <div className="QRScreen">
      <BarcodeScanner onData={setData} />;
      <div className="data">{data}</div>
    </div>
  );
}

BarcodeScanner connects to the device's camera and provides a live preview. By default it scans for QR code. When one enters into the camera's view, the component invokes the onData callback. When the code disappears, the same callback is given undefined.

Providing visual cue

It's important to let the user know that your app is able to recognize a code. Use the cornerPoints prop to tell BarcodeScanner to draw a quadrilateral where the code appears in the video:

const cp = {
  stroke: '#00ff00',
  fill: 'rgba(255, 0, 0, 0.25)',
  lineWidth: 3,
};

function QRScreen() {
  const [ data, setData ] = useState();
  return (
    <div className="QRScreen">
      <BarcodeScanner cornerPoints={cp} onData={setData} />;
      <div className="data">{data}</div>
    </div>
  );
}

The code above draws a quadrilateral with green outline and red translucent interior:

cornerPoints

Alternately, you can draw a "targetting box" over the area by specifying boundingBox:

const bb = {
  stroke: '#fff',
  lineWidth: 4,
  radii: 10,
  gap: 0.5,
  margin: 0.1
};

function QRScreen() {
  const [ data, setData ] = useState();
  return (
    <div className="QRScreen">
      <BarcodeScanner boundingBox={bb} onData={setData} />;
      <div className="data">{data}</div>
    </div>
  );
}

boundingBox

If you app exits from the scanner screen as soon as it finds a correct code, you might choose to delay the exit slightly so the user actually has a chance to see the visual indicator. In that case you would want to make clearInterval longer (the default is 250ms) so the code does not disappear (due to the user's shaky hand, for instance) during the delay.

Another way to let the user know that a code has been captured is to switch from the live video to a frozen frame. You app will receive a JPEG snapshot upon barcode detection when you provide a onSnapshot handler.

Check out the live demo to see the component in action. You can see the source code here.

Detecting other barcode types

BarcodeScanner looks for QR codes by default. To scan for barcodes of other formats, set the accept prop to a comma-delimited list of formats:

const bb = {
  stroke: '#fff',
  lineWidth: 4,
  radii: 10,
  gap: 0.5,
  margin: 0.1
};

function UTCScreen() {
  const [ data, setData ] = useState();
  return (
    <div className="UTCScreen">
      <BarcodeScanner boundingBox={bb} accept="upc_a,upc_e" onData={setData} />;
      <div className="data">{data}</div>
    </div>
  );
}

A list of available formats can be found at Mozilla.

QR code support fallback

As of writing, the Barcode Detection API is not available on Windows or iOS. This library provides fallback for scanning QR codes using jsQR.

A second fallback based on quirc is available. It's more efficient since it uses WebAssembly, but is not as tolerant of poor lighting conditions as jsQR. It might be appropriate for less powerful mobile devices.

To enable the use of quirc, set the use prop to "api,quirc,jsqr":

const cp = {
  fill: 'rgba(0, 255, 0, 0.5)',
};

function UTCScreen() {
  const [ data, setData ] = useState();
  return (
    <div className="UTCScreen">
      <BarcodeScanner cornerPoints={cp} use="api,quirc,jsqr" onData={setData} />;
      <div className="data">{data}</div>
    </div>
  );
}

Live demo

The live demo is designed to run on either a desktop or a mobile environment. It shows you the different ways your app can respond to the detection of a QR code.

live demo

Click on one of the letters on the left to toggle the different methods. "A" is the Barcode API (will be used if available), "Q" is the WebAssembly-based quirc. "J" is jsQR.

API Reference