zedbar 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -9
- package/bin/zedbarimg.mjs +147 -0
- package/package.json +4 -1
- package/zedbar.d.ts +12 -0
- package/zedbar.js +25 -0
- package/zedbar_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ A port of the [ZBar](http://zbar.sourceforge.net/) barcode scanning library from
|
|
|
9
9
|
- **Fast**: Native-speed scanning via WebAssembly
|
|
10
10
|
- **No native dependencies**: Works on any platform Node.js supports
|
|
11
11
|
- **Multiple formats**: QR Code, EAN-13, EAN-8, UPC-A, UPC-E, Code 128, Code 93, Code 39, Codabar, Interleaved 2 of 5, DataBar, SQ Code
|
|
12
|
+
- **CLI tool**: Command-line `zedbarimg` binary for scanning images (similar to `zbarimg`)
|
|
12
13
|
|
|
13
14
|
## Installation
|
|
14
15
|
|
|
@@ -16,31 +17,79 @@ A port of the [ZBar](http://zbar.sourceforge.net/) barcode scanning library from
|
|
|
16
17
|
npm install zedbar
|
|
17
18
|
```
|
|
18
19
|
|
|
19
|
-
## Usage
|
|
20
|
+
## Command-line Usage
|
|
21
|
+
|
|
22
|
+
The package includes a `zedbarimg` command-line tool:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# Scan a single image
|
|
26
|
+
zedbarimg barcode.png
|
|
27
|
+
|
|
28
|
+
# Scan multiple images
|
|
29
|
+
zedbarimg image1.png image2.jpg image3.webp
|
|
30
|
+
|
|
31
|
+
# Quiet mode (only output barcode data)
|
|
32
|
+
zedbarimg --quiet qrcode.png
|
|
33
|
+
|
|
34
|
+
# Show help
|
|
35
|
+
zedbarimg --help
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
After installing globally, you can use it directly:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm install -g zedbar
|
|
42
|
+
zedbarimg barcode.png
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Library Usage
|
|
46
|
+
|
|
47
|
+
### Scan from image file bytes
|
|
20
48
|
|
|
21
49
|
```javascript
|
|
22
|
-
import {
|
|
23
|
-
import
|
|
50
|
+
import { scanImageBytes } from 'zedbar';
|
|
51
|
+
import { readFileSync } from 'fs';
|
|
24
52
|
|
|
25
|
-
//
|
|
26
|
-
const
|
|
27
|
-
.grayscale()
|
|
28
|
-
.raw()
|
|
29
|
-
.toBuffer({ resolveWithObject: true });
|
|
53
|
+
// Read image file (PNG, JPEG, WebP, BMP)
|
|
54
|
+
const imageBytes = readFileSync('barcode.png');
|
|
30
55
|
|
|
31
56
|
// Scan for barcodes
|
|
32
|
-
for (const { symbolType, text } of
|
|
57
|
+
for (const { symbolType, text } of scanImageBytes(imageBytes)) {
|
|
58
|
+
console.log(`${symbolType}: ${text}`);
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Scan from grayscale data
|
|
63
|
+
|
|
64
|
+
If you already have grayscale image data:
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import { scanGrayscale } from 'zedbar';
|
|
68
|
+
|
|
69
|
+
// data is Uint8Array of grayscale pixels (1 byte per pixel, row-major)
|
|
70
|
+
for (const { symbolType, text } of scanGrayscale(data, width, height)) {
|
|
33
71
|
console.log(`${symbolType}: ${text}`);
|
|
34
72
|
}
|
|
35
73
|
```
|
|
36
74
|
|
|
37
75
|
## API
|
|
38
76
|
|
|
77
|
+
### `scanImageBytes(bytes)`
|
|
78
|
+
|
|
79
|
+
Scans an encoded image (PNG, JPEG, WebP, BMP) for barcodes and QR codes.
|
|
80
|
+
|
|
81
|
+
**Parameters:**
|
|
82
|
+
|
|
83
|
+
- `bytes` (`Uint8Array` or `Buffer`) - Raw bytes of an image file
|
|
84
|
+
|
|
85
|
+
**Returns:** `DecodeResult[]` - Array of decoded barcodes
|
|
86
|
+
|
|
39
87
|
### `scanGrayscale(data, width, height)`
|
|
40
88
|
|
|
41
89
|
Scans grayscale image data for barcodes and QR codes.
|
|
42
90
|
|
|
43
91
|
**Parameters:**
|
|
92
|
+
|
|
44
93
|
- `data` (`Uint8Array`) - Grayscale pixel data, 1 byte per pixel, row-major order
|
|
45
94
|
- `width` (`number`) - Image width in pixels
|
|
46
95
|
- `height` (`number`) - Image height in pixels
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* zedbarimg - Scan and decode barcodes from image files
|
|
5
|
+
*
|
|
6
|
+
* A command-line tool for scanning barcodes and QR codes from images.
|
|
7
|
+
* Powered by the zedbar Rust library compiled to WebAssembly.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readFileSync } from "fs";
|
|
11
|
+
import { dirname, join } from "path";
|
|
12
|
+
import { fileURLToPath } from "url";
|
|
13
|
+
import { createRequire } from "module";
|
|
14
|
+
|
|
15
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
16
|
+
const __dirname = dirname(__filename);
|
|
17
|
+
const require = createRequire(import.meta.url);
|
|
18
|
+
|
|
19
|
+
// Import the CommonJS WASM module
|
|
20
|
+
const zedbar = require("../zedbar.js");
|
|
21
|
+
|
|
22
|
+
// Parse command-line arguments
|
|
23
|
+
function parseArgs() {
|
|
24
|
+
const args = process.argv.slice(2);
|
|
25
|
+
const options = {
|
|
26
|
+
quiet: false,
|
|
27
|
+
raw: false,
|
|
28
|
+
files: [],
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < args.length; i++) {
|
|
32
|
+
const arg = args[i];
|
|
33
|
+
|
|
34
|
+
if (arg === "-q" || arg === "--quiet") {
|
|
35
|
+
options.quiet = true;
|
|
36
|
+
} else if (arg === "--raw") {
|
|
37
|
+
options.raw = true;
|
|
38
|
+
} else if (arg === "-h" || arg === "--help") {
|
|
39
|
+
showHelp();
|
|
40
|
+
process.exit(0);
|
|
41
|
+
} else if (arg === "-v" || arg === "--version") {
|
|
42
|
+
const pkgPath = join(__dirname, "..", "package.json");
|
|
43
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
44
|
+
console.log(`zedbarimg ${pkg.version}`);
|
|
45
|
+
process.exit(0);
|
|
46
|
+
} else if (!arg.startsWith("-")) {
|
|
47
|
+
options.files.push(arg);
|
|
48
|
+
} else {
|
|
49
|
+
console.error(`Unknown option: ${arg}`);
|
|
50
|
+
showHelp();
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (options.files.length === 0) {
|
|
56
|
+
console.error("Error: No image files specified\n");
|
|
57
|
+
showHelp();
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return options;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function showHelp() {
|
|
65
|
+
console.log(`
|
|
66
|
+
Usage: zedbarimg [OPTIONS] <IMAGE_FILES...>
|
|
67
|
+
|
|
68
|
+
Scan and decode barcodes from one or more image files
|
|
69
|
+
|
|
70
|
+
Options:
|
|
71
|
+
-q, --quiet Minimal output, only print decoded symbol data
|
|
72
|
+
--raw Output decoded symbol data without converting charsets
|
|
73
|
+
-h, --help Print help information
|
|
74
|
+
-v, --version Print version information
|
|
75
|
+
|
|
76
|
+
Supported formats:
|
|
77
|
+
- Images: PNG, JPEG, BMP, WebP
|
|
78
|
+
- Barcodes: QR Code, EAN-13, EAN-8, UPC-A, UPC-E, Code 128, Code 93,
|
|
79
|
+
Code 39, Codabar, Interleaved 2/5, DataBar, SQ Code
|
|
80
|
+
|
|
81
|
+
Examples:
|
|
82
|
+
zedbarimg barcode.png
|
|
83
|
+
zedbarimg --quiet qrcode.jpg
|
|
84
|
+
zedbarimg image1.png image2.jpg image3.gif
|
|
85
|
+
`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function main() {
|
|
89
|
+
const options = parseArgs();
|
|
90
|
+
let totalSymbols = 0;
|
|
91
|
+
|
|
92
|
+
for (const filename of options.files) {
|
|
93
|
+
// Read the image file as raw bytes
|
|
94
|
+
let imageBytes;
|
|
95
|
+
try {
|
|
96
|
+
imageBytes = readFileSync(filename);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
if (!options.quiet) {
|
|
99
|
+
console.error(`Failed to read file '${filename}': ${err.message}`);
|
|
100
|
+
}
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Scan the image using the WASM library
|
|
105
|
+
let results;
|
|
106
|
+
try {
|
|
107
|
+
results = zedbar.scanImageBytes(imageBytes);
|
|
108
|
+
} catch (err) {
|
|
109
|
+
if (!options.quiet) {
|
|
110
|
+
console.error(`Failed to scan image '${filename}': ${err.message}`);
|
|
111
|
+
}
|
|
112
|
+
process.exit(1);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
totalSymbols += results.length;
|
|
116
|
+
|
|
117
|
+
// Print results
|
|
118
|
+
for (const symbol of results) {
|
|
119
|
+
if (options.raw) {
|
|
120
|
+
// Raw mode: just output the data bytes with newline
|
|
121
|
+
process.stdout.write(Buffer.from(symbol.data));
|
|
122
|
+
process.stdout.write("\n");
|
|
123
|
+
} else {
|
|
124
|
+
// Normal mode: include symbol type
|
|
125
|
+
const text = symbol.text || Buffer.from(symbol.data).toString("utf8");
|
|
126
|
+
console.log(`${symbol.symbolType}:${text}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Print statistics unless quiet mode
|
|
132
|
+
if (!options.quiet) {
|
|
133
|
+
if (totalSymbols === 0) {
|
|
134
|
+
console.error("No barcodes found");
|
|
135
|
+
process.exit(1);
|
|
136
|
+
} else {
|
|
137
|
+
console.error(
|
|
138
|
+
`scanned ${totalSymbols} barcode symbol${totalSymbols === 1 ? "" : "s"} from ${options.files.length} image${options.files.length === 1 ? "" : "s"}`,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
} else if (totalSymbols === 0) {
|
|
142
|
+
// In quiet mode, still exit with error if no barcodes found
|
|
143
|
+
process.exit(1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zedbar",
|
|
3
3
|
"description": "Fast QR code and barcode scanner for Node.js, powered by WebAssembly",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"license": "LGPL-3.0-or-later",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -33,5 +33,8 @@
|
|
|
33
33
|
},
|
|
34
34
|
"engines": {
|
|
35
35
|
"node": ">=16.0.0"
|
|
36
|
+
},
|
|
37
|
+
"bin": {
|
|
38
|
+
"zedbarimg": "./bin/zedbarimg.mjs"
|
|
36
39
|
}
|
|
37
40
|
}
|
package/zedbar.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* Scan an encoded image (PNG, JPEG, BMP, WebP) for barcodes and QR codes.
|
|
5
|
+
*
|
|
6
|
+
* `bytes` should contain the raw bytes of an image file in one of the
|
|
7
|
+
* supported formats: PNG, JPEG, BMP, or WebP.
|
|
8
|
+
*
|
|
9
|
+
* The image will be automatically decoded and converted to grayscale
|
|
10
|
+
* before scanning.
|
|
11
|
+
*
|
|
12
|
+
* Returns an array of `DecodeResult` objects.
|
|
13
|
+
*/
|
|
14
|
+
export function scanImageBytes(bytes: Uint8Array): DecodeResult[];
|
|
3
15
|
/**
|
|
4
16
|
* Scan grayscale image data for barcodes and QR codes.
|
|
5
17
|
*
|
package/zedbar.js
CHANGED
|
@@ -63,6 +63,31 @@ function getArrayJsValueFromWasm0(ptr, len) {
|
|
|
63
63
|
wasm.__externref_drop_slice(ptr, len);
|
|
64
64
|
return result;
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Scan an encoded image (PNG, JPEG, BMP, WebP) for barcodes and QR codes.
|
|
68
|
+
*
|
|
69
|
+
* `bytes` should contain the raw bytes of an image file in one of the
|
|
70
|
+
* supported formats: PNG, JPEG, BMP, or WebP.
|
|
71
|
+
*
|
|
72
|
+
* The image will be automatically decoded and converted to grayscale
|
|
73
|
+
* before scanning.
|
|
74
|
+
*
|
|
75
|
+
* Returns an array of `DecodeResult` objects.
|
|
76
|
+
* @param {Uint8Array} bytes
|
|
77
|
+
* @returns {DecodeResult[]}
|
|
78
|
+
*/
|
|
79
|
+
exports.scanImageBytes = function(bytes) {
|
|
80
|
+
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
|
|
81
|
+
const len0 = WASM_VECTOR_LEN;
|
|
82
|
+
const ret = wasm.scanImageBytes(ptr0, len0);
|
|
83
|
+
if (ret[3]) {
|
|
84
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
85
|
+
}
|
|
86
|
+
var v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice();
|
|
87
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
|
|
88
|
+
return v2;
|
|
89
|
+
};
|
|
90
|
+
|
|
66
91
|
/**
|
|
67
92
|
* Scan grayscale image data for barcodes and QR codes.
|
|
68
93
|
*
|
package/zedbar_bg.wasm
CHANGED
|
Binary file
|