Gathering detailed insights and metrics for @sec-ant/zxing-wasm
Gathering detailed insights and metrics for @sec-ant/zxing-wasm
Gathering detailed insights and metrics for @sec-ant/zxing-wasm
Gathering detailed insights and metrics for @sec-ant/zxing-wasm
ZXing-C++ WebAssembly as an ES/CJS module with types. Read or write barcodes in various JS runtimes: Web, Node.js, Bun, and Deno.
npm install @sec-ant/zxing-wasm
Typescript
Module System
Node Version
NPM Version
63.9
Supply Chain
96.4
Quality
75.7
Maintenance
100
Vulnerability
100
License
TypeScript (84.35%)
C++ (12.25%)
CMake (2.42%)
Shell (0.79%)
HTML (0.18%)
Total Downloads
84,789
Last Day
18
Last Week
138
Last Month
900
Last Year
32,043
90 Stars
327 Commits
10 Forks
2 Watching
4 Branches
4 Contributors
Latest Version
2.2.0
Package Id
@sec-ant/zxing-wasm@2.2.0
Unpacked Size
2.75 MB
Size
1.21 MB
File Count
18
NPM Version
10.2.0
Node Version
20.6.1
Publised On
21 Oct 2023
Cumulative downloads
Total Downloads
Last day
-37.9%
18
Compared to previous day
Last week
-41.5%
138
Compared to previous week
Last month
-26.6%
900
Compared to previous month
Last year
-39.3%
32,043
Compared to previous year
An ES module wrapper of zxing-wasm-build. Read or write barcodes in your browser!
1git clone https://github.com/Sec-ant/zxing-wasm 2cd zxing-wasm 3npm i 4npm run fetch 5npm run build
npm i @sec-ant/zxing-wasm
This package exports 3 subpaths: full
, reader
and writer
. You can choose whichever fits your needs. If you use TypeScript, you should set moduleResolution
to bundler
, node16
or nodenext
in your tsconfig.json
file to properly resolve the exported module.
@sec-ant/zxing-wasm
or @sec-ant/zxing-wasm/full
These 2 subpaths include functions to both read and write barcodes. The wasm binary size is ~1.26 MB.
1import { 2 readBarcodesFromImageFile, 3 readBarcodesFromImageData, 4 writeBarcodeToImageFile, 5} from "@sec-ant/zxing-wasm";
or
1import { 2 readBarcodesFromImageFile, 3 readBarcodesFromImageData, 4 writeBarcodeToImageFile, 5} from "@sec-ant/zxing-wasm/full";
@sec-ant/zxing-wasm/reader
This subpath only includes functions to read barcodes. The wasm binary size is ~976 KB.
1import { 2 readBarcodesFromImageFile, 3 readBarcodesFromImageData, 4} from "@sec-ant/zxing-wasm/reader";
@sec-ant/zxing-wasm/writer
This subpath only includes functions to write barcodes. The wasm binary size is ~383 KB.
1import { writeBarcodeToImageFile } from "@sec-ant/zxing-wasm/writer";
readBarcodesFromImageFile
and readBarcodesFromImageData
These 2 functions are for reading barcodes.
readBarcodesFromImageFile
accepts an image Blob
or an image File
as the first input. They're encoded images, e.g. .png
.jpg
files.
readBarcodesFromImageData
accepts an ImageData
as the first input. They're raw pixels that usually acquired from <canvas>
or related APIs.
Both of these 2 functions accepts the same second input: ZXingReadOptions
:
1interface ZXingReadOptions { 2 /* Try harder to find barcodes, default = true */ 3 tryHarder?: boolean; 4 /* An array of barcode formats to detect, default = [] (indicates any format) */ 5 formats?: readonly ZXingReadInputBarcodeFormat[]; 6 /* Upper limit of the number of barcodes to be detected, default = 255 (max) */ 7 maxSymbols?: number; 8}
The allowed barcode formats to read are:
1type ZXingReadInputBarcodeFormat = 2 | "Aztec" 3 | "Codabar" 4 | "Code128" 5 | "Code39" 6 | "Code93" 7 | "DataBar" 8 | "DataBarExpanded" 9 | "DataMatrix" 10 | "EAN-13" 11 | "EAN-8" 12 | "ITF" 13 | "Linear-Codes" 14 | "Matrix-Codes" 15 | "MaxiCode" 16 | "MicroQRCode" 17 | "PDF417" 18 | "QRCode" 19 | "UPC-A" 20 | "UPC-E";
The return result of these 2 functions is a Promise
of an array of ZXingReadOutput
s:
1interface ZXingReadOutput { 2 /* detected barcode format */ 3 format: ZXingReadOutputBarcodeFormat; 4 /* detected barcode text */ 5 text: string; 6 /* detected barcode raw bytes */ 7 bytes: Uint8Array; 8 /* error message (if any) */ 9 error: string; 10 /* detected barcode position: 11 { 12 bottomLeft: { x, y }, 13 bottomRight: { x, y }, 14 topLeft: { x, y }, 15 topLeft: { x, y } 16 } 17 */ 18 position: ZXingPosition; 19 /* symbology identifier: https://github.com/zxing-cpp/zxing-cpp/blob/1bb03a85ef9846076fc5068b05646454f7fe6f6f/core/src/Content.h#L24 */ 20 symbologyIdentifier: string; 21 /* error correction code level: L M Q H */ 22 eccLevel: ZXingReadOutputECCLevel; 23 /* QRCode / DataMatrix / Aztec version or size */ 24 version: string; 25 /* orientation of barcode in degree */ 26 orientation: number; 27 /* is the symbol mirrored (currently only supported by QRCode and DataMatrix) */ 28 isMirrored: boolean; 29 /* is the symbol inverted / has reveresed reflectance */ 30 isInverted: boolean; 31}
e.g.
1import { 2 readBarcodesFromImageFile, 3 readBarcodesFromImageData, 4 ZXingReadOptions, 5} from "@sec-ant/zxing-wasm/reader"; 6 7const zxingReadOptions: ZXingReadOptions = { 8 tryHarder: true, 9 formats: ["QRCode"], 10 maxSymbols: 1, 11}; 12 13/** 14 * Read from image file/blob 15 */ 16const imageFile = await fetch( 17 "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Hello%20world!", 18).then((resp) => resp.blob()); 19 20const imageFileReadOutputs = await readBarcodesFromImageFile( 21 imageFile, 22 zxingReadOptions, 23); 24 25console.log(imageFileReadOutputs[0].text); // Hello world! 26 27/** 28 * Read from image data 29 */ 30const imageData = await createImageBitmap(imageFile).then((imageBitmap) => { 31 const { width, height } = imageBitmap; 32 const context = new OffscreenCanvas(width, height).getContext( 33 "2d", 34 ) as OffscreenCanvasRenderingContext2D; 35 context.drawImage(imageBitmap, 0, 0, width, height); 36 return context.getImageData(0, 0, width, height); 37}); 38 39const imageDataReadOutputs = await readBarcodesFromImageData( 40 imageData, 41 zxingReadOptions, 42); 43 44console.log(imageDataReadOutputs[0].text); // Hello world!
writeBarcodeToImageFile
There is only 1 function to write barcodes. The first argument of this function is a text string to be encoded and the second argument is a ZXingWriteOptions
:
1interface ZXingWriteOptions { 2 /* barcode format to write 3 "DataBar", "DataBarExpanded", "MaxiCode" and "MicroQRCode" are currently not supported 4 default = "QRCode" */ 5 format?: ZXingWriteInputBarcodeFormat; 6 /* encoding charset, default = "UTF-8" */ 7 charset?: ZXingCharacterSet; 8 /* barcode margin, default = 10 */ 9 quietZone?: number; 10 /* barcode width, default = 200 */ 11 width?: number; 12 /* barcode height, default = 200 */ 13 height?: number; 14 /* (E)rror (C)orrection (C)apability level, -1 ~ 8, default = -1 (default) */ 15 eccLevel?: ZXingWriteInputECCLevel; 16}
The return result of this function is a Promise
of ZXingWriteOutput
:
1interface ZXingWriteOutput { 2 /* a png image blob, or null */ 3 image: Blob | null; 4 /* the error reason if image is null */ 5 error: string; 6}
e.g.
1import { writeBarcodeToImageFile } from "@sec-ant/zxing-wasm/writer"; 2 3const writeOutput = await writeBarcodeToImageFile("Hello world!", { 4 format: "QRCode", 5 charset: "UTF-8", 6 quietZone: 5, 7 width: 150, 8 height: 150, 9 eccLevel: 2, 10}); 11 12console.log(writeOutput.image);
When using this package, the wasm binary needs to be served along with the JS glue code. In order to provide a smooth dev experience, the wasm binary serve path is automatically assigned the jsDelivr CDN url upon build.
If you would like to change the serve path (to one of your local network hosts or other CDNs), please use setZXingModuleOverrides
to override the locateFile
function in advance. locateFile
is one of the Emscripten Module
attribute hooks that can affect the code execution of the Module
object during its lifecycles.
1import { 2 setZXingModuleOverrides, 3 writeBarcodeToImageFile, 4} from "@sec-ant/zxing-wasm"; 5 6// override the locateFile function 7setZXingModuleOverrides({ 8 locateFile: (path, prefix) => { 9 if (path.endsWith(".wasm")) { 10 return `https://esm.sh/@sec-ant/zxing-wasm/dist/full/${path}`; 11 } 12 return prefix + path; 13 }, 14}); 15 16// call read or write functions afterwards 17const writeOutput = await writeBarcodeToImageFile("Hello world!");
The wasm binary won't be fetched or instantiated unless a read or write function is firstly called, and will only be instantiated once given the same module overrides. So there'll be a cold start in the first function call (or several calls if they appear in a very short period). If you want to manually trigger the download and instantiation of the wasm binary prior to any read or write functions, you can use getZXingModule
. This function will also return a Promise
that resolves to a ZXingModule
.
1import { getZXingModule } from "@sec-ant/zxing-wasm"; 2 3/** 4 * This function will trigger the download and 5 * instantiation of the wasm binary immediately 6 */ 7const zxingModulePromise1 = getZXingModule(); 8 9const zxingModulePromise2 = getZXingModule(); 10 11console.log(zxingModulePromise1 === zxingModulePromise2); // true
getZXingModule
can also optionally accept a ZXingModuleOverrides
argument.
1import { getZXingModule } from "@sec-ant/zxing-wasm"; 2 3getZXingModule({ 4 locateFile: (path, prefix) => { 5 if (path.endsWith(".wasm")) { 6 return `https://esm.sh/@sec-ant/zxing-wasm/dist/full/${path}`; 7 } 8 return prefix + path; 9 }, 10});
MIT
No vulnerabilities found.
No security vulnerabilities found.