Skip to content

Commit

Permalink
feat: accept ArrayBuffer and TypedArray as input in readBarcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Feb 23, 2025
1 parent 86e32f9 commit 20c6f83
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-ravens-send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"zxing-wasm": minor
---

Accept `ArrayBuffer` and `TypedArray` as `input` in `readBarcodes`
2 changes: 1 addition & 1 deletion src/full/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function setZXingModuleOverrides(
}

export async function readBarcodes(
input: Blob | ImageData,
input: Blob | ArrayBuffer | Uint8Array | ImageData,
readerOptions?: ReaderOptions,
) {
return readBarcodesWithFactory(zxingModuleFactory, input, readerOptions);
Expand Down
2 changes: 1 addition & 1 deletion src/reader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function setZXingModuleOverrides(
}

export async function readBarcodes(
input: Blob | ImageData,
input: Blob | ArrayBuffer | Uint8Array | ImageData,
readerOptions?: ReaderOptions,
) {
return readBarcodesWithFactory(zxingModuleFactory, input, readerOptions);
Expand Down
40 changes: 26 additions & 14 deletions src/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function purgeZXingModuleWithFactory<T extends ZXingModuleType>(
* Reads barcodes from an image using a ZXing module factory.
*
* @param zxingModuleFactory - Factory function to create a ZXing module instance
* @param input - Source image data as either a Blob or ImageData object
* @param input - Source image data as a Blob, ArrayBuffer, Uint8Array, or ImageData
* @param readerOptions - Optional configuration options for barcode reading (defaults to defaultReaderOptions)
* @returns An array of ReadResult objects containing decoded barcode information
*
Expand All @@ -271,7 +271,7 @@ export function purgeZXingModuleWithFactory<T extends ZXingModuleType>(
*/
export async function readBarcodesWithFactory<T extends "reader" | "full">(
zxingModuleFactory: ZXingModuleFactory<T>,
input: Blob | ImageData,
input: Blob | ArrayBuffer | Uint8Array | ImageData,
readerOptions: ReaderOptions = defaultReaderOptions,
) {
const requiredReaderOptions: Required<ReaderOptions> = {
Expand All @@ -283,18 +283,8 @@ export async function readBarcodesWithFactory<T extends "reader" | "full">(
});
let zxingReadResultVector: ZXingVector<ZXingReadResult>;
let bufferPtr: number;
if ("size" in input) {
/* Blob */
const { size } = input;
const buffer = new Uint8Array(await input.arrayBuffer());
bufferPtr = zxingModule._malloc(size);
zxingModule.HEAPU8.set(buffer, bufferPtr);
zxingReadResultVector = zxingModule.readBarcodesFromImage(
bufferPtr,
size,
readerOptionsToZXingReaderOptions(requiredReaderOptions),
);
} else {

if ("width" in input && "height" in input && "data" in input) {
/* ImageData */
const {
data: buffer,
Expand All @@ -310,6 +300,28 @@ export async function readBarcodesWithFactory<T extends "reader" | "full">(
height,
readerOptionsToZXingReaderOptions(requiredReaderOptions),
);
} else {
let size: number;
let buffer: Uint8Array;
if ("buffer" in input) {
/* Uint8Array */
[size, buffer] = [input.byteLength, input];
} else if ("byteLength" in input) {
/* ArrayBuffer */
[size, buffer] = [input.byteLength, new Uint8Array(input)];
} else if ("size" in input) {
/* Blob */
[size, buffer] = [input.size, new Uint8Array(await input.arrayBuffer())];
} else {
throw new TypeError("Invalid input type");
}
bufferPtr = zxingModule._malloc(size);
zxingModule.HEAPU8.set(buffer, bufferPtr);
zxingReadResultVector = zxingModule.readBarcodesFromImage(
bufferPtr,
size,
readerOptionsToZXingReaderOptions(requiredReaderOptions),
);
}
zxingModule._free(bufferPtr);
const readResults: ReadResult[] = [];
Expand Down

0 comments on commit 20c6f83

Please sign in to comment.