Add barcode-detector-api-polyfill dependency and update environment variable declarations in SvelteKit project
CI / build (push) Has been skipped
CI / deploy (push) Successful in 1m50s

This commit is contained in:
eewing
2026-02-17 14:58:53 -06:00
parent cff20a652a
commit 9022114c8d
2137 changed files with 201996 additions and 762 deletions
+11
View File
@@ -0,0 +1,11 @@
import type { IDetectedBarcode, IBarcodeDetector } from './models';
export declare class BarcodeDetector implements IBarcodeDetector {
private reader;
constructor(options?: {
formats: string[];
});
static getSupportedFormats(): Promise<string[]>;
detect(imageSource: ImageBitmapSource): Promise<IDetectedBarcode[]>;
private blobToImage;
private imageDataSourceToCanvas;
}
+84
View File
@@ -0,0 +1,84 @@
import { BrowserMultiFormatReader } from '@zxing/browser';
import { DecodeHintType, NotFoundException } from '@zxing/library';
import { nativeToZxingFormat, zxingToNativeFormat } from './constants';
export class BarcodeDetector {
reader;
constructor(options) {
const hints = new Map([
[DecodeHintType.TRY_HARDER, true],
[
DecodeHintType.POSSIBLE_FORMATS,
options
? options.formats.map((f) => nativeToZxingFormat[f])
: Object.values(nativeToZxingFormat),
],
]);
this.reader = new BrowserMultiFormatReader(hints);
}
static getSupportedFormats() {
return Promise.resolve(Object.values(zxingToNativeFormat));
}
async detect(imageSource) {
try {
let result;
if (imageSource instanceof HTMLVideoElement || imageSource instanceof HTMLImageElement) {
result = await this.reader.scanOneResult(imageSource, false);
}
else if (imageSource instanceof HTMLCanvasElement) {
result = this.reader.decodeFromCanvas(imageSource);
}
else if (imageSource instanceof Blob) {
const image = await this.blobToImage(imageSource);
result = await this.reader.scanOneResult(image, false);
}
else if (imageSource instanceof ImageBitmap
|| imageSource instanceof ImageData
|| imageSource instanceof VideoFrame) {
result = this.reader.decodeFromCanvas(this.imageDataSourceToCanvas(imageSource));
}
else {
throw new TypeError('Image source is not supported');
}
return [{
rawValue: result.getText(),
format: zxingToNativeFormat[result.getBarcodeFormat()],
boundingBox: new DOMRectReadOnly(), // TODO: think of a way to map this in a meaningful way
cornerPoints: result.getResultPoints().map((p) => ({ x: p.getX(), y: p.getY() })),
}];
}
catch (err) {
if (err && !(err instanceof NotFoundException)) {
throw err;
}
return [];
}
}
async blobToImage(blob) {
return new Promise((resolve, reject) => {
const imageObjUrl = URL.createObjectURL(blob);
const image = new Image();
image.onload = () => {
URL.revokeObjectURL(imageObjUrl);
resolve(image);
};
image.onerror = () => reject();
image.src = imageObjUrl;
});
}
imageDataSourceToCanvas(source) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const width = source instanceof VideoFrame ? source.displayWidth : source.width;
const height = source instanceof VideoFrame ? source.displayHeight : source.height;
if (ctx) {
if (source instanceof ImageData) {
ctx.putImageData(source, width, height);
}
else {
ctx.drawImage(source, width, height);
}
}
return canvas;
}
}
//# sourceMappingURL=BarcodeDetector.js.map
@@ -0,0 +1 @@
{"version":3,"file":"BarcodeDetector.js","sourceRoot":"","sources":["../../src/BarcodeDetector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAU,MAAM,gBAAgB,CAAC;AAE3E,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEvE,MAAM,OAAO,eAAe;IAClB,MAAM,CAA2B;IAEzC,YAAY,OAA+B;QACzC,MAAM,KAAK,GAAG,IAAI,GAAG,CAA0B;YAC7C,CAAC,cAAc,CAAC,UAAU,EAAE,IAAI,CAAC;YACjC;gBACE,cAAc,CAAC,gBAAgB;gBAC/B,OAAO;oBACL,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC;oBACpD,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC;aACvC;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,GAAG,IAAI,wBAAwB,CAAC,KAAK,CAAC,CAAC;I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,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC;QAEnF,IAAI,GAAG,EAAE,CAAC;YACR,IAAI,MAAM,YAAY,SAAS,EAAE,CAAC;gBAChC,GAAG,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;YAC1C,CAAC;iBAAM,CAAC;gBACN,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;YACvC,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
+1
View File
@@ -0,0 +1 @@
export {};
+5
View File
@@ -0,0 +1,5 @@
import { BarcodeDetector } from './BarcodeDetector';
if (!('BarcodeDetector' in window)) {
window.BarcodeDetector = BarcodeDetector;
}
//# sourceMappingURL=browser.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/browser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,IAAI,CAAC,CAAC,iBAAiB,IAAI,MAAM,CAAC,EAAE,CAAC;IAClC,MAAoC,CAAC,eAAe,GAAG,eAAe,CAAC;AAC1E,CAAC"}
+3
View File
@@ -0,0 +1,3 @@
import { BarcodeFormat } from '@zxing/library';
export declare const zxingToNativeFormat: Record<BarcodeFormat, string>;
export declare const nativeToZxingFormat: Record<string, BarcodeFormat>;
+37
View File
@@ -0,0 +1,37 @@
import { BarcodeFormat } from '@zxing/library';
// format names taken from https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API#supported_barcode_formats
export const zxingToNativeFormat = {
[BarcodeFormat.AZTEC]: 'aztec',
[BarcodeFormat.CODABAR]: 'codabar',
[BarcodeFormat.CODE_39]: 'code_39',
[BarcodeFormat.CODE_93]: 'code_93',
[BarcodeFormat.CODE_128]: 'code_128',
[BarcodeFormat.DATA_MATRIX]: 'data_matrix',
[BarcodeFormat.EAN_8]: 'ean_8',
[BarcodeFormat.EAN_13]: 'ean_13',
[BarcodeFormat.ITF]: 'itf',
[BarcodeFormat.PDF_417]: 'pdf417',
[BarcodeFormat.QR_CODE]: 'qr_code',
[BarcodeFormat.UPC_A]: 'upc_a',
[BarcodeFormat.UPC_E]: 'upc_e',
[BarcodeFormat.UPC_EAN_EXTENSION]: 'unknown',
[BarcodeFormat.MAXICODE]: 'unknown',
[BarcodeFormat.RSS_14]: 'unknown',
[BarcodeFormat.RSS_EXPANDED]: 'unknown',
};
export const nativeToZxingFormat = {
aztec: BarcodeFormat.AZTEC,
codabar: BarcodeFormat.CODABAR,
code_39: BarcodeFormat.CODE_39,
code_93: BarcodeFormat.CODE_93,
code_128: BarcodeFormat.CODE_128,
data_matrix: BarcodeFormat.DATA_MATRIX,
ean_8: BarcodeFormat.EAN_8,
ean_13: BarcodeFormat.EAN_13,
itf: BarcodeFormat.ITF,
pdf417: BarcodeFormat.PDF_417,
qr_code: BarcodeFormat.QR_CODE,
upc_a: BarcodeFormat.UPC_A,
upc_e: BarcodeFormat.UPC_E,
};
//# sourceMappingURL=constants.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE/C,2HAA2H;AAE3H,MAAM,CAAC,MAAM,mBAAmB,GAAkC;IAChE,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,OAAO;IAC9B,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,SAAS;IAClC,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,SAAS;IAClC,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,SAAS;IAClC,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,UAAU;IACpC,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE,aAAa;IAC1C,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,OAAO;IAC9B,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,QAAQ;IAChC,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE,KAAK;IAC1B,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,QAAQ;IACjC,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,SAAS;IAClC,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,OAAO;IAC9B,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,OAAO;IAC9B,CAAC,aAAa,CAAC,iBAAiB,CAAC,EAAE,SAAS;IAC5C,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,SAAS;IACnC,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,SAAS;IACjC,CAAC,aAAa,CAAC,YAAY,CAAC,EAAE,SAAS;CACxC,CAAC;AAEF,MAAM,CAAC,MAAM,mBAAmB,GAAkC;IAChE,KAAK,EAAE,aAAa,CAAC,KAAK;IAC1B,OAAO,EAAE,aAAa,CAAC,OAAO;IAC9B,OAAO,EAAE,aAAa,CAAC,OAAO;IAC9B,OAAO,EAAE,aAAa,CAAC,OAAO;IAC9B,QAAQ,EAAE,aAAa,CAAC,QAAQ;IAChC,WAAW,EAAE,aAAa,CAAC,WAAW;IACtC,KAAK,EAAE,aAAa,CAAC,KAAK;IAC1B,MAAM,EAAE,aAAa,CAAC,MAAM;IAC5B,GAAG,EAAE,aAAa,CAAC,GAAG;IACtB,MAAM,EAAE,aAAa,CAAC,OAAO;IAC7B,OAAO,EAAE,aAAa,CAAC,OAAO;IAC9B,KAAK,EAAE,aAAa,CAAC,KAAK;IAC1B,KAAK,EAAE,aAAa,CAAC,KAAK;CAC3B,CAAC"}
+4
View File
@@ -0,0 +1,4 @@
import { BarcodeDetector } from './BarcodeDetector';
export type * from './models';
export { BarcodeDetector };
export default BarcodeDetector;
+4
View File
@@ -0,0 +1,4 @@
import { BarcodeDetector } from './BarcodeDetector';
export { BarcodeDetector };
export default BarcodeDetector;
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGpD,OAAO,EAAE,eAAe,EAAE,CAAC;AAC3B,eAAe,eAAe,CAAC"}
@@ -0,0 +1,4 @@
import { IDetectedBarcode } from './IDetectedBarcode';
export interface IBarcodeDetector {
detect(image: ImageBitmapSource): Promise<IDetectedBarcode[]>;
}
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=IBarcodeDetector.js.map
@@ -0,0 +1 @@
{"version":3,"file":"IBarcodeDetector.js","sourceRoot":"","sources":["../../../src/models/IBarcodeDetector.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
export interface ICornerPoint {
x: number;
y: number;
}
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=ICornerPoint.js.map
@@ -0,0 +1 @@
{"version":3,"file":"ICornerPoint.js","sourceRoot":"","sources":["../../../src/models/ICornerPoint.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
import { ICornerPoint } from './ICornerPoint';
export interface IDetectedBarcode {
boundingBox: DOMRectReadOnly;
cornerPoints: ICornerPoint[];
format: string;
rawValue: string;
}
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=IDetectedBarcode.js.map
@@ -0,0 +1 @@
{"version":3,"file":"IDetectedBarcode.js","sourceRoot":"","sources":["../../../src/models/IDetectedBarcode.ts"],"names":[],"mappings":""}
@@ -0,0 +1,8 @@
import { IBarcodeDetector } from './IBarcodeDetector';
export type WindowWithBarcodeDetector = Window & typeof globalThis & {
BarcodeDetector: {
prototype: IBarcodeDetector;
new (): IBarcodeDetector;
getSupportedFormats(): Promise<string[]>;
};
};
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=WindowWithBarcodeDetector.js.map
@@ -0,0 +1 @@
{"version":3,"file":"WindowWithBarcodeDetector.js","sourceRoot":"","sources":["../../../src/models/WindowWithBarcodeDetector.ts"],"names":[],"mappings":""}
+4
View File
@@ -0,0 +1,4 @@
export * from './ICornerPoint';
export * from './IDetectedBarcode';
export * from './IBarcodeDetector';
export * from './WindowWithBarcodeDetector';
+5
View File
@@ -0,0 +1,5 @@
export * from './ICornerPoint';
export * from './IDetectedBarcode';
export * from './IBarcodeDetector';
export * from './WindowWithBarcodeDetector';
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/models/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,6BAA6B,CAAC"}