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

@undecaf/barcode-detector-polyfill

v0.9.21

Published

A WebAssembly polyfill for the Barcode Detection API

Downloads

4,310

Readme

A WebAssembly polyfill for the Barcode Detection API

Minified size Open issues Vulnerabilities Total downloads License

This package polyfills the Barcode Detection API for browsers, using a WebAssembly build of the ZBar Bar Code Reader that is written in C/C++. It offers the following features:

  • Polyfills the BarcodeDetector class
  • Supports these barcode formats: codabar, code_39, code_93, code_128, databar, databar_exp, ean_2, ean_5, ean_8, ean_13, ean_13+2, ean_13+5, isbn_10, isbn_13, isbn_13+2, isbn_13+5, itf, qr_code, sq_code, upc_a, upc_e
  • Not supported: aztec, data_matrix, pdf417
  • Scans <img>, <canvas> and live <video> elements, image and video Blobs and Files and more
  • Detects multiple barcodes per frame, also with different types
  • Barcodes may be oriented horizontally or vertically
  • Outperforms pure JavaScript polyfills

An online example is available on GitHub (source code with build scripts for Rollup and esbuild) and on CodePen.

Polyfilling BarcodeDetector

In an ES module

Install:

$ npm install @undecaf/barcode-detector-polyfill
    or
$ yarn add @undecaf/barcode-detector-polyfill

Make the polyfill available if necessary:

import { BarcodeDetectorPolyfill } from '@undecaf/barcode-detector-polyfill'

try {
    window['BarcodeDetector'].getSupportedFormats()
} catch {
    window['BarcodeDetector'] = BarcodeDetectorPolyfill
}
    ⁝

In a plain <script>

Expose the BarcodeDetectorPolyfill API in variable barcodeDetectorPolyfill:

<script src="https://cdn.jsdelivr.net/npm/@undecaf/[email protected]/dist/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@undecaf/[email protected]/dist/index.js"></script>
<script>
    try {
        window['BarcodeDetector'].getSupportedFormats()
    } catch {
        window['BarcodeDetector'] = barcodeDetectorPolyfill.BarcodeDetectorPolyfill
    }
      ⁝
</script>

Using BarcodeDetector/BarcodeDetectorPolyfill

Querying the supported barcode formats

const formats = await BarcodeDetector.getSupportedFormats()

Setting up a BarcodeDetector instance

const detector = new BarcodeDetector({ formats: ['code_39', 'code_128', 'ean_13'] })

If formats is omitted then all supported formats will be detected.

Where applicable (e.g. format 'qr_code'), text is assumed to be UTF-8 encoded. As an extension to the BarcodeDetector API, a different encoding can be set for the BarcodeDetectorPolyfill:

const detector = new BarcodeDetectorPolyfill({ 
  formats: ['qr_code'],
  zbar: {
      encoding: 'iso-8859-15'
  }
})

encoding may be any of the Encoding API encodings.

Detecting barcodes

const barcodes = await detector.detect(source)

source must be an object of which an ImageBitmap can be obtained, or else a TypeError will be thrown:

In addition, any object with a zero width or height property will be tolerated.

The detector processes the source in natural size, making detection results independent of the size rendered by the browser.

Detected barcodes are stored as an array of objects in barcodes since source may contain multiple barcodes. Each object has the following properties (see here for details):

  • format: the detected barcode format (one of the formats specified as constructor options)
  • rawValue: the decoded barcode, always a string decoded from raw data as specified
  • boundingBox: the DOMRectReadOnly enclosing the barcode in the source
  • cornerPoints: an arry of four {x, y} pairs in clockwise order, representing four corner points of the detected barcode. BarcodeDetectorPolyfill returns the boundingBox corner points.

Additional properties provided only by BarcodeDetectorPolyfill:

  • orientation: 0 → image is upright, 1 → rotated 90° clockwise, 2 → upside down, 3 → rotated 90° counterclockwise, -1 → unknown
  • quality: a positive integer indicating the barcode quality as seen by the detector, specific to each format

If an error occurs during barcode detection then the detect() method returns a rejected Promise containing the error.

Typescript support

Type definitions for BarcodeDetectorPolyfill, for constructor and method parameter types and for result types are provided in @undecaf/barcode-detector-polyfill/dist/main.d.ts.

Build configurations

Bundling dependency @undecaf/zbar-wasm

This package is under the MIT license although it depends on @undecaf/zbar-wasm which is under LGPL. Nevertheless, @undecaf/zbar-wasm may be bundled in your project as this does not violate the LGPL requirements.

Module @undecaf/zbar-wasm, however, expects the WASM file zbar.wasm to be located at the same path as itself; details can be found in the documentation of @undecaf/zbar-wasm. Therefore, bundlers must be configured accordingly. Configuration examples for Rollup and esbuild can be found in directory example-bundled. They were used to bundle the JavaScript code for this online example in docs/example-bundled.

Loading dependency @undecaf/zbar-wasm at runtime

~~In order to comply with the LGPL, @undecaf/zbar-wasm must not be bundled but may only be loaded as a library at runtime.~~ @undecaf/zbar-wasm can also be loaded at runtime, by default from https://cdn.jsdelivr.net. It can also be fetched from a different endpoint if desired.

Bundlers must be configured so that they treat @undecaf/zbar-wasm as an external dependency instead of trying to resolve it. Configuration examples for Rollup and esbuild can be found in directory example-loaded. They were used to bundle the JavaScript code for this online example in docs/example-loaded. They also illustrate how to load @undecaf/zbar-wasm from a non-default endpoint.

Credits to ...

Disclaimer

I, the author of this document, have compiled it to the best of my knowledge, but it still expresses my personal opinion. Therefore, in addition to what the licenses mentioned below state, I hereby decline any liability for any false, inaccurate, inappropriate or incomplete information that may be presented here.

License

Software: MIT

Documentation: CC-BY-SA 4.0