@gueshbzh-perso/scanner 0.11.0 → 0.11.1
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/README.md +6 -3
- package/dist/native.d.ts +21 -9
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +45 -22
- package/dist/native.js.map +1 -1
- package/dist/types.d.ts +14 -11
- package/dist/types.d.ts.map +1 -1
- package/dist/wasm/scanner_wasm_bg.wasm +0 -0
- package/package.json +1 -1
- package/src/native.ts +43 -20
- package/src/types.ts +14 -11
package/README.md
CHANGED
|
@@ -147,9 +147,12 @@ overlay (ROI mask, highlights, torch / camera buttons) · beep · vibration
|
|
|
147
147
|
strategies (laser engraving, shadows, light-on-dark codes…), a safety net
|
|
148
148
|
that costs a little CPU and battery. Off by default. Its codes get the
|
|
149
149
|
same multi-frame confirmation and de-duplication. It is used only when it
|
|
150
|
-
|
|
151
|
-
the
|
|
152
|
-
|
|
150
|
+
reads the formats it has; the ones it lacks (`micro_qr` always, others
|
|
151
|
+
depending on the browser) are read only with `engineFallback`. With the
|
|
152
|
+
default `formats` list they are accepted as unread; with an explicit list,
|
|
153
|
+
one of them keeps the engine (a requested format is never dropped
|
|
154
|
+
silently). UPC-A is covered through EAN-13 where the native decoder has no
|
|
155
|
+
`upc_a` (Safari). It is dropped for the engine alone if it ever fails.
|
|
153
156
|
Detection is by feature, not by browser: any browser that ships the API
|
|
154
157
|
gets it, but a JavaScript polyfill of `BarcodeDetector` is ignored (it
|
|
155
158
|
would only be a slower engine on the main thread). Safari, Firefox and
|
package/dist/native.d.ts
CHANGED
|
@@ -20,19 +20,31 @@ export interface BarcodeDetectorClass {
|
|
|
20
20
|
/** Native decoder serving one reader configuration. */
|
|
21
21
|
export declare class NativeDetector {
|
|
22
22
|
private detector;
|
|
23
|
-
/**
|
|
23
|
+
/** Requested formats the detector reads. */
|
|
24
24
|
readonly formats: readonly BarcodeFormat[];
|
|
25
|
+
/**
|
|
26
|
+
* Requested formats the native decoder cannot read. They are read only
|
|
27
|
+
* with `processing.engineFallback`.
|
|
28
|
+
*/
|
|
29
|
+
readonly missing: readonly BarcodeFormat[];
|
|
30
|
+
/** UPC-A codes come back as EAN-13 and are converted (see `create`). */
|
|
31
|
+
private upcFromEan;
|
|
25
32
|
private constructor();
|
|
26
33
|
/**
|
|
27
|
-
* A detector for `formats` (default: all), or `undefined` when the
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
34
|
+
* A detector for `formats` (default: all), or `undefined` when the engine
|
|
35
|
+
* must keep the job: API absent, no requested format readable natively, or
|
|
36
|
+
* an **explicit** list naming a format the native decoder lacks while
|
|
37
|
+
* `engineFallback` is off (a format the application asked for must not be
|
|
38
|
+
* dropped silently). With the default list, missing formats (always
|
|
39
|
+
* `micro_qr`, sometimes others) are accepted: the native decoder reads the
|
|
40
|
+
* rest, and the missing ones are read only with `engineFallback`. The
|
|
41
|
+
* engine fallback never turns itself on: it costs CPU and battery, that is
|
|
42
|
+
* the application's call.
|
|
31
43
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
44
|
+
* UPC-A is EAN-13 with a leading `0` (GTIN-12 inside GTIN-13), so a native
|
|
45
|
+
* decoder without `upc_a` but with `ean_13` covers it: it is asked for
|
|
46
|
+
* EAN-13 and those codes are reported as UPC-A, as this SDK's engine (and
|
|
47
|
+
* ZXing) do when UPC-A is requested.
|
|
36
48
|
*/
|
|
37
49
|
static create(formats: readonly BarcodeFormat[] | undefined, api?: BarcodeDetectorClass | undefined, engineFallback?: boolean): Promise<NativeDetector | undefined>;
|
|
38
50
|
/**
|
package/dist/native.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,aAAa,E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CAAC,MAAM,EAAE,CAAC,CAAC;CAC1C;AAwBD,uDAAuD;AACvD,qBAAa,cAAc;IAEvB,OAAO,CAAC,QAAQ;IAChB,4CAA4C;IAC5C,QAAQ,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE;IAC1C;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE;IAC1C,wEAAwE;IACxE,OAAO,CAAC,UAAU;IAVpB,OAAO;IAaP;;;;;;;;;;;;;;;OAeG;WACU,MAAM,CACjB,OAAO,EAAE,SAAS,aAAa,EAAE,GAAG,SAAS,EAC7C,GAAG,GAAE,oBAAoB,GAAG,SAAuB,EACnD,cAAc,UAAQ,GACrB,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC;IA2BtC;;;;OAIG;IACG,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;CAgB1E;AAiBD,2EAA2E;AAC3E,wBAAgB,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAE7C"}
|
package/dist/native.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* raw frame with every format); without it, see `NativeDetector.create`.
|
|
2
|
+
* Every format of this SDK. The native API spells them the same way
|
|
3
|
+
* (`qr_code`, `ean_13`…) but has no `micro_qr`, and a given browser may lack
|
|
4
|
+
* others (Safari, built on Apple's Vision framework, has no `upc_a`).
|
|
6
5
|
*/
|
|
7
|
-
const
|
|
6
|
+
const ALL_FORMATS = [
|
|
8
7
|
'qr_code',
|
|
8
|
+
'micro_qr',
|
|
9
9
|
'data_matrix',
|
|
10
10
|
'aztec',
|
|
11
11
|
'pdf417',
|
|
@@ -22,27 +22,39 @@ const NATIVE_FORMATS = [
|
|
|
22
22
|
/** Native decoder serving one reader configuration. */
|
|
23
23
|
export class NativeDetector {
|
|
24
24
|
constructor(detector,
|
|
25
|
-
/**
|
|
26
|
-
formats
|
|
25
|
+
/** Requested formats the detector reads. */
|
|
26
|
+
formats,
|
|
27
|
+
/**
|
|
28
|
+
* Requested formats the native decoder cannot read. They are read only
|
|
29
|
+
* with `processing.engineFallback`.
|
|
30
|
+
*/
|
|
31
|
+
missing,
|
|
32
|
+
/** UPC-A codes come back as EAN-13 and are converted (see `create`). */
|
|
33
|
+
upcFromEan) {
|
|
27
34
|
this.detector = detector;
|
|
28
35
|
this.formats = formats;
|
|
36
|
+
this.missing = missing;
|
|
37
|
+
this.upcFromEan = upcFromEan;
|
|
29
38
|
}
|
|
30
39
|
/**
|
|
31
|
-
* A detector for `formats` (default: all), or `undefined` when the
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
40
|
+
* A detector for `formats` (default: all), or `undefined` when the engine
|
|
41
|
+
* must keep the job: API absent, no requested format readable natively, or
|
|
42
|
+
* an **explicit** list naming a format the native decoder lacks while
|
|
43
|
+
* `engineFallback` is off (a format the application asked for must not be
|
|
44
|
+
* dropped silently). With the default list, missing formats (always
|
|
45
|
+
* `micro_qr`, sometimes others) are accepted: the native decoder reads the
|
|
46
|
+
* rest, and the missing ones are read only with `engineFallback`. The
|
|
47
|
+
* engine fallback never turns itself on: it costs CPU and battery, that is
|
|
48
|
+
* the application's call.
|
|
35
49
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
50
|
+
* UPC-A is EAN-13 with a leading `0` (GTIN-12 inside GTIN-13), so a native
|
|
51
|
+
* decoder without `upc_a` but with `ean_13` covers it: it is asked for
|
|
52
|
+
* EAN-13 and those codes are reported as UPC-A, as this SDK's engine (and
|
|
53
|
+
* ZXing) do when UPC-A is requested.
|
|
40
54
|
*/
|
|
41
55
|
static async create(formats, api = nativeApi(), engineFallback = false) {
|
|
42
56
|
if (!api)
|
|
43
57
|
return undefined;
|
|
44
|
-
if (!engineFallback && formats?.includes('micro_qr'))
|
|
45
|
-
return undefined;
|
|
46
58
|
let supported;
|
|
47
59
|
try {
|
|
48
60
|
supported = await api.getSupportedFormats();
|
|
@@ -50,14 +62,20 @@ export class NativeDetector {
|
|
|
50
62
|
catch {
|
|
51
63
|
return undefined;
|
|
52
64
|
}
|
|
65
|
+
const explicit = formats !== undefined && formats.length > 0;
|
|
66
|
+
const requested = explicit ? formats : ALL_FORMATS;
|
|
67
|
+
const upcFromEan = requested.includes('upc_a') && !supported.includes('upc_a') && supported.includes('ean_13');
|
|
68
|
+
const covered = (f) => supported.includes(f) || (f === 'upc_a' && upcFromEan);
|
|
69
|
+
const readable = requested.filter(covered);
|
|
70
|
+
const missing = requested.filter((f) => !covered(f));
|
|
53
71
|
// On Android the list is empty until Google Play Services has downloaded
|
|
54
72
|
// its barcode module (first use on the device, in the background): the
|
|
55
73
|
// detector is unavailable *for now*, the engine does the whole job.
|
|
56
|
-
|
|
57
|
-
if (wanted.length === 0 || !wanted.every((f) => supported.includes(f)))
|
|
74
|
+
if (readable.length === 0 || (explicit && missing.length > 0 && !engineFallback))
|
|
58
75
|
return undefined;
|
|
76
|
+
const native = readable.map((f) => (f === 'upc_a' && upcFromEan ? 'ean_13' : f));
|
|
59
77
|
try {
|
|
60
|
-
return new NativeDetector(new api({ formats: [...
|
|
78
|
+
return new NativeDetector(new api({ formats: [...new Set(native)] }), readable, missing, upcFromEan);
|
|
61
79
|
}
|
|
62
80
|
catch {
|
|
63
81
|
return undefined;
|
|
@@ -72,13 +90,18 @@ export class NativeDetector {
|
|
|
72
90
|
const found = await this.detector.detect(source);
|
|
73
91
|
const timestamp = Date.now();
|
|
74
92
|
return found.flatMap((code) => {
|
|
75
|
-
|
|
93
|
+
let format = code.format;
|
|
94
|
+
let text = code.rawValue;
|
|
95
|
+
if (this.upcFromEan && format === 'ean_13' && /^0\d{12}$/.test(text)) {
|
|
96
|
+
format = 'upc_a';
|
|
97
|
+
text = text.slice(1);
|
|
98
|
+
}
|
|
76
99
|
if (!this.formats.includes(format) || code.cornerPoints.length === 0)
|
|
77
100
|
return [];
|
|
78
101
|
if (!contains(rect, center(code.cornerPoints)))
|
|
79
102
|
return [];
|
|
80
103
|
const points = code.cornerPoints.map((p) => ({ x: p.x, y: p.y }));
|
|
81
|
-
return [{ text
|
|
104
|
+
return [{ text, format, points, timestamp }];
|
|
82
105
|
});
|
|
83
106
|
}
|
|
84
107
|
}
|
package/dist/native.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"native.js","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAgCA
|
|
1
|
+
{"version":3,"file":"native.js","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAgCA;;;;GAIG;AACH,MAAM,WAAW,GAA6B;IAC5C,SAAS;IACT,UAAU;IACV,aAAa;IACb,OAAO;IACP,QAAQ;IACR,UAAU;IACV,SAAS;IACT,SAAS;IACT,QAAQ;IACR,OAAO;IACP,OAAO;IACP,OAAO;IACP,KAAK;IACL,SAAS;CACV,CAAC;AAEF,uDAAuD;AACvD,MAAM,OAAO,cAAc;IACzB,YACU,QAAiC;IACzC,4CAA4C;IACnC,OAAiC;IAC1C;;;OAGG;IACM,OAAiC;IAC1C,wEAAwE;IAChE,UAAmB;QATnB,aAAQ,GAAR,QAAQ,CAAyB;QAEhC,YAAO,GAAP,OAAO,CAA0B;QAKjC,YAAO,GAAP,OAAO,CAA0B;QAElC,eAAU,GAAV,UAAU,CAAS;IAC1B,CAAC;IAEJ;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,CACjB,OAA6C,EAC7C,MAAwC,SAAS,EAAE,EACnD,cAAc,GAAG,KAAK;QAEtB,IAAI,CAAC,GAAG;YAAE,OAAO,SAAS,CAAC;QAC3B,IAAI,SAAmB,CAAC;QACxB,IAAI,CAAC;YACH,SAAS,GAAG,MAAM,GAAG,CAAC,mBAAmB,EAAE,CAAC;QAC9C,CAAC;QAAC,MA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essingOptions {
|
|
|
109
109
|
backend?: 'auto' | 'webgl' | 'canvas';
|
|
110
110
|
/**
|
|
111
111
|
* Use the browser's native `BarcodeDetector` (Chrome on Android, macOS and
|
|
112
|
-
* ChromeOS
|
|
113
|
-
*
|
|
114
|
-
* the
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
112
|
+
* ChromeOS; Safari when its Shape Detection API is enabled): it reads codes
|
|
113
|
+
* whatever their orientation, and the engine's workers stay idle. Codes get
|
|
114
|
+
* the same confirmation and de-duplication.
|
|
115
|
+
*
|
|
116
|
+
* Formats it lacks (`micro_qr` always, others depending on the browser)
|
|
117
|
+
* are read only with `engineFallback`. With the default `formats` list
|
|
118
|
+
* they are accepted as unread; with an explicit list, one of them keeps
|
|
119
|
+
* the engine instead (a requested format is never dropped silently).
|
|
120
|
+
* Elsewhere (Safari without the API, Firefox, Chrome on Windows / Linux),
|
|
121
|
+
* or if it fails, the engine does everything. `'off'`: always the engine
|
|
122
|
+
* alone. Default: `'auto'`.
|
|
119
123
|
*/
|
|
120
124
|
nativeDetector?: 'auto' | 'off';
|
|
121
125
|
/**
|
|
122
126
|
* With the native detector: while it finds nothing, also hand a frame to
|
|
123
127
|
* the engine's fallback strategies from time to time (~3 per second), a
|
|
124
|
-
* safety net for codes
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* instead of the native detector. Default: `false`.
|
|
128
|
+
* safety net for codes it misses (laser engraving, shadows, light-on-dark
|
|
129
|
+
* codes) and for the formats it lacks. Costs a little CPU and battery, so
|
|
130
|
+
* it is never turned on implicitly. Default: `false`.
|
|
128
131
|
*/
|
|
129
132
|
engineFallback?: boolean;
|
|
130
133
|
}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,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IACnC,UAAU,CAAC,EAAE,iBAAiB,CAAC;CAChC;AAED,2CAA2C;AAC3C,MAAM,WAAW,WAAW;IAC1B,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,iGAAiG;IACjG,QAAQ,EAAE,MAAM,CAAC;IACjB,0GAA0G;IAC1G,MAAM,EAAE,MAAM,CAAC;IACf,+CAA+C;IAC/C,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,6BAA6B;IAC7B,OAAO,EAAE,OAAO,GAAG,QAAQ,CAAC;IAC5B,gDAAgD;IAChD,MAAM,EAAE,OAAO,CAAC;IAChB,8EAA8E;IAC9E,OAAO,EAAE,MAAM,CAAC;IAChB,qGAAqG;IACrG,MAAM,EAAE,OAAO,CAAC;IAChB,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC3B,yCAAyC;IACzC,QAAQ,EAAE,SAAS,EAAE,CAAC;IACtB,KAAK,EAAE,KAAK,CAAC;IACb,OAAO,EAAE,SAAS,CAAC;IACnB,OAAO,EAAE,SAAS,CAAC;CACpB"}
|
|
Binary file
|
package/package.json
CHANGED
package/src/native.ts
CHANGED
|
@@ -31,13 +31,13 @@ export interface BarcodeDetectorClass {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* raw frame with every format); without it, see `NativeDetector.create`.
|
|
34
|
+
* Every format of this SDK. The native API spells them the same way
|
|
35
|
+
* (`qr_code`, `ean_13`…) but has no `micro_qr`, and a given browser may lack
|
|
36
|
+
* others (Safari, built on Apple's Vision framework, has no `upc_a`).
|
|
38
37
|
*/
|
|
39
|
-
const
|
|
38
|
+
const ALL_FORMATS: readonly BarcodeFormat[] = [
|
|
40
39
|
'qr_code',
|
|
40
|
+
'micro_qr',
|
|
41
41
|
'data_matrix',
|
|
42
42
|
'aztec',
|
|
43
43
|
'pdf417',
|
|
@@ -56,20 +56,32 @@ const NATIVE_FORMATS: readonly BarcodeFormat[] = [
|
|
|
56
56
|
export class NativeDetector {
|
|
57
57
|
private constructor(
|
|
58
58
|
private detector: BarcodeDetectorInstance,
|
|
59
|
-
/**
|
|
59
|
+
/** Requested formats the detector reads. */
|
|
60
60
|
readonly formats: readonly BarcodeFormat[],
|
|
61
|
+
/**
|
|
62
|
+
* Requested formats the native decoder cannot read. They are read only
|
|
63
|
+
* with `processing.engineFallback`.
|
|
64
|
+
*/
|
|
65
|
+
readonly missing: readonly BarcodeFormat[],
|
|
66
|
+
/** UPC-A codes come back as EAN-13 and are converted (see `create`). */
|
|
67
|
+
private upcFromEan: boolean,
|
|
61
68
|
) {}
|
|
62
69
|
|
|
63
70
|
/**
|
|
64
|
-
* A detector for `formats` (default: all), or `undefined` when the
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
71
|
+
* A detector for `formats` (default: all), or `undefined` when the engine
|
|
72
|
+
* must keep the job: API absent, no requested format readable natively, or
|
|
73
|
+
* an **explicit** list naming a format the native decoder lacks while
|
|
74
|
+
* `engineFallback` is off (a format the application asked for must not be
|
|
75
|
+
* dropped silently). With the default list, missing formats (always
|
|
76
|
+
* `micro_qr`, sometimes others) are accepted: the native decoder reads the
|
|
77
|
+
* rest, and the missing ones are read only with `engineFallback`. The
|
|
78
|
+
* engine fallback never turns itself on: it costs CPU and battery, that is
|
|
79
|
+
* the application's call.
|
|
68
80
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
81
|
+
* UPC-A is EAN-13 with a leading `0` (GTIN-12 inside GTIN-13), so a native
|
|
82
|
+
* decoder without `upc_a` but with `ean_13` covers it: it is asked for
|
|
83
|
+
* EAN-13 and those codes are reported as UPC-A, as this SDK's engine (and
|
|
84
|
+
* ZXing) do when UPC-A is requested.
|
|
73
85
|
*/
|
|
74
86
|
static async create(
|
|
75
87
|
formats: readonly BarcodeFormat[] | undefined,
|
|
@@ -77,20 +89,26 @@ export class NativeDetector {
|
|
|
77
89
|
engineFallback = false,
|
|
78
90
|
): Promise<NativeDetector | undefined> {
|
|
79
91
|
if (!api) return undefined;
|
|
80
|
-
if (!engineFallback && formats?.includes('micro_qr')) return undefined;
|
|
81
92
|
let supported: string[];
|
|
82
93
|
try {
|
|
83
94
|
supported = await api.getSupportedFormats();
|
|
84
95
|
} catch {
|
|
85
96
|
return undefined;
|
|
86
97
|
}
|
|
98
|
+
const explicit = formats !== undefined && formats.length > 0;
|
|
99
|
+
const requested = explicit ? formats : ALL_FORMATS;
|
|
100
|
+
const upcFromEan =
|
|
101
|
+
requested.includes('upc_a') && !supported.includes('upc_a') && supported.includes('ean_13');
|
|
102
|
+
const covered = (f: BarcodeFormat) => supported.includes(f) || (f === 'upc_a' && upcFromEan);
|
|
103
|
+
const readable = requested.filter(covered);
|
|
104
|
+
const missing = requested.filter((f) => !covered(f));
|
|
87
105
|
// On Android the list is empty until Google Play Services has downloaded
|
|
88
106
|
// its barcode module (first use on the device, in the background): the
|
|
89
107
|
// detector is unavailable *for now*, the engine does the whole job.
|
|
90
|
-
|
|
91
|
-
|
|
108
|
+
if (readable.length === 0 || (explicit && missing.length > 0 && !engineFallback)) return undefined;
|
|
109
|
+
const native = readable.map((f) => (f === 'upc_a' && upcFromEan ? 'ean_13' : f));
|
|
92
110
|
try {
|
|
93
|
-
return new NativeDetector(new api({ formats: [...
|
|
111
|
+
return new NativeDetector(new api({ formats: [...new Set(native)] }), readable, missing, upcFromEan);
|
|
94
112
|
} catch {
|
|
95
113
|
return undefined;
|
|
96
114
|
}
|
|
@@ -105,11 +123,16 @@ export class NativeDetector {
|
|
|
105
123
|
const found = await this.detector.detect(source);
|
|
106
124
|
const timestamp = Date.now();
|
|
107
125
|
return found.flatMap((code) => {
|
|
108
|
-
|
|
126
|
+
let format = code.format as BarcodeFormat;
|
|
127
|
+
let text = code.rawValue;
|
|
128
|
+
if (this.upcFromEan && format === 'ean_13' && /^0\d{12}$/.test(text)) {
|
|
129
|
+
format = 'upc_a';
|
|
130
|
+
text = text.slice(1);
|
|
131
|
+
}
|
|
109
132
|
if (!this.formats.includes(format) || code.cornerPoints.length === 0) return [];
|
|
110
133
|
if (!contains(rect, center(code.cornerPoints))) return [];
|
|
111
134
|
const points = code.cornerPoints.map((p) => ({ x: p.x, y: p.y }));
|
|
112
|
-
return [{ text
|
|
135
|
+
return [{ text, format, points, timestamp }];
|
|
113
136
|
});
|
|
114
137
|
}
|
|
115
138
|
}
|
package/src/types.ts
CHANGED
|
@@ -120,22 +120,25 @@ export interface ProcessingOptions {
|
|
|
120
120
|
backend?: 'auto' | 'webgl' | 'canvas';
|
|
121
121
|
/**
|
|
122
122
|
* Use the browser's native `BarcodeDetector` (Chrome on Android, macOS and
|
|
123
|
-
* ChromeOS
|
|
124
|
-
*
|
|
125
|
-
* the
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
123
|
+
* ChromeOS; Safari when its Shape Detection API is enabled): it reads codes
|
|
124
|
+
* whatever their orientation, and the engine's workers stay idle. Codes get
|
|
125
|
+
* the same confirmation and de-duplication.
|
|
126
|
+
*
|
|
127
|
+
* Formats it lacks (`micro_qr` always, others depending on the browser)
|
|
128
|
+
* are read only with `engineFallback`. With the default `formats` list
|
|
129
|
+
* they are accepted as unread; with an explicit list, one of them keeps
|
|
130
|
+
* the engine instead (a requested format is never dropped silently).
|
|
131
|
+
* Elsewhere (Safari without the API, Firefox, Chrome on Windows / Linux),
|
|
132
|
+
* or if it fails, the engine does everything. `'off'`: always the engine
|
|
133
|
+
* alone. Default: `'auto'`.
|
|
130
134
|
*/
|
|
131
135
|
nativeDetector?: 'auto' | 'off';
|
|
132
136
|
/**
|
|
133
137
|
* With the native detector: while it finds nothing, also hand a frame to
|
|
134
138
|
* the engine's fallback strategies from time to time (~3 per second), a
|
|
135
|
-
* safety net for codes
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* instead of the native detector. Default: `false`.
|
|
139
|
+
* safety net for codes it misses (laser engraving, shadows, light-on-dark
|
|
140
|
+
* codes) and for the formats it lacks. Costs a little CPU and battery, so
|
|
141
|
+
* it is never turned on implicitly. Default: `false`.
|
|
139
142
|
*/
|
|
140
143
|
engineFallback?: boolean;
|
|
141
144
|
}
|