11# A WebAssembly build of the ZBar Bar Code Reader
22
3- ![ Install size] ( https://badgen.net/packagephobia/install/@undecaf/zbar-wasm?color=42cc24 )
43![ Open issues] ( https://badgen.net/github/open-issues/undecaf/zbar-wasm )
54![ Vulnerabilities] ( https://snyk.io/test/npm/@undecaf/zbar-wasm/badge.svg )
65![ Total downloads] ( https://badgen.net/npm/dt/@undecaf/zbar-wasm )
@@ -15,6 +14,7 @@ of the [ZBar Bar Code Reader](https://github.com/mchehab/zbar) written in C/C++.
1514
1615+ Provided as minified ES module, CommonJS module and plain script
1716+ Runs in modern browsers, in Node.js and also in workers
17+ + Deployment size approx. 330 kByte
1818+ Supports Code-39, Code-93, Code-128, Codabar, Databar/Expanded,
1919 EAN/GTIN-5/8/13, ISBN-10/13, ISBN-13+2, ISBN-13+5, ITF (Interleaved 2 of 5), QR Code, UPC-A/E.
2020+ Detects multiple barcodes per frame, also with different types
@@ -23,6 +23,8 @@ of the [ZBar Bar Code Reader](https://github.com/mchehab/zbar) written in C/C++.
2323 RGB/grayscale [ ` ArrayBuffer ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer ) objects
2424+ Outperforms pure JavaScript barcode scanners
2525
26+ :warning : v0.10.0 contains breaking changes regarding bundling, please refer to section [ Bundling/deploying zbar-wasm] ( #bundling-deploying-zbar-wasm ) .
27+
2628
2729## Examples based on zbar-wasm
2830
@@ -48,11 +50,11 @@ An example that scans a static image file:
4850<!DOCTYPE html>
4951<html >
5052<body >
51- <img id =" img" crossorigin =" anonymous" src =" https://raw.githubusercontent.com/undecaf/zbar-wasm/master/test /img/qr_code.png" >
53+ <img id =" img" crossorigin =" anonymous" src =" https://raw.githubusercontent.com/undecaf/zbar-wasm/master/tests /img/qr_code.png" >
5254 <pre id =" result" ></pre >
5355
5456 <script type =" module" >
55- import * as zbarWasm from ' https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.16 /dist/main.js'
57+ import * as zbarWasm from ' https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.0 /dist/main.js'
5658
5759 (async () => {
5860 const
@@ -86,28 +88,28 @@ Almost identical to the snippet above, just replace the lines
8688``` html
8789 ⁝
8890 <script type =" module" >
89- import * as zbarWasm from ' https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.16 /dist/main.js'
91+ import * as zbarWasm from ' https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.0 /dist/main.js'
9092 ⁝
9193` ` `
9294
9395with
9496
9597` ` ` html
9698 ⁝
97- < script src= " https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.9.16 /dist/index.js" > </script >
99+ < script src= " https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm@0.10.0 /dist/index.js" > </script >
98100 <script >
99101 ⁝
100102` ` `
101103
102104
103- ### Including zbar-wasm as ESM or as CommonJS module
105+ ### Using zbar-wasm as an ESM or CommonJS module in Node.js
104106
105107Installing:
106108
107109` ` ` shell script
108- $ npm install @undecaf/ zbar- wasm@0.9 . 16
110+ $ npm install @undecaf/ zbar- wasm@0.10 . 0
109111 or
110- $ yarn add @undecaf/ zbar- wasm@0.9 . 16
112+ $ yarn add @undecaf/ zbar- wasm@0.10 . 0
111113` ` `
112114
113115Using:
@@ -117,42 +119,100 @@ Using:
117119
118120Please refer to the [API documentation](#api-documentation) for what can be imported/required.
119121
120- A simple Node.js example that scans a static image file:
122+ A simple ES module that scans a static image file:
121123
122124` ` ` javascript
123- const { createCanvas , loadImage } = require ( ' canvas' ) ;
124- const { scanImageData } = require ( ' @undecaf/zbar-wasm' ) ;
125+ import { createCanvas , loadImage } from ' canvas' ;
126+ import { scanImageData } from ' @undecaf/zbar-wasm' ;
125127
126128(async (url ) => {
127129 const
128- img = await loadImage (url),
129- canvas = createCanvas (img .width , img .height ),
130- ctx = canvas .getContext (' 2d' );
130+ img = await loadImage (url),
131+ canvas = createCanvas (img .width , img .height ),
132+ ctx = canvas .getContext (' 2d' );
131133
132- ctx .drawImage (img, 0 , 0 );
134+ ctx .drawImage (img, 0 , 0 )
133135
134136 const
135- imageData = ctx .getImageData (0 , 0 , img .width , img .height ),
136- symbols = await scanImageData (imageData);
137+ imageData = ctx .getImageData (0 , 0 , img .width , img .height ),
138+ // @ts-ignore
139+ symbols = await scanImageData (imageData);
137140
138- console .log (symbols [0 ].typeName , symbols [0 ].decode ());
139- }) (' https://raw.githubusercontent.com/undecaf/zbar-wasm/master/test /img/qr_code.png' );
141+ console .log (ssymbols [0 ]? .typeName , ymbols [0 ]? .decode ())
142+ })(' https://raw.githubusercontent.com/undecaf/zbar-wasm/master/tests /img/qr_code.png' )
140143` ` `
141144
142- ### Bundling/deploying zbar-wasm
145+ For a CommonJS module, just replace the first lines with
143146
144- zbar-wasm loads the WebAssembly file ` zbar .wasm ` at runtime. ` zbar .wasm ` must be located in the same directory
145- as the zbar-wasm ` < script> ` or module, be it on a file system or at a remote endpoint.
147+ ` ` ` javascript
148+ const { createCanvas , loadImage } = require (' canvas' );
149+ const { scanImageData } = require (' @undecaf/zbar-wasm' );
150+ ` ` `
146151
147- This must be observed when bundling zbar-wasm or deploying it to a server:
148152
149- + ` @undecaf/ zbar- wasm/ dist/ zbar .wasm ` must be copied as-is (e.g. using [` copy- webpack- plugin` ](https://www.npmjs.com/package/copy-webpack-plugin),
150- [` rollup- plugin- copy` ](https://www.npmjs.com/package/rollup-plugin-copy), [` esbuild- plugin- copy` ](https://www.npmjs.com/package/esbuild-plugin-copy)
151- or similar).
152- + ` zbar .wasm ` must be copied to the directory where the zbar-wasm module/the bundle containing that module is located.
153- + It should be served as ` application/ wasm` so that it can be compiled in parallel with being received
154- by the browser.
153+ ### Bundling/deploying zbar-wasm
155154
155+ zbar-wasm delegates barcode scanning to the WebAssembly code in file ` zbar .wasm ` .
156+ This file is part of the package and can be provided at runtime in different ways:
157+
158+ + It can be loaded from a CDN by browsers.
159+ + It can be bundled as an asset. That asset should be served to browsers as ` application/ wasm`
160+ + so that it can be compiled in parallel with being received.
161+ + zbar-wasm also provides modules that contain ` zbar .wasm ` as inline data.
162+
163+ The [package entry points](https://nodejs.org/docs/latest-v16.x/api/packages.html#package-entry-points)
164+ of zbar-wasm have been chosen so that bundlers will select the
165+ appropriate module by default in most cases. ` zbar .wasm ` as inline data requires an
166+ [export condition](https://nodejs.org/docs/latest-v16.x/api/packages.html#conditional-exports)
167+ in the bundler configuration.
168+
169+ The build process of zbar-wasm tests bundling with
170+ [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/) and
171+ [esbuild](https://esbuild.github.io/) and also tests the resulting bundles.
172+ The bundler configuration files
173+ [` tests/ {webpack,rollup,esbuild}.config .js ` ](https://github.com/undecaf/zbar-wasm/tree/master/tests)
174+ may be used as a reference of how to achieve a particular bundling result. They cover the following
175+ combinations of platform, module type and ` zbar .wasm ` provisioning:
176+
177+ <table>
178+ <thead>
179+ <tr>
180+ <th style='text-align: left;'>Platform →</th>
181+ <th colspan='2'>Node.js</th>
182+ <th colspan='2'>Browser</th>
183+ </tr>
184+ <tr>
185+ <th style='text-align: left;'><code>zbar.wasm</code>↓</th>
186+ <th>ESM</th>
187+ <th>CommonJS</th>
188+ <th>ESM</th>
189+ <th>Script</th>
190+ </tr>
191+ </thead>
192+ <tbody>
193+ <tr>
194+ <th style='text-align: left;'>from CDN</th>
195+ <td></td>
196+ <td></td>
197+ <td>:white_check_mark:</td>
198+ <td>:white_check_mark:</td>
199+ </tr>
200+ <tr>
201+ <th style='text-align: left;'>bundled</th>
202+ <td>:white_check_mark:</td>
203+ <td>:white_check_mark:</td>
204+ <td>:white_check_mark:</td>
205+ <td></td>
206+ </tr>
207+ <tr>
208+ <th style='text-align: left;'>inlined</th>
209+ <td>:white_check_mark:</td>
210+ <td>:white_check_mark:</td>
211+ <td>:white_check_mark:</td>
212+ <td>:white_check_mark:</td>
213+ </tr>
214+ </tbody>
215+ </table>
156216
157217## API documentation
158218
@@ -183,7 +243,7 @@ Prerequisites:
183243+ A Linux platform
184244+ GNU ` make` , ` tar` and ` curl`
185245+ [Docker](https://www.docker.com/) or [Podman](https://podman.io/)
186- + [Node.js](https://nodejs.org/) v12 +
246+ + [Node.js](https://nodejs.org/) v16 +
187247
188248To build:
189249
0 commit comments