Skip to content

Commit f4e23d1

Browse files
committed
Fix #7: use the native TextDecoder for QR codes, add more QR tests
1 parent 53d796d commit f4e23d1

File tree

10 files changed

+99
-36
lines changed

10 files changed

+99
-36
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ EM_OPTS = --rm -w /$(SRC) -v $$PWD:/$(SRC) emscripten/emsdk:$(EM_VERSION)
1414
EM_DOCKER = docker run -u $(shell id -u):$(shell id -g) $(EM_OPTS)
1515
EM_PODMAN = podman run $(EM_OPTS)
1616
EM_ENGINE = $(EM_DOCKER)
17+
18+
# See https://emscripten.org/docs/tools_reference/emcc.html
1719
EMCC = $(EM_ENGINE) emcc
1820
EMMAKE = $(EM_ENGINE) emmake
1921
EMCONFIG = $(EM_ENGINE) emconfigure
2022

2123
ZBAR_DEPS = $(ZBAR_SRC)/make.done
2224
ZBAR_OBJS = $(ZBAR_SRC)/zbar/*.o $(ZBAR_SRC)/zbar/*/*.o
2325
ZBAR_INC = -I $(ZBAR_SRC)/include/ -I $(ZBAR_SRC)/
26+
27+
# See https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
2428
EMCC_FLAGS = -Oz -Wall -Werror -s ALLOW_MEMORY_GROWTH=1 \
2529
-s EXPORTED_FUNCTIONS="['_malloc','_free']" \
2630
-s MODULARIZE=1 -s EXPORT_NAME=zbarWasm

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ An example that scans a static image file:
5252
<pre id="result"></pre>
5353

5454
<script type="module">
55-
import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/[email protected].13/dist/main.js'
55+
import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/[email protected].14/dist/main.js'
5656
5757
(async () => {
5858
const
@@ -86,15 +86,15 @@ Almost identical to the snippet above, just replace the lines
8686
```html
8787
8888
<script type="module">
89-
import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/[email protected].13/dist/main.js'
89+
import * as zbarWasm from 'https://cdn.jsdelivr.net/npm/@undecaf/[email protected].14/dist/main.js'
9090
9191
```
9292
9393
with
9494
9595
```html
9696
97-
<script src="https://cdn.jsdelivr.net/npm/@undecaf/[email protected].13/dist/index.js"></script>
97+
<script src="https://cdn.jsdelivr.net/npm/@undecaf/[email protected].14/dist/index.js"></script>
9898
<script>
9999
100100
```
@@ -105,9 +105,9 @@ with
105105
Installing:
106106
107107
```shell script
108-
$ npm install @undecaf/zbar-wasm@0.9.13
108+
$ npm install @undecaf/zbar-wasm@0.9.14
109109
or
110-
$ yarn add @undecaf/zbar-wasm@0.9.13
110+
$ yarn add @undecaf/zbar-wasm@0.9.14
111111
```
112112
113113
Using:

docs/example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ <h5>Result</h5>
6363
</div>
6464
</div>
6565

66-
<script src="https://cdn.jsdelivr.net/npm/@undecaf/[email protected].13/dist/index.js"></script>
66+
<script src="https://cdn.jsdelivr.net/npm/@undecaf/[email protected].14/dist/index.js"></script>
6767
<script src="js/main.js"></script>
6868

6969
</body>

package-lock.json

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@undecaf/zbar-wasm",
3-
"version": "0.9.13",
3+
"version": "0.9.14",
44
"description": "A WebAssembly build of the C/C++ ZBar barcode reader",
55
"type": "module",
66
"main": "./dist/main.cjs",
@@ -50,5 +50,8 @@
5050
"rollup-plugin-ts": "^2.0.4",
5151
"tslib": "^2.3.1",
5252
"typescript": "^4.5.3"
53+
},
54+
"dependencies": {
55+
"jschardet": "^3.0.0"
5356
}
5457
}

src/module.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import { ZBarImage } from './ZBarImage';
22
import { ZBarScanner } from './ZBarScanner';
33
import { ZBarSymbol } from './ZBarSymbol';
4+
import { ZBarConfigType, ZBarSymbolType } from './enum';
45

5-
const defaultScannerPromise = ZBarScanner.create();
6+
// Returns a new ZBarScanner instance that delegates QR code text decoding
7+
// to the native TextDecoder (fixes #7: Issue with utf-8)
68
export const getDefaultScanner = async () => {
7-
return await defaultScannerPromise;
9+
const scanner = await ZBarScanner.create();
10+
scanner.setConfig(ZBarSymbolType.ZBAR_NONE, ZBarConfigType.ZBAR_CFG_BINARY, 1);
11+
return scanner;
812
};
913

