barcode-detector-api-polyfill 1.0.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/LICENSE +21 -0
- package/README.md +61 -0
- package/browser/barcode-detector-polyfill.min.js +21 -0
- package/browser/barcode-detector-polyfill.min.js.map +7 -0
- package/cjs/BarcodeDetector.d.ts +13 -0
- package/cjs/BarcodeDetector.js +112 -0
- package/cjs/BarcodeDetector.js.map +1 -0
- package/cjs/browser.d.ts +1 -0
- package/cjs/browser.js +7 -0
- package/cjs/browser.js.map +1 -0
- package/cjs/index.d.ts +4 -0
- package/cjs/index.js +7 -0
- package/cjs/index.js.map +1 -0
- package/cjs/models/IBarcodeDetector.d.ts +4 -0
- package/cjs/models/IBarcodeDetector.js +3 -0
- package/cjs/models/IBarcodeDetector.js.map +1 -0
- package/cjs/models/ICornerPoint.d.ts +4 -0
- package/cjs/models/ICornerPoint.js +3 -0
- package/cjs/models/ICornerPoint.js.map +1 -0
- package/cjs/models/IDetectedBarcode.d.ts +7 -0
- package/cjs/models/IDetectedBarcode.js +3 -0
- package/cjs/models/IDetectedBarcode.js.map +1 -0
- package/cjs/models/WindowWithBarcodeDetector.d.ts +8 -0
- package/cjs/models/WindowWithBarcodeDetector.js +3 -0
- package/cjs/models/WindowWithBarcodeDetector.js.map +1 -0
- package/cjs/models/index.d.ts +4 -0
- package/cjs/models/index.js +21 -0
- package/cjs/models/index.js.map +1 -0
- package/es2022/BarcodeDetector.d.ts +13 -0
- package/es2022/BarcodeDetector.js +109 -0
- package/es2022/BarcodeDetector.js.map +1 -0
- package/es2022/browser.d.ts +1 -0
- package/es2022/browser.js +5 -0
- package/es2022/browser.js.map +1 -0
- package/es2022/index.d.ts +4 -0
- package/es2022/index.js +4 -0
- package/es2022/index.js.map +1 -0
- package/es2022/models/IBarcodeDetector.d.ts +4 -0
- package/es2022/models/IBarcodeDetector.js +2 -0
- package/es2022/models/IBarcodeDetector.js.map +1 -0
- package/es2022/models/ICornerPoint.d.ts +4 -0
- package/es2022/models/ICornerPoint.js +2 -0
- package/es2022/models/ICornerPoint.js.map +1 -0
- package/es2022/models/IDetectedBarcode.d.ts +7 -0
- package/es2022/models/IDetectedBarcode.js +2 -0
- package/es2022/models/IDetectedBarcode.js.map +1 -0
- package/es2022/models/WindowWithBarcodeDetector.d.ts +8 -0
- package/es2022/models/WindowWithBarcodeDetector.js +2 -0
- package/es2022/models/WindowWithBarcodeDetector.js.map +1 -0
- package/es2022/models/index.d.ts +4 -0
- package/es2022/models/index.js +5 -0
- package/es2022/models/index.js.map +1 -0
- package/package.json +47 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { IDetectedBarcode, IBarcodeDetector } from './models';
|
|
2
|
+
export declare class BarcodeDetector implements IBarcodeDetector {
|
|
3
|
+
private reader;
|
|
4
|
+
private static zxingToNativeFormat;
|
|
5
|
+
private static nativeToZxingFormat;
|
|
6
|
+
constructor(options?: {
|
|
7
|
+
formats: string[];
|
|
8
|
+
});
|
|
9
|
+
static getSupportedFormats(): Promise<string[]>;
|
|
10
|
+
detect(imageSource: ImageBitmapSource): Promise<IDetectedBarcode[]>;
|
|
11
|
+
private blobToImage;
|
|
12
|
+
private imageBitmapToCanvas;
|
|
13
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BarcodeDetector = void 0;
|
|
4
|
+
const browser_1 = require("@zxing/browser");
|
|
5
|
+
const library_1 = require("@zxing/library");
|
|
6
|
+
class BarcodeDetector {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
const hints = new Map([
|
|
9
|
+
[library_1.DecodeHintType.TRY_HARDER, true],
|
|
10
|
+
[
|
|
11
|
+
library_1.DecodeHintType.POSSIBLE_FORMATS,
|
|
12
|
+
options
|
|
13
|
+
? options.formats.map((f) => BarcodeDetector.nativeToZxingFormat[f])
|
|
14
|
+
: Object.values(BarcodeDetector.nativeToZxingFormat),
|
|
15
|
+
],
|
|
16
|
+
]);
|
|
17
|
+
this.reader = new browser_1.BrowserMultiFormatReader(hints);
|
|
18
|
+
}
|
|
19
|
+
static getSupportedFormats() {
|
|
20
|
+
return Promise.resolve(Object.values(BarcodeDetector.zxingToNativeFormat));
|
|
21
|
+
}
|
|
22
|
+
async detect(imageSource) {
|
|
23
|
+
try {
|
|
24
|
+
let result;
|
|
25
|
+
if (imageSource instanceof HTMLVideoElement || imageSource instanceof HTMLImageElement) {
|
|
26
|
+
result = await this.reader.scanOneResult(imageSource, false);
|
|
27
|
+
}
|
|
28
|
+
else if (imageSource instanceof HTMLCanvasElement) {
|
|
29
|
+
result = this.reader.decodeFromCanvas(imageSource);
|
|
30
|
+
}
|
|
31
|
+
else if (imageSource instanceof Blob) {
|
|
32
|
+
const image = await this.blobToImage(imageSource);
|
|
33
|
+
result = await this.reader.scanOneResult(image, false);
|
|
34
|
+
}
|
|
35
|
+
else if (imageSource instanceof ImageBitmap) {
|
|
36
|
+
result = this.reader.decodeFromCanvas(this.imageBitmapToCanvas(imageSource));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
throw new TypeError('Image source is not supported');
|
|
40
|
+
}
|
|
41
|
+
return [{
|
|
42
|
+
rawValue: result.getText(),
|
|
43
|
+
format: BarcodeDetector.zxingToNativeFormat[result.getBarcodeFormat()],
|
|
44
|
+
boundingBox: new DOMRectReadOnly(),
|
|
45
|
+
cornerPoints: result.getResultPoints().map((p) => ({ x: p.getX(), y: p.getY() })),
|
|
46
|
+
}];
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
if (err && !(err instanceof library_1.NotFoundException)) {
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async blobToImage(blob) {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
const imageObjUrl = URL.createObjectURL(blob);
|
|
58
|
+
const image = new Image();
|
|
59
|
+
image.onload = () => {
|
|
60
|
+
URL.revokeObjectURL(imageObjUrl);
|
|
61
|
+
resolve(image);
|
|
62
|
+
};
|
|
63
|
+
image.onerror = () => reject();
|
|
64
|
+
image.src = imageObjUrl;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
imageBitmapToCanvas(imageBitmap) {
|
|
68
|
+
const canvas = document.createElement('canvas');
|
|
69
|
+
const ctx = canvas.getContext('2d');
|
|
70
|
+
if (ctx) {
|
|
71
|
+
ctx.drawImage(imageBitmap, imageBitmap.width, imageBitmap.height);
|
|
72
|
+
}
|
|
73
|
+
return canvas;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.BarcodeDetector = BarcodeDetector;
|
|
77
|
+
// format names taken from https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API#supported_barcode_formats
|
|
78
|
+
BarcodeDetector.zxingToNativeFormat = {
|
|
79
|
+
[browser_1.BarcodeFormat.AZTEC]: 'aztec',
|
|
80
|
+
[browser_1.BarcodeFormat.CODABAR]: 'codabar',
|
|
81
|
+
[browser_1.BarcodeFormat.CODE_39]: 'code_39',
|
|
82
|
+
[browser_1.BarcodeFormat.CODE_93]: 'code_93',
|
|
83
|
+
[browser_1.BarcodeFormat.CODE_128]: 'code_128',
|
|
84
|
+
[browser_1.BarcodeFormat.DATA_MATRIX]: 'data_matrix',
|
|
85
|
+
[browser_1.BarcodeFormat.EAN_8]: 'ean_8',
|
|
86
|
+
[browser_1.BarcodeFormat.EAN_13]: 'ean_13',
|
|
87
|
+
[browser_1.BarcodeFormat.ITF]: 'itf',
|
|
88
|
+
[browser_1.BarcodeFormat.PDF_417]: 'pdf417',
|
|
89
|
+
[browser_1.BarcodeFormat.QR_CODE]: 'qr_code',
|
|
90
|
+
[browser_1.BarcodeFormat.UPC_A]: 'upc_a',
|
|
91
|
+
[browser_1.BarcodeFormat.UPC_E]: 'upc_e',
|
|
92
|
+
[browser_1.BarcodeFormat.UPC_EAN_EXTENSION]: 'unknown',
|
|
93
|
+
[browser_1.BarcodeFormat.MAXICODE]: 'unknown',
|
|
94
|
+
[browser_1.BarcodeFormat.RSS_14]: 'unknown',
|
|
95
|
+
[browser_1.BarcodeFormat.RSS_EXPANDED]: 'unknown',
|
|
96
|
+
};
|
|
97
|
+
BarcodeDetector.nativeToZxingFormat = {
|
|
98
|
+
aztec: browser_1.BarcodeFormat.AZTEC,
|
|
99
|
+
codabar: browser_1.BarcodeFormat.CODABAR,
|
|
100
|
+
code_39: browser_1.BarcodeFormat.CODE_39,
|
|
101
|
+
code_93: browser_1.BarcodeFormat.CODE_93,
|
|
102
|
+
code_128: browser_1.BarcodeFormat.CODE_128,
|
|
103
|
+
data_matrix: browser_1.BarcodeFormat.DATA_MATRIX,
|
|
104
|
+
ean_8: browser_1.BarcodeFormat.EAN_8,
|
|
105
|
+
ean_13: browser_1.BarcodeFormat.EAN_13,
|
|
106
|
+
itf: browser_1.BarcodeFormat.ITF,
|
|
107
|
+
pdf417: browser_1.BarcodeFormat.PDF_417,
|
|
108
|
+
qr_code: browser_1.BarcodeFormat.QR_CODE,
|
|
109
|
+
upc_a: browser_1.BarcodeFormat.UPC_A,
|
|
110
|
+
upc_e: browser_1.BarcodeFormat.UPC_E,
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=BarcodeDetector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BarcodeDetector.js","sourceRoot":"","sources":["../../src/BarcodeDetector.ts"],"names":[],"mappings":";;;AAAA,4CAAyE;AACzE,4CAA2E;AAG3E,MAAa,eAAe;IAwC1B,YAAY,OAA+B;QACzC,MAAM,KAAK,GAAG,IAAI,GAAG,CAA0B;YAC7C,CAAC,wBAAc,CAAC,UAAU,EAAE,IAAI,CAAC;YACjC;gBACE,wBAAc,CAAC,gBAAgB;gBAC/B,OAAO;oBACL,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,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AAQ;IAChC,WAAW,EAAE,uBAAa,CAAC,WAAW;IACtC,KAAK,EAAE,uBAAa,CAAC,KAAK;IAC1B,MAAM,EAAE,uBAAa,CAAC,MAAM;IAC5B,GAAG,EAAE,uBAAa,CAAC,GAAG;IACtB,MAAM,EAAE,uBAAa,CAAC,OAAO;IAC7B,OAAO,EAAE,uBAAa,CAAC,OAAO;IAC9B,KAAK,EAAE,uBAAa,CAAC,KAAK;IAC1B,KAAK,EAAE,uBAAa,CAAC,KAAK;CAC3B,CAAC"}
|
package/cjs/browser.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/cjs/browser.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const BarcodeDetector_1 = require("./BarcodeDetector");
|
|
4
|
+
if (!('BarcodeDetector' in window)) {
|
|
5
|
+
window.BarcodeDetector = BarcodeDetector_1.BarcodeDetector;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/browser.ts"],"names":[],"mappings":";;AACA,uDAAoD;AAEpD,IAAI,CAAC,CAAC,iBAAiB,IAAI,MAAM,CAAC,EAAE;IACjC,MAAoC,CAAC,eAAe,GAAG,iCAAe,CAAC;CACzE"}
|
package/cjs/index.d.ts
ADDED
package/cjs/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BarcodeDetector = void 0;
|
|
4
|
+
const BarcodeDetector_1 = require("./BarcodeDetector");
|
|
5
|
+
Object.defineProperty(exports, "BarcodeDetector", { enumerable: true, get: function () { return BarcodeDetector_1.BarcodeDetector; } });
|
|
6
|
+
exports.default = BarcodeDetector_1.BarcodeDetector;
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAoD;AAG3C,gGAHA,iCAAe,OAGA;AACxB,kBAAe,iCAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IBarcodeDetector.js","sourceRoot":"","sources":["../../../src/models/IBarcodeDetector.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ICornerPoint.js","sourceRoot":"","sources":["../../../src/models/ICornerPoint.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IDetectedBarcode.js","sourceRoot":"","sources":["../../../src/models/IDetectedBarcode.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WindowWithBarcodeDetector.js","sourceRoot":"","sources":["../../../src/models/WindowWithBarcodeDetector.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./ICornerPoint"), exports);
|
|
18
|
+
__exportStar(require("./IDetectedBarcode"), exports);
|
|
19
|
+
__exportStar(require("./IBarcodeDetector"), exports);
|
|
20
|
+
__exportStar(require("./WindowWithBarcodeDetector"), exports);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/models/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,qDAAmC;AACnC,qDAAmC;AACnC,8DAA4C"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { IDetectedBarcode, IBarcodeDetector } from './models';
|
|
2
|
+
export declare class BarcodeDetector implements IBarcodeDetector {
|
|
3
|
+
private reader;
|
|
4
|
+
private static zxingToNativeFormat;
|
|
5
|
+
private static nativeToZxingFormat;
|
|
6
|
+
constructor(options?: {
|
|
7
|
+
formats: string[];
|
|
8
|
+
});
|
|
9
|
+
static getSupportedFormats(): Promise<string[]>;
|
|
10
|
+
detect(imageSource: ImageBitmapSource): Promise<IDetectedBarcode[]>;
|
|
11
|
+
private blobToImage;
|
|
12
|
+
private imageBitmapToCanvas;
|
|
13
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { BarcodeFormat, BrowserMultiFormatReader } from '@zxing/browser';
|
|
2
|
+
import { DecodeHintType, NotFoundException } from '@zxing/library';
|
|
3
|
+
export class BarcodeDetector {
|
|
4
|
+
reader;
|
|
5
|
+
// format names taken from https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API#supported_barcode_formats
|
|
6
|
+
static zxingToNativeFormat = {
|
|
7
|
+
[BarcodeFormat.AZTEC]: 'aztec',
|
|
8
|
+
[BarcodeFormat.CODABAR]: 'codabar',
|
|
9
|
+
[BarcodeFormat.CODE_39]: 'code_39',
|
|
10
|
+
[BarcodeFormat.CODE_93]: 'code_93',
|
|
11
|
+
[BarcodeFormat.CODE_128]: 'code_128',
|
|
12
|
+
[BarcodeFormat.DATA_MATRIX]: 'data_matrix',
|
|
13
|
+
[BarcodeFormat.EAN_8]: 'ean_8',
|
|
14
|
+
[BarcodeFormat.EAN_13]: 'ean_13',
|
|
15
|
+
[BarcodeFormat.ITF]: 'itf',
|
|
16
|
+
[BarcodeFormat.PDF_417]: 'pdf417',
|
|
17
|
+
[BarcodeFormat.QR_CODE]: 'qr_code',
|
|
18
|
+
[BarcodeFormat.UPC_A]: 'upc_a',
|
|
19
|
+
[BarcodeFormat.UPC_E]: 'upc_e',
|
|
20
|
+
[BarcodeFormat.UPC_EAN_EXTENSION]: 'unknown',
|
|
21
|
+
[BarcodeFormat.MAXICODE]: 'unknown',
|
|
22
|
+
[BarcodeFormat.RSS_14]: 'unknown',
|
|
23
|
+
[BarcodeFormat.RSS_EXPANDED]: 'unknown',
|
|
24
|
+
};
|
|
25
|
+
static nativeToZxingFormat = {
|
|
26
|
+
aztec: BarcodeFormat.AZTEC,
|
|
27
|
+
codabar: BarcodeFormat.CODABAR,
|
|
28
|
+
code_39: BarcodeFormat.CODE_39,
|
|
29
|
+
code_93: BarcodeFormat.CODE_93,
|
|
30
|
+
code_128: BarcodeFormat.CODE_128,
|
|
31
|
+
data_matrix: BarcodeFormat.DATA_MATRIX,
|
|
32
|
+
ean_8: BarcodeFormat.EAN_8,
|
|
33
|
+
ean_13: BarcodeFormat.EAN_13,
|
|
34
|
+
itf: BarcodeFormat.ITF,
|
|
35
|
+
pdf417: BarcodeFormat.PDF_417,
|
|
36
|
+
qr_code: BarcodeFormat.QR_CODE,
|
|
37
|
+
upc_a: BarcodeFormat.UPC_A,
|
|
38
|
+
upc_e: BarcodeFormat.UPC_E,
|
|
39
|
+
};
|
|
40
|
+
constructor(options) {
|
|
41
|
+
const hints = new Map([
|
|
42
|
+
[DecodeHintType.TRY_HARDER, true],
|
|
43
|
+
[
|
|
44
|
+
DecodeHintType.POSSIBLE_FORMATS,
|
|
45
|
+
options
|
|
46
|
+
? options.formats.map((f) => BarcodeDetector.nativeToZxingFormat[f])
|
|
47
|
+
: Object.values(BarcodeDetector.nativeToZxingFormat),
|
|
48
|
+
],
|
|
49
|
+
]);
|
|
50
|
+
this.reader = new BrowserMultiFormatReader(hints);
|
|
51
|
+
}
|
|
52
|
+
static getSupportedFormats() {
|
|
53
|
+
return Promise.resolve(Object.values(BarcodeDetector.zxingToNativeFormat));
|
|
54
|
+
}
|
|
55
|
+
async detect(imageSource) {
|
|
56
|
+
try {
|
|
57
|
+
let result;
|
|
58
|
+
if (imageSource instanceof HTMLVideoElement || imageSource instanceof HTMLImageElement) {
|
|
59
|
+
result = await this.reader.scanOneResult(imageSource, false);
|
|
60
|
+
}
|
|
61
|
+
else if (imageSource instanceof HTMLCanvasElement) {
|
|
62
|
+
result = this.reader.decodeFromCanvas(imageSource);
|
|
63
|
+
}
|
|
64
|
+
else if (imageSource instanceof Blob) {
|
|
65
|
+
const image = await this.blobToImage(imageSource);
|
|
66
|
+
result = await this.reader.scanOneResult(image, false);
|
|
67
|
+
}
|
|
68
|
+
else if (imageSource instanceof ImageBitmap) {
|
|
69
|
+
result = this.reader.decodeFromCanvas(this.imageBitmapToCanvas(imageSource));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
throw new TypeError('Image source is not supported');
|
|
73
|
+
}
|
|
74
|
+
return [{
|
|
75
|
+
rawValue: result.getText(),
|
|
76
|
+
format: BarcodeDetector.zxingToNativeFormat[result.getBarcodeFormat()],
|
|
77
|
+
boundingBox: new DOMRectReadOnly(),
|
|
78
|
+
cornerPoints: result.getResultPoints().map((p) => ({ x: p.getX(), y: p.getY() })),
|
|
79
|
+
}];
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
if (err && !(err instanceof NotFoundException)) {
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async blobToImage(blob) {
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
const imageObjUrl = URL.createObjectURL(blob);
|
|
91
|
+
const image = new Image();
|
|
92
|
+
image.onload = () => {
|
|
93
|
+
URL.revokeObjectURL(imageObjUrl);
|
|
94
|
+
resolve(image);
|
|
95
|
+
};
|
|
96
|
+
image.onerror = () => reject();
|
|
97
|
+
image.src = imageObjUrl;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
imageBitmapToCanvas(imageBitmap) {
|
|
101
|
+
const canvas = document.createElement('canvas');
|
|
102
|
+
const ctx = canvas.getContext('2d');
|
|
103
|
+
if (ctx) {
|
|
104
|
+
ctx.drawImage(imageBitmap, imageBitmap.width, imageBitmap.height);
|
|
105
|
+
}
|
|
106
|
+
return canvas;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=BarcodeDetector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BarcodeDetector.js","sourceRoot":"","sources":["../../src/BarcodeDetector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AACzE,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAU,MAAM,gBAAgB,CAAC;AAG3E,MAAM,OAAO,eAAe;IAClB,MAAM,CAA2B;IAEzC,2HAA2H;IACnH,MAAM,CAAC,mBAAmB,GAAkC;QAClE,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,OAAO;QAC9B,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,SAAS;QAClC,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,SAAS;QAClC,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,SAAS;QAClC,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,UAAU;QACpC,CAAC,aAAa,CAAC,WAAW,CAAC,E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AC,IAAI,CAAC,CAAC;QAEpC,IAAI,GAAG,EAAE;YACP,GAAG,CAAC,SAAS,CAAC,WAAW,EAAE,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;SACnE;QAED,OAAO,MAAM,CAAC;IAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
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;IACjC,MAAoC,CAAC,eAAe,GAAG,eAAe,CAAC;CACzE"}
|
package/es2022/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
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 @@
|
|
|
1
|
+
{"version":3,"file":"IBarcodeDetector.js","sourceRoot":"","sources":["../../../src/models/IBarcodeDetector.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ICornerPoint.js","sourceRoot":"","sources":["../../../src/models/ICornerPoint.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IDetectedBarcode.js","sourceRoot":"","sources":["../../../src/models/IDetectedBarcode.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WindowWithBarcodeDetector.js","sourceRoot":"","sources":["../../../src/models/WindowWithBarcodeDetector.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "barcode-detector-api-polyfill",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "A polyfill for the BarcodeDetector API using the ZXing library",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"clean": "shx rm -rf dist",
|
|
8
|
+
"build": "npm run clean && tsc && npm run build:cjs && npm run build:browser && npm run build:copy",
|
|
9
|
+
"build:cjs": "tsc --build tsconfig.cjs.json",
|
|
10
|
+
"build:browser": "esbuild src/browser.ts --outfile=dist/browser/barcode-detector-polyfill.min.js --bundle --tree-shaking --target=firefox112,safari16 --minify --sourcemap",
|
|
11
|
+
"build:copy": "shx cp README.md dist && shx cp package.json dist && shx cp LICENSE dist",
|
|
12
|
+
"lint": "eslint src",
|
|
13
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"barcode",
|
|
17
|
+
"detector",
|
|
18
|
+
"reader",
|
|
19
|
+
"api",
|
|
20
|
+
"polyfill",
|
|
21
|
+
"browser"
|
|
22
|
+
],
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/StefanNedelchev/barcode-detector-polyfill.git"
|
|
26
|
+
},
|
|
27
|
+
"author": "Stefan Nedelchev",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"main": "./cjs/index.js",
|
|
30
|
+
"module": "./es2022/inedx.js",
|
|
31
|
+
"typings": "./es2022/index.d.ts",
|
|
32
|
+
"esnext": "./es2022/index.js",
|
|
33
|
+
"unpkg": "./browser/barcode-detector-polyfill.min.js",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@zxing/browser": "^0.1.4",
|
|
36
|
+
"@zxing/library": "^0.20.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^18.16.19",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "^5.61.0",
|
|
41
|
+
"@typescript-eslint/parser": "^5.61.0",
|
|
42
|
+
"esbuild": "^0.18.11",
|
|
43
|
+
"eslint": "^8.44.0",
|
|
44
|
+
"shx": "^0.3.4",
|
|
45
|
+
"typescript": "~5.1.6"
|
|
46
|
+
}
|
|
47
|
+
}
|