|
| 1 | +# A WebAssembly build of the ZBar Bar Code Reader |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +This project was forked from [ZBar.wasm](https://github.com/samsam2310/zbar.wasm), |
| 10 | +a [WebAssembly](https://webassembly.org/) build |
| 11 | +of the [ZBar Bar Code Reader](https://github.com/mchehab/zbar) written in C/C++. |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | ++ Provided as minified ES module, CommonJS module and plain script |
| 16 | ++ Runs in modern browsers, in Node.js and also in workers |
| 17 | ++ Supports Code-39, Code-93, Code-128, Codabar, Databar/Expanded, |
| 18 | + EAN/GTIN-5/8/13, ISBN-10/13, ITF (Interleaved 2 of 5), QR Code, UPC-A/E. |
| 19 | ++ Detects composite barcodes such as ISBN-13+5 and multiple barcodes per image |
| 20 | ++ Barcodes may be oriented horizontally or vertically |
| 21 | ++ Outperforms pure ECMAScript barcode scanners |
| 22 | + |
| 23 | +An example page [is available here](https://undecaf.github.io/zbar-wasm/example/) |
| 24 | +([source code](https://github.com/undecaf/zbar-wasm/tree/master/docs/example)). |
| 25 | + |
| 26 | + |
| 27 | +## Getting started |
| 28 | + |
| 29 | +### Using zbar-wasm as `<script type="module">` |
| 30 | + |
| 31 | +An example that scans a static image file: |
| 32 | + |
| 33 | +```html |
| 34 | +<!DOCTYPE html> |
| 35 | +<html> |
| 36 | +<body> |
| 37 | + <img id="img" crossorigin="anonymous" src="https://github.com/undecaf/zbar-wasm/blob/master/test/img/qr_code.png"> |
| 38 | + <pre id="result"></pre> |
| 39 | + |
| 40 | + <script type="module"> |
| 41 | + import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm/dist/main.js' |
| 42 | +
|
| 43 | + (async () => { |
| 44 | + const |
| 45 | + img = document.getElementById('img'), |
| 46 | + result = document.getElementById('result'), |
| 47 | + canvas = document.createElement('canvas'), |
| 48 | + context = canvas.getContext('2d'); |
| 49 | +
|
| 50 | + await img.decode() |
| 51 | + canvas.width = img.naturalWidth |
| 52 | + canvas.height = img.naturalHeight |
| 53 | + context.drawImage(img, 0, 0) |
| 54 | +
|
| 55 | + const |
| 56 | + imageData = context.getImageData(0, 0, canvas.width, canvas.height), |
| 57 | + symbols = await zbarWasm.scanImageData(imageData); |
| 58 | + |
| 59 | + symbols.forEach(s => s.rawData = s.decode()) |
| 60 | + result.innerText = JSON.stringify(symbols, null, 2) |
| 61 | + })() |
| 62 | + </script> |
| 63 | +</body> |
| 64 | +</html> |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +### Using zbar-wasm as plain `<script>` |
| 69 | + |
| 70 | +Almost identical to the snippet above, just replace the lines |
| 71 | + |
| 72 | +```html |
| 73 | + ⁝ |
| 74 | + <script type="module"> |
| 75 | + import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm/dist/main.js' |
| 76 | + ⁝ |
| 77 | +``` |
| 78 | +
|
| 79 | +with |
| 80 | +
|
| 81 | +```html |
| 82 | + ⁝ |
| 83 | + <script src="https://cdn.jsdelivr.net/npm/@undecaf/zbar-wasm/dist/index.js"></script> |
| 84 | + <script> |
| 85 | + ⁝ |
| 86 | +``` |
| 87 | +
|
| 88 | +
|
| 89 | +### Including zbar-wasm as ESM or as CommonJS module |
| 90 | +
|
| 91 | +Installing: |
| 92 | +
|
| 93 | +```shell script |
| 94 | +$ npm install @undecaf/zbar-wasm |
| 95 | + or |
| 96 | +$ yarn add @undecaf/zbar-wasm |
| 97 | +``` |
| 98 | +
|
| 99 | +`import ... from '@undecaf/zbar-wasm'` pulls the ES module from the package, |
| 100 | +`require('@undecaf/zbar-wasm')` pulls the CommonJS module. |
| 101 | +
|
| 102 | +Please refer to the [API documentation](#api-documentation) for what can be imported/required. |
| 103 | +
|
| 104 | +A simple Node.js example that scans a static image file: |
| 105 | +
|
| 106 | +```javascript |
| 107 | +const { createCanvas, loadImage } = require('canvas'); |
| 108 | +const { scanImageData } = require('@undecaf/zbar-wasm'); |
| 109 | +
|
| 110 | +(async (url) => { |
| 111 | + const |
| 112 | + img = await loadImage(url), |
| 113 | + canvas = createCanvas(img.width, img.height), |
| 114 | + ctx = canvas.getContext('2d'); |
| 115 | +
|
| 116 | + ctx.drawImage(img, 0, 0); |
| 117 | +
|
| 118 | + const |
| 119 | + imageData = ctx.getImageData(0, 0, img.width, img.height), |
| 120 | + symbols = await scanImageData(imageData); |
| 121 | +
|
| 122 | + console.log(symbols[0].typeName, symbols[0].decode()); |
| 123 | +}) ('https://github.com/undecaf/zbar-wasm/blob/master/test/img/qr_code.png'); |
| 124 | +``` |
| 125 | +
|
| 126 | +### Bundling/deploying zbar-wasm |
| 127 | +
|
| 128 | +zbar-wasm loads the WebAssembly file `zbar.wasm` at runtime. `zbar.wasm` must be located in the same directory |
| 129 | +as the zbar-wasm `<script>` or module, be it on a file system or at a remote endpoint. |
| 130 | +
|
| 131 | +This must be observed when bundling zbar-wasm or deploying it to a server: |
| 132 | +
|
| 133 | ++ `@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), |
| 134 | + [`rollup-plugin-copy`](https://www.npmjs.com/package/rollup-plugin-copy), [`esbuild-plugin-copy`](https://www.npmjs.com/package/esbuild-plugin-copy) |
| 135 | + or similar). |
| 136 | ++ `zbar.wasm` must be copied to the directory where the zbar-wasm module/the bundle containing that module is located. |
| 137 | ++ It should be served as `applićation/wasm` so that it can be compiled in parallel with being received |
| 138 | + by the browser. |
| 139 | +
|
| 140 | +
|
| 141 | +### Licensing considerations |
| 142 | +
|
| 143 | +Please note that zbar-wasm is licensed under the LGPL because it is derived from an LGPL-licensed work. |
| 144 | +Therefore, bundling zbar-wasm imposes the LGPL on the bundles, too. |
| 145 | +
|
| 146 | +
|
| 147 | +## API documentation |
| 148 | +
|
| 149 | +Owing to the predecessor of this project, [samsam2310/zbar.wasm](https://github.com/samsam2310/zbar.wasm), |
| 150 | +a [wiki](https://github.com/samsam2310/zbar.wasm/wiki) and an extensive |
| 151 | +[API Reference](https://github.com/samsam2310/zbar.wasm/wiki/API-Reference) are already available. |
| 152 | +Many thanks to the author! |
| 153 | +
|
| 154 | +Please note that a few classes have been renamed compared to the documentation in order to avoid |
| 155 | +conflicts with built-in JavaScript class names: |
| 156 | +
|
| 157 | ++ `Symbol` → `ZBarSymbol` |
| 158 | ++ `Image` → `ZBarImage` |
| 159 | ++ `ImageScanner` → `ZBarScanner` |
| 160 | +
|
| 161 | +
|
| 162 | +## Building zbar-wasm from source |
| 163 | +
|
| 164 | +Prerequisites: |
| 165 | +
|
| 166 | ++ A Linux platform |
| 167 | ++ GNU `make`, `tar` and `curl` |
| 168 | ++ [Docker](https://www.docker.com/) or [Podman](https://podman.io/) |
| 169 | ++ [Node.js](https://nodejs.org/) v12+ |
| 170 | +
|
| 171 | +The `Makefile` runs [emscripten](https://emscripten.org/) in a container, compiling the C/C++ |
| 172 | +sources of the [ZBar Bar Code Reader](https://github.com/mchehab/zbar) |
| 173 | +to [WebAssembly](https://webassembly.org/). It also compiles and bundles the TypeScript glue code |
| 174 | +and runs the tests in Node.js on the host machine. |
| 175 | +
|
| 176 | +To build: |
| 177 | +
|
| 178 | +```bash |
| 179 | +$ git clone https://github.com/undecaf/zbar-wasm |
| 180 | +$ cd zbar-wasm |
| 181 | +$ make |
| 182 | +``` |
| 183 | +
|
| 184 | +If you prefer [Podman](https://podman.io/) as container engine then `Makefile` needs to be edited before running `make`: |
| 185 | +replace the line |
| 186 | +
|
| 187 | +``` |
| 188 | +EM_ENGINE = $(EM_DOCKER) |
| 189 | +``` |
| 190 | +
|
| 191 | +with |
| 192 | +
|
| 193 | +``` |
| 194 | +EM_ENGINE = $(EM_PODMAN) |
| 195 | +``` |
| 196 | +
|
| 197 | +
|
| 198 | +## Credits to... |
| 199 | +
|
| 200 | ++ [samsam2310]() for providing invaluable information in his [zbar.wasm](https://github.com/samsam2310/zbar.wasm) repository |
| 201 | ++ [mchehab](https://github.com/mchehab) for maintaining [zbar](https://github.com/mchehab/zbar) |
| 202 | ++ the [emscripten](https://emscripten.org/) folks for their compiler toolchain |
| 203 | +
|
| 204 | +
|
| 205 | +## License |
| 206 | +
|
| 207 | +Software: [LGPL-2.1](http://opensource.org/licenses/LGPL-2.1) |
| 208 | +
|
| 209 | +Documentation: [CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/) |
0 commit comments