14+
let defaultScanner: ZBarScanner;
15+
1016
const scanImage = async (
1117
image: ZBarImage,
1218
scanner?: ZBarScanner
1319
): Promise<Array<ZBarSymbol>> => {
1420
if (scanner === undefined) {
15-
scanner = await defaultScannerPromise;
21+
// Create the default scanner lazily
22+
scanner = defaultScanner || await getDefaultScanner();
23+
defaultScanner = scanner;
1624
}
1725
const res = scanner.scan(image);
1826
if (res < 0) {

test/ZBarScanner.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { ImageFile, imageFiles } from './imageFiles';
2-
import { ZBarImage, ZBarScanner } from '../dist/main.cjs';
2+
import { getDefaultScanner, scanImageData } from '../dist/main.cjs';
33

4-
const singleBarcodes =
5-
[ 'codabar', 'code_39', 'code_93', 'code_128', 'databar', 'ean_13', 'itf', 'qr_code', 'qr_code-200mb' ];
4+
const singleBarcodes = [
5+
'codabar', 'code_39', 'code_93', 'code_128', 'databar', 'ean_13', 'itf',
6+
'qr_code', 'qr_code-200mb', 'qr_code-utf-8-1', 'qr_code-utf-8-2',
7+
];
68

79

810
test('ZBarScanner created', async () => {
9-
const scanner = await ZBarScanner.create();
11+
const scanner = await getDefaultScanner();
1012

1113
expect(scanner).toBeDefined();
1214
expect(scanner.getPointer()).toBeGreaterThan(0);
@@ -19,22 +21,16 @@ singleBarcodes.forEach(basename => {
1921
test('ZBarScanner scanning', async () => {
2022
const imageFile: ImageFile = imageFiles[basename];
2123
const imageData: ImageData = await imageFile.loadImageData();
22-
const image: ZBarImage = await ZBarImage.createFromRGBABuffer(imageData.width, imageData.height, imageData.data);
2324

24-
const scanner = await ZBarScanner.create();
25-
scanner.scan(image);
26-
const symbols = scanner.getResults();
25+
const symbols = await scanImageData(imageData);
2726

2827
imageFile.expect(symbols);
29-
30-
image.destroy();
31-
scanner.destroy();
3228
});
3329
});
3430

3531

3632
test('ZBarScanner destroyed', async () => {
37-
const scanner = await ZBarScanner.create();
33+
const scanner = await getDefaultScanner();
3834

3935
scanner.destroy();
4036

test/imageFiles.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,32 @@ class ExpectedSymbol {
88
readonly decoded: string;
99
readonly orientation: ZBarOrientation;
1010
readonly points?: Array<{ x: number, y: number }>;
11-
11+
1212
constructor(
13-
type: ZBarSymbolType,
14-
decoded: string,
15-
orientation: ZBarOrientation,
13+
type: ZBarSymbolType,
14+
decoded: string,
15+
orientation: ZBarOrientation,
1616
points?: Array<{ x: number, y: number }>
1717
) {
1818
this.type = type;
1919
this.decoded = decoded;
2020
this.orientation = orientation;
2121
this.points = points;
2222
}
23-
23+
2424
}
25-
26-
25+
26+
2727
export class ImageFile {
28-
28+
2929
readonly path: string;
3030
readonly expectedSymbols: Array<ExpectedSymbol>;
31-
31+
3232
constructor(basename: string, expectedSymbols: Array<ExpectedSymbol>) {
3333
this.path = `${__dirname}/../test/img/${basename}.png`;
3434
this.expectedSymbols = expectedSymbols;
3535
}
36-
36+
3737
async loadImageData(): Promise<ImageData> {
3838
const image = await loadImage(this.path);
3939
const canvas = createCanvas(image.width, image.height);
@@ -77,8 +77,8 @@ class ExpectedSymbol {
7777
expect(symbols).toHaveLength(this.expectedSymbols.length);
7878
}
7979
}
80-
81-
80+
81+
8282
export const imageFiles: Record<string, ImageFile> = {
8383
'codabar':
8484
new ImageFile(
@@ -177,4 +177,40 @@ export const imageFiles: Record<string, ImageFile> = {
177177
)
178178
]
179179
),
180+
181+
'qr_code-utf-8-1':
182+
new ImageFile(
183+
'qr_code-utf-8-1',
184+
[
185+
new ExpectedSymbol(
186+
ZBarSymbolType.ZBAR_QRCODE,
187+
'ÄÖÜ äöü ß ÁÉÍÓÚ áéíóú ÀÈÌÒÙ àéíóú',
188+
ZBarOrientation.ZBAR_ORIENT_UP,
189+
[
190+
{ x: 10, y: 10 },
191+
{ x: 10, y: 156 },
192+
{ x: 156, y: 156 },
193+
{ x: 156, y: 10 }
194+
]
195+
)
196+
]
197+
),
198+
199+
'qr_code-utf-8-2':
200+
new ImageFile(
201+
'qr_code-utf-8-2',
202+
[
203+
new ExpectedSymbol(
204+
ZBarSymbolType.ZBAR_QRCODE,
205+
'Thôn Hoan Trung, Chiến Thắng, Bắc Sơn, Lạng Sơn',
206+
ZBarOrientation.ZBAR_ORIENT_UP,
207+
[
208+
{ x: 26, y: 26 },
209+
{ x: 26, y: 283 },
210+
{ x: 282, y: 284 },
211+
{ x: 281, y: 26 }
212+
]
213+
)
214+
]
215+
),
180216
};

test/img/qr_code-utf-8-1.png

4.16 KB
Loading

test/img/qr_code-utf-8-2.png

10 KB
Loading

0 commit comments

Comments
 (0)