zxing-wasm
ZXing-C++ WebAssembly as an ES module with types. Read or write barcodes in your browser!
Build
git clone --recurse-submodules https://github.com/Sec-ant/zxing-wasm
cd zxing-wasm
npm i
npm run cmake
npm run build:wasm
npm run build
Install
npm i zxing-wasm
Documentation
https://zxing-wasm.netlify.app/
Usage
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.
zxing-wasm
or zxing-wasm/full
These 2 subpaths include functions to both read and write barcodes. The wasm binary size is ~1.12 MB.
import {
readBarcodesFromImageFile,
readBarcodesFromImageData,
writeBarcodeToImageFile,
} from "zxing-wasm";
or
import {
readBarcodesFromImageFile,
readBarcodesFromImageData,
writeBarcodeToImageFile,
} from "zxing-wasm/full";
zxing-wasm/reader
This subpath only includes functions to read barcodes. The wasm binary size is ~881 KB.
import {
readBarcodesFromImageFile,
readBarcodesFromImageData,
} from "zxing-wasm/reader";
zxing-wasm/writer
This subpath only includes a function to write barcodes. The wasm binary size is ~328 KB.
import { writeBarcodeToImageFile } from "zxing-wasm/writer";
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 optionally accept the same second input: DecodeHints
.
The return result of these 2 functions is a Promise
of an array of ReadResult
s.
e.g.
import {
readBarcodesFromImageFile,
readBarcodesFromImageData,
type DecodeHints,
} from "zxing-wasm/reader";
const decodeHints: DecodeHints = {
tryHarder: true,
formats: ["QRCode"],
maxNumberOfSymbols: 1,
};
const imageFile = await fetch(
"https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Hello%20world!",
).then((resp) => resp.blob());
const imageFileReadResults = await readBarcodesFromImageFile(
imageFile,
decodeHints,
);
console.log(imageFileReadResults[0].text);
const imageData = await createImageBitmap(imageFile).then((imageBitmap) => {
const { width, height } = imageBitmap;
const context = new OffscreenCanvas(width, height).getContext(
"2d",
) as OffscreenCanvasRenderingContext2D;
context.drawImage(imageBitmap, 0, 0, width, height);
return context.getImageData(0, 0, width, height);
});
const imageDataReadResults = await readBarcodesFromImageData(
imageData,
decodeHints,
);
console.log(imageDataReadResults[0].text);
This function is used to write barcodes. The first argument of this function is a text string to be encoded and the optional second argument is an EncodeHints
.
The return result of this function is a Promise
of a WriteResult
.
e.g.
import { writeBarcodeToImageFile, type EncodeHints } from "zxing-wasm/writer";
const encodeHints: EncodeHints = {
format: "QRCode",
width: 150,
height: 150,
margin: 10,
eccLevel: 2,
};
const writeOutput = await writeBarcodeToImageFile("Hello world!", encodeHints);
console.log(writeOutput.image);
Notes
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 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, some other CDNs, or just Base64 encoded data URIs), 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.
e.g.
import { setZXingModuleOverrides, writeBarcodeToImageFile } from "zxing-wasm";
setZXingModuleOverrides({
locateFile: (path, prefix) => {
if (path.endsWith(".wasm")) {
return `https://esm.sh/zxing-wasm/dist/full/${path}`;
}
return prefix + path;
},
});
const 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 (Object.is
) ZXingModuleOverrides. 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
.
import { getZXingModule } from "zxing-wasm";
const zxingModulePromise1 = getZXingModule();
const zxingModulePromise2 = getZXingModule();
console.log(zxingModulePromise1 === zxingModulePromise2);
getZXingModule
can also optionally accept a ZXingModuleOverrides
argument.
import { getZXingModule } from "zxing-wasm";
getZXingModule({
locateFile: (path, prefix) => {
if (path.endsWith(".wasm")) {
return `https://esm.sh/zxing-wasm/dist/full/${path}`;
}
return prefix + path;
},
});
License
MIT