Skip to content

Commit

Permalink
Initial the documentation (#1)
Browse files Browse the repository at this point in the history
* Disable npm package lock

* Initial the documentation
  • Loading branch information
samsam2310 authored Jun 1, 2018
1 parent 87aa1a2 commit 4cc3408
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,105 @@

[![Build Status](https://travis-ci.com/samsam2310/zbar.wasm.svg?branch=master)](https://travis-ci.com/samsam2310/zbar.wasm)

A wasm build for C/C++ Zbar bar code scan library.
A webassembly build for C/C++ Zbar bar code scan library.
Webassembly can run on many morden browser without additional support, and is faster than many pure ECMAScript implementation.


## Quick Start

Online Demo: https://zbar.chino.taipei

Install:
``` bash
npm install zbar.wasm
```

Quick example:

``` javascript
import Scanner from 'zbar.wasm';

const loadImage = async src => {
// Load image
const imgBlob = await fetch(src).then(resp => resp.blob());
const img = await createImageBitmap(imgBlob);
// Make canvas same size as image
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
// Draw image onto canvas
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
return {
imageData: ctx.getImageData(0, 0, img.width, img.height),
width: img.width,
height: img.height
}
}

const main = async () => {
// Create a sannner object
const scanner = await Scanner({locateFile: file => ('data/' + file)});
const { imageData, width, height } = await loadImage('URL_TO_IMAGE');
const scanRes = scanner.scanQrcode(imageData.data, width, height);
if (scanRes.length > 0) {
console.log('Find Qrcode: ' + scanRes);
}

main();
```
## API
`Scanner.scanQrcode(imgData, width, height)`
#### Parameter
* imgData
A `Uint8Array` Object or something compatible, representing a one-dimensional array containing the data in the RGBA order, with integer values between 0 and 255 (included). See [ImageData.data](https://developer.mozilla.org/en-US/docs/Web/API/ImageData/data).
* width
The width of the image.
* height
The height of the image.
#### Return value
A array contains decode Qrcode data strings. If nothing is found, a empty list is returned.
## How to include
Although Webassembly is cross-platform and can run with browser or nodejs, on the nodejs side you may want to use native C++ Zbar instead to get better performance.
Therefore zbar.wasm is written with ES6 and recommended to load with babel + webpack.
The .wasm 'glue' code generated by Emscripten requires some node builtins, but won't actually use them in a web environment. Add this to webpack.config.js to ignore them when building. (Have Tested with webpack v4)
```
{
...
externals: {
'fs': true,
'path': true,
},
...
}
```
When initializing the scanner object, the script will try to load the `.wasm` binary file. This file `zbar.wasm` should be available through the internet (or accessable by nodejs on server side). The file can be served by CDN or use `copy-webpack-plugin`.
Scanner takes a object as argument and mix it into Webassembly Module. One usefull usage is to pass a `locateFile` function to tell all the location of the file needed by Webassembly.
For example:
``` javascript
const scanner = await Scanner({
locateFile: filename => ('https:/my.cdn.com/myproject/' + filename)
});
```
For more details, see [Demo webpack.config.js](https://github.com/samsam2310/zbar.wasm/blob/gh-pages/webpack.config.js)

0 comments on commit 4cc3408

Please sign in to comment.