Skip to content

Commit 42c1066

Browse files
committed
Initial push to repository
0 parents  commit 42c1066

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+6867
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Coverage directory used by tools like istanbul
2+
coverage
3+
4+
# Dependency directories
5+
node_modules/
6+
7+
# Intermediate and distribution artifacts
8+
build/
9+
dist/
10+
zbar-*/
11+
zbar-*.tar.gz
12+
undecaf-*.tgz

LICENSE

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
ZBAR_VERSION = 0.23.90
2+
ZBAR_SRC = zbar-$(ZBAR_VERSION)
3+
4+
SRC = src
5+
BUILD = build
6+
DIST = dist
7+
TEST = test
8+
TEST_TS = $(wildcard ./$(TEST)/*.ts)
9+
TEST_JS = $(patsubst ./$(TEST)/%.ts,./$(BUILD)/%.js,$(TEST_TS))
10+
TEST_COVERAGE = ./coverage
11+
12+
EM_VERSION = 3.1.0
13+
EM_OPTS = --rm -w /$(SRC) -v $$PWD:/$(SRC) emscripten/emsdk:$(EM_VERSION)
14+
EM_DOCKER = docker run -u $(shell id -u):$(shell id -g) $(EM_OPTS)
15+
EM_PODMAN = podman run $(EM_OPTS)
16+
EM_ENGINE = $(EM_DOCKER)
17+
EMCC = $(EM_ENGINE) emcc
18+
EMMAKE = $(EM_ENGINE) emmake
19+
EMCONFIG = $(EM_ENGINE) emconfigure
20+
21+
ZBAR_DEPS = $(ZBAR_SRC)/make.done
22+
ZBAR_OBJS = $(ZBAR_SRC)/zbar/*.o $(ZBAR_SRC)/zbar/*/*.o
23+
ZBAR_INC = -I $(ZBAR_SRC)/include/ -I $(ZBAR_SRC)/
24+
EMCC_FLAGS = -Oz -Wall -Werror -s ALLOW_MEMORY_GROWTH=1 \
25+
-s EXPORTED_FUNCTIONS="['_malloc','_free']" \
26+
-s MODULARIZE=1 -s EXPORT_NAME=zbarWasm
27+
28+
BUNDLES = $(DIST)/main.js $(DIST)/main.cjs $(DIST)/index.js
29+
30+
TSC = npx tsc
31+
TSC_FLAGS = -p ./tsconfig.test.json
32+
33+
ROLLUP = npx rollup
34+
ROLLUP_FLAGS = -c
35+
36+
.PHONY: all dist test clean-build clean
37+
38+
all: dist test
39+
40+
test: dist $(TEST_JS)
41+
jest --config ./jest.config.cjs --coverage
42+
43+
dist: $(BUNDLES) $(DIST)/zbar.wasm
44+
45+
clean-build:
46+
-rm -rf $(DIST) $(BUILD)
47+
48+
clean: clean-build
49+
-rm $(ZBAR_SRC).tar.gz
50+
-rm -rf $(ZBAR_SRC) $(TEST_COVERAGE)
51+
52+
$(TEST_JS): $(TEST_TS) $(BUNDLES) tsconfig.json tsconfig.test.json
53+
$(TSC) $(TSC_FLAGS)
54+
55+
$(BUNDLES): $(BUILD)/zbar.js $(BUILD)/zbar.mjs $(SRC)/*.ts tsconfig.json rollup.config.js package.json
56+
$(ROLLUP) $(ROLLUP_FLAGS)
57+
58+
$(DIST)/zbar.wasm: $(BUILD)/zbar.wasm
59+
mkdir -p $(DIST)/
60+
cp $(BUILD)/zbar.wasm $(DIST)/
61+
62+
$(BUILD)/zbar.wasm $(BUILD)/zbar.js $(BUILD)/zbar.mjs: $(ZBAR_DEPS) $(SRC)/module.c $(BUILD)/symbol.test.o
63+
$(EMCC) $(EMCC_FLAGS) -o $(BUILD)/zbar.js $(SRC)/module.c $(ZBAR_INC) $(ZBAR_OBJS)
64+
$(EMCC) $(EMCC_FLAGS) -o $(BUILD)/zbar.mjs $(SRC)/module.c $(ZBAR_INC) $(ZBAR_OBJS)
65+
66+
$(BUILD)/symbol.test.o: $(ZBAR_DEPS) $(TEST)/symbol.test.c
67+
mkdir -p $(BUILD)/
68+
$(EMCC) -Wall -Werror -g2 -c $(TEST)/symbol.test.c -o $@ $(ZBAR_INC)
69+
70+
$(ZBAR_DEPS): $(ZBAR_SRC)/Makefile
71+
cd $(ZBAR_SRC) && $(EMMAKE) make CFLAGS=-Os CXXFLAGS=-Os \
72+
DEFS="-DZNO_MESSAGES -DHAVE_CONFIG_H"
73+
touch -m $(ZBAR_DEPS)
74+
75+
$(ZBAR_SRC)/Makefile: $(ZBAR_SRC)/configure
76+
cd $(ZBAR_SRC) && $(EMCONFIG) ./configure --without-x --without-xshm \
77+
--without-xv --without-jpeg --without-libiconv-prefix \
78+
--without-imagemagick --without-npapi --without-gtk \
79+
--without-python --without-qt --without-xshm --disable-video \
80+
--disable-pthread --disable-assert
81+
82+
$(ZBAR_SRC)/configure: $(ZBAR_SRC).tar.gz
83+
tar zxvf $(ZBAR_SRC).tar.gz
84+
touch -m $(ZBAR_SRC)/configure
85+
86+
$(ZBAR_SRC).tar.gz:
87+
curl -L -o $(ZBAR_SRC).tar.gz https://linuxtv.org/downloads/zbar/zbar-$(ZBAR_VERSION).tar.gz

README.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# A WebAssembly build of the ZBar Bar Code Reader
2+
3+
![Minified size](https://badgen.net/bundlephobia/min/@undecaf/zbar-wasm)
4+
![Open issues](https://badgen.net/github/open-issues/undecaf/zbar-wasm)
5+
![Vulnerabilities](https://snyk.io/test/npm/@undecaf/zbar-wasm/badge.svg)
6+
![Total downloads](https://badgen.net/npm/dt/@undecaf/zbar-wasm)
7+
![License](https://badgen.net/github/license/undecaf/zbar-wasm)
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` &rarr; `ZBarSymbol`
158+
+ `Image` &rarr; `ZBarImage`
159+
+ `ImageScanner` &rarr; `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

Comments
 (0)