Gathering detailed insights and metrics for @preflower/barcode-detector-polyfill
Gathering detailed insights and metrics for @preflower/barcode-detector-polyfill
Gathering detailed insights and metrics for @preflower/barcode-detector-polyfill
Gathering detailed insights and metrics for @preflower/barcode-detector-polyfill
A WebAssembly polyfill for the Barcode Detection API
npm install @preflower/barcode-detector-polyfill
Typescript
Module System
Node Version
NPM Version
JavaScript (68.73%)
TypeScript (31.27%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
144 Stars
47 Commits
15 Forks
3 Watchers
2 Branches
4 Contributors
Updated on Jul 12, 2025
Latest Version
0.9.21
Package Id
@preflower/barcode-detector-polyfill@0.9.21
Unpacked Size
120.67 kB
Size
31.92 kB
File Count
11
NPM Version
10.1.0
Node Version
20.7.0
Published on
Nov 29, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
26
fork from @undecaf/barcode-detector-polyfill, but use dependency import @undecaf/zbar-wasm instead of dynmiac import
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:
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
aztec
, data_matrix
, pdf417
<img>
, <canvas>
and live <video>
elements, image and video Blob
s and File
s and moreAn online example is available on GitHub (source code with build scripts for Rollup and esbuild) and on CodePen.
BarcodeDetector
Install:
1$ npm install @undecaf/barcode-detector-polyfill 2 or 3$ yarn add @undecaf/barcode-detector-polyfill
Make the polyfill available if necessary:
1import { BarcodeDetectorPolyfill } from '@undecaf/barcode-detector-polyfill' 2 3try { 4 window['BarcodeDetector'].getSupportedFormats() 5} catch { 6 window['BarcodeDetector'] = BarcodeDetectorPolyfill 7} 8 ⁝
<script type="module">
Import the BarcodeDetectorPolyfill
API:
1<script type="module"> 2 import { BarcodeDetectorPolyfill } from "https://cdn.jsdelivr.net/npm/@undecaf/barcode-detector-polyfill@0.9.21/dist/main.js"; 3 4 try { 5 window['BarcodeDetector'].getSupportedFormats() 6 } catch { 7 window['BarcodeDetector'] = BarcodeDetectorPolyfill 8 } 9 ⁝ 10</script>
A detailed example is available here.
<script>
Expose the BarcodeDetectorPolyfill
API in variable barcodeDetectorPolyfill
:
1<script src="https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.15/dist/index.js"></script> 2<script src="https://cdn.jsdelivr.net/npm/@undecaf/barcode-detector-polyfill@0.9.21/dist/index.js"></script> 3<script> 4 try { 5 window['BarcodeDetector'].getSupportedFormats() 6 } catch { 7 window['BarcodeDetector'] = barcodeDetectorPolyfill.BarcodeDetectorPolyfill 8 } 9 ⁝ 10</script>
A detailed example is available here.
BarcodeDetector
/BarcodeDetectorPolyfill
1const formats = await BarcodeDetector.getSupportedFormats()
BarcodeDetector
instance1const 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
:
1const detector = new BarcodeDetectorPolyfill({ 2 formats: ['qr_code'], 3 zbar: { 4 encoding: 'iso-8859-15' 5 } 6})
encoding
may be any of the Encoding API encodings.
1const barcodes = await detector.detect(source)
source
must be an object of which an ImageBitmap
can be obtained, or else a
TypeError
will be thrown:
<img>
element (HTMLImageElement
)<video>
element (HTMLVideoElement
)
from which a single frame will be taken<canvas>
element (HTMLCanvasElement
)ImageBitmap
ImageData
instanceCanvasRenderingContext2D
Blob
or a
File
with a content type
of image/*
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 specifiedboundingBox
: 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
→ unknownquality
: 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.
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
.
@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
.
@undecaf/zbar-wasm
at runtimeIn 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.
barcode-detector-zbar
that has an extensive list of useful linksI, 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.
Software: MIT
Documentation: CC-BY-SA 4.0
No vulnerabilities found.
No security vulnerabilities found.