@nddeps/barcode-scanner 0.3.1 → 0.4.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/dist/create-barcode-scanner.d.ts +39 -31
- package/dist/create-worker.d.ts +6 -0
- package/dist/index.js +277 -277
- package/dist/worker.js +2135 -1
- package/dist/{worker/worker.types.d.ts → worker.types.d.ts} +9 -3
- package/package.json +2 -5
- package/dist/worker/index.d.ts +0 -3
- package/dist/worker/worker.decode.d.ts +0 -3
- package/dist/worker/worker.install.d.ts +0 -2
- package/dist/worker/worker.instance.d.ts +0 -4
|
@@ -1,35 +1,43 @@
|
|
|
1
1
|
import { type ScanArea } from './utils';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
type DecodeFailureHandler = (this: State) => Promise<void> | void;
|
|
3
|
+
type DecodeSuccessHandler = (this: State, data: string, area: ScanArea) => Promise<void> | void;
|
|
4
|
+
type LifecycleHook = (this: State) => void;
|
|
5
|
+
type State = {
|
|
6
|
+
decodeFrameTs: number;
|
|
7
|
+
isDecodeFrameProcessed: boolean;
|
|
8
|
+
isDestroyed: boolean;
|
|
9
|
+
isVideoActive: boolean;
|
|
10
|
+
isVideoPaused: boolean;
|
|
11
|
+
scanArea: ScanArea;
|
|
12
|
+
scanRate: number;
|
|
13
|
+
video: HTMLVideoElement;
|
|
14
|
+
};
|
|
15
|
+
declare function createBarcodeScanner(video: HTMLVideoElement, { debug, getScanArea, handleDecodeFailure, handleDecodeSuccess, lifecycle, scanRate, }?: {
|
|
4
16
|
debug?: boolean;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
facingMode: "environment" | "user";
|
|
18
|
-
isDecodeFrameProcessed: boolean;
|
|
19
|
-
isDestroyed: boolean;
|
|
20
|
-
isReady: boolean;
|
|
21
|
-
isVideoActive: boolean;
|
|
22
|
-
isVideoPaused: boolean;
|
|
23
|
-
onDecodeFailure: () => void;
|
|
24
|
-
onDecodeSuccess: (data: string, area: ScanArea) => void;
|
|
25
|
-
requestFrame: (callback: () => void) => number;
|
|
26
|
-
resumeOnVisibilityChange: boolean;
|
|
27
|
-
scanArea: ScanArea;
|
|
28
|
-
scanRate: number;
|
|
29
|
-
video: HTMLVideoElement;
|
|
30
|
-
worker: null | Worker;
|
|
17
|
+
getScanArea?: (video: HTMLVideoElement) => ScanArea;
|
|
18
|
+
handleDecodeFailure?: DecodeFailureHandler;
|
|
19
|
+
handleDecodeSuccess?: DecodeSuccessHandler;
|
|
20
|
+
lifecycle?: {
|
|
21
|
+
onBeforeCreate?: LifecycleHook;
|
|
22
|
+
onBeforeDecode?: LifecycleHook;
|
|
23
|
+
onBeforeStart?: LifecycleHook;
|
|
24
|
+
onBeforeStop?: LifecycleHook;
|
|
25
|
+
onCreate?: LifecycleHook;
|
|
26
|
+
onDecode?: LifecycleHook;
|
|
27
|
+
onStart?: LifecycleHook;
|
|
28
|
+
onStop?: LifecycleHook;
|
|
31
29
|
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
scanRate?: number;
|
|
31
|
+
}): Promise<{
|
|
32
|
+
destroy: () => void;
|
|
33
|
+
pause: () => Promise<void>;
|
|
34
|
+
start: ({ facingMode, ...rest }?: {
|
|
35
|
+
facingMode?: "environment" | "user";
|
|
36
|
+
handleDecodeFailure?: DecodeFailureHandler;
|
|
37
|
+
handleDecodeSuccess?: DecodeSuccessHandler;
|
|
38
|
+
}) => Promise<void>;
|
|
39
|
+
state: State;
|
|
40
|
+
stop: () => Promise<void>;
|
|
41
|
+
}>;
|
|
42
|
+
export type { DecodeFailureHandler, DecodeSuccessHandler, LifecycleHook, State };
|
|
35
43
|
export { createBarcodeScanner };
|
package/dist/index.js
CHANGED
|
@@ -1,289 +1,289 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
function x(t) {
|
|
2
|
+
const r = new EventTarget(), a = new Proxy(t, {
|
|
3
|
+
get(i, o, c) {
|
|
4
|
+
return Reflect.get(i, o, c);
|
|
5
|
+
},
|
|
6
|
+
set(i, o, c, s) {
|
|
7
|
+
const d = Reflect.set(i, o, c, s);
|
|
8
|
+
return r.dispatchEvent(new CustomEvent("change")), d;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
function n(i, o) {
|
|
12
|
+
let c = i();
|
|
13
|
+
function s() {
|
|
14
|
+
const d = i();
|
|
15
|
+
JSON.stringify(d) !== JSON.stringify(c) && (c = d, o(d));
|
|
16
|
+
}
|
|
17
|
+
return r.addEventListener("change", s), () => r.removeEventListener("change", s);
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
state: a,
|
|
21
|
+
watch: n
|
|
22
|
+
};
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
const O = 1e3 * 32, F = 1e3 * 16;
|
|
25
|
+
function T() {
|
|
26
|
+
const t = new Worker(new URL(
|
|
27
|
+
/* @vite-ignore */
|
|
28
|
+
"" + new URL("worker.js", import.meta.url).href,
|
|
29
|
+
import.meta.url
|
|
30
|
+
), { type: "module" }), r = new Promise((n, i) => {
|
|
31
|
+
const o = setTimeout(() => {
|
|
32
|
+
i(new Error("Worker load timeout"));
|
|
33
|
+
}, O);
|
|
34
|
+
t.addEventListener(
|
|
35
|
+
"message",
|
|
36
|
+
({ data: { payload: c, type: s } }) => {
|
|
37
|
+
s === "init" && (clearTimeout(o), c.status === "success" ? n(!0) : i(new Error("Worker failed to load")));
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
once: !0
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
async function a(n) {
|
|
45
|
+
await r;
|
|
46
|
+
const i = `${performance.now()}-${Math.random().toString(36).slice(2)}`;
|
|
47
|
+
return new Promise((o, c) => {
|
|
48
|
+
const s = setTimeout(() => {
|
|
49
|
+
t.removeEventListener("message", d), c(new Error("Decode timeout"));
|
|
50
|
+
}, F), d = ({ data: { payload: w, type: g } }) => {
|
|
51
|
+
g !== "decode" || w.uuid !== i || (clearTimeout(s), t.removeEventListener("message", d), o(w.data));
|
|
52
|
+
};
|
|
53
|
+
t.addEventListener("message", d), t.postMessage({ payload: { data: n, uuid: i }, type: "decode" });
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
decode: a,
|
|
58
|
+
worker: t
|
|
59
|
+
};
|
|
30
60
|
}
|
|
31
|
-
async function
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
} catch {
|
|
38
|
-
return !1;
|
|
39
|
-
}
|
|
61
|
+
async function V() {
|
|
62
|
+
try {
|
|
63
|
+
return (await navigator.permissions.query({ name: "camera" })).state === "granted";
|
|
64
|
+
} catch {
|
|
65
|
+
return (await navigator.mediaDevices.enumerateDevices()).filter((a) => a.deviceId && a.kind === "videoinput").length > 0;
|
|
66
|
+
}
|
|
40
67
|
}
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
68
|
+
async function H() {
|
|
69
|
+
if (await V())
|
|
70
|
+
return !0;
|
|
71
|
+
try {
|
|
72
|
+
const r = (await navigator.mediaDevices.getUserMedia({ video: !0 })).getTracks();
|
|
73
|
+
for (const a of r)
|
|
74
|
+
a.stop();
|
|
75
|
+
return !0;
|
|
76
|
+
} catch {
|
|
77
|
+
return !1;
|
|
78
|
+
}
|
|
49
79
|
}
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
80
|
+
function k(t) {
|
|
81
|
+
const r = Math.round(0.6666666666666666 * Math.min(t.videoWidth, t.videoHeight));
|
|
82
|
+
return {
|
|
83
|
+
height: r,
|
|
84
|
+
width: r,
|
|
85
|
+
x: Math.round((t.videoWidth - r) / 2),
|
|
86
|
+
y: Math.round((t.videoHeight - r) / 2)
|
|
87
|
+
};
|
|
56
88
|
}
|
|
57
|
-
function
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
switch (r.objectFit) {
|
|
66
|
-
case "contain": return {
|
|
67
|
-
height: s < a ? e.offsetHeight : e.offsetWidth / s,
|
|
68
|
-
width: s < a ? e.offsetHeight * s : e.offsetWidth
|
|
69
|
-
};
|
|
70
|
-
case "cover": return {
|
|
71
|
-
height: s > a ? e.offsetHeight : e.offsetWidth / s,
|
|
72
|
-
width: s > a ? e.offsetHeight * s : e.offsetWidth
|
|
73
|
-
};
|
|
74
|
-
case "fill": return i;
|
|
75
|
-
case "none": return o;
|
|
76
|
-
case "scale-down": return {
|
|
77
|
-
height: Math.min(s < a ? e.offsetHeight : e.offsetWidth / s, e.videoHeight),
|
|
78
|
-
width: Math.min(s < a ? e.offsetHeight * s : e.offsetWidth, e.videoWidth)
|
|
79
|
-
};
|
|
80
|
-
default: return o;
|
|
81
|
-
}
|
|
89
|
+
function A(t, r) {
|
|
90
|
+
const a = window.getComputedStyle(t), [n, i] = a.objectPosition.split(" ").map(
|
|
91
|
+
(o, c) => o.endsWith("%") ? (c === 0 ? t.offsetWidth - r.width : t.offsetHeight - r.height) * parseFloat(o) / 100 : parseFloat(o)
|
|
92
|
+
);
|
|
93
|
+
return {
|
|
94
|
+
x: n,
|
|
95
|
+
y: i
|
|
96
|
+
};
|
|
82
97
|
}
|
|
83
|
-
function
|
|
84
|
-
|
|
98
|
+
function W(t) {
|
|
99
|
+
const r = window.getComputedStyle(t), a = { height: t.offsetHeight, width: t.offsetWidth }, n = a.width / a.height, i = { height: t.videoHeight, width: t.videoWidth }, o = i.width / i.height;
|
|
100
|
+
switch (r.objectFit) {
|
|
101
|
+
case "contain":
|
|
102
|
+
return {
|
|
103
|
+
height: o < n ? t.offsetHeight : t.offsetWidth / o,
|
|
104
|
+
width: o < n ? t.offsetHeight * o : t.offsetWidth
|
|
105
|
+
};
|
|
106
|
+
case "cover":
|
|
107
|
+
return {
|
|
108
|
+
height: o > n ? t.offsetHeight : t.offsetWidth / o,
|
|
109
|
+
width: o > n ? t.offsetHeight * o : t.offsetWidth
|
|
110
|
+
};
|
|
111
|
+
case "fill":
|
|
112
|
+
return a;
|
|
113
|
+
case "none":
|
|
114
|
+
return i;
|
|
115
|
+
case "scale-down":
|
|
116
|
+
return {
|
|
117
|
+
height: Math.min(
|
|
118
|
+
o < n ? t.offsetHeight : t.offsetWidth / o,
|
|
119
|
+
t.videoHeight
|
|
120
|
+
),
|
|
121
|
+
width: Math.min(
|
|
122
|
+
o < n ? t.offsetHeight * o : t.offsetWidth,
|
|
123
|
+
t.videoWidth
|
|
124
|
+
)
|
|
125
|
+
};
|
|
126
|
+
default:
|
|
127
|
+
return i;
|
|
128
|
+
}
|
|
85
129
|
}
|
|
86
|
-
function
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
height: r.height / e.videoHeight * a.height,
|
|
90
|
-
width: r.width / e.videoWidth * a.width,
|
|
91
|
-
x: l / e.videoWidth * a.width + c.x,
|
|
92
|
-
y: u / e.videoHeight * a.height + c.y
|
|
93
|
-
};
|
|
130
|
+
function L(t) {
|
|
131
|
+
return "BarcodeDetector" in t;
|
|
94
132
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
133
|
+
function j(t, r) {
|
|
134
|
+
const a = /scaleX\(-1\)/.test(t.style.transform), n = W(t), i = A(t, n), o = a ? t.videoWidth - r.x - r.width : r.x, c = r.y;
|
|
135
|
+
return {
|
|
136
|
+
height: r.height / t.videoHeight * n.height,
|
|
137
|
+
width: r.width / t.videoWidth * n.width,
|
|
138
|
+
x: o / t.videoWidth * n.width + i.x,
|
|
139
|
+
y: c / t.videoHeight * n.height + i.y
|
|
140
|
+
};
|
|
103
141
|
}
|
|
104
|
-
function
|
|
105
|
-
|
|
142
|
+
function C(t, r) {
|
|
143
|
+
const a = /scaleX\(-1\)/.test(t.style.transform), n = W(t), i = A(t, n), o = a ? n.width - (r.x - i.x) - r.width : r.x - i.x, c = r.y - i.y, s = t.videoHeight / n.height, d = t.videoWidth / n.width;
|
|
144
|
+
return {
|
|
145
|
+
height: r.height * s,
|
|
146
|
+
width: r.width * d,
|
|
147
|
+
x: o * d,
|
|
148
|
+
y: c * s
|
|
149
|
+
};
|
|
106
150
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
["Codabar", "L"],
|
|
110
|
-
["Code39", "L"],
|
|
111
|
-
["Code93", "L"],
|
|
112
|
-
["Code128", "L"],
|
|
113
|
-
["DataBar", "L"],
|
|
114
|
-
["DataBarExpanded", "L"],
|
|
115
|
-
["DataMatrix", "M"],
|
|
116
|
-
["EAN-8", "L"],
|
|
117
|
-
["EAN-13", "L"],
|
|
118
|
-
["ITF", "L"],
|
|
119
|
-
["MaxiCode", "M"],
|
|
120
|
-
["PDF417", "M"],
|
|
121
|
-
["QRCode", "M"],
|
|
122
|
-
["UPC-A", "L"],
|
|
123
|
-
["UPC-E", "L"],
|
|
124
|
-
["MicroQRCode", "M"],
|
|
125
|
-
["rMQRCode", "M"],
|
|
126
|
-
["DXFilmEdge", "L"],
|
|
127
|
-
["DataBarLimited", "L"]
|
|
128
|
-
], Mt = Dt.map(([e]) => e);
|
|
129
|
-
Mt.filter((e, r) => Dt[r][1] === "L"), Mt.filter((e, r) => Dt[r][1] === "M");
|
|
130
|
-
var It = {
|
|
131
|
-
formats: [],
|
|
132
|
-
tryHarder: !0,
|
|
133
|
-
tryRotate: !0,
|
|
134
|
-
tryInvert: !0,
|
|
135
|
-
tryDownscale: !0,
|
|
136
|
-
tryDenoise: !1,
|
|
137
|
-
binarizer: "LocalAverage",
|
|
138
|
-
isPure: !1,
|
|
139
|
-
downscaleFactor: 3,
|
|
140
|
-
downscaleThreshold: 500,
|
|
141
|
-
minLineCount: 2,
|
|
142
|
-
maxNumberOfSymbols: 255,
|
|
143
|
-
tryCode39ExtendedMode: !0,
|
|
144
|
-
returnErrors: !1,
|
|
145
|
-
eanAddOnSymbol: "Ignore",
|
|
146
|
-
textMode: "HRI",
|
|
147
|
-
characterSet: "Unknown"
|
|
148
|
-
};
|
|
149
|
-
({ ...It }), [...It.formats], [...[
|
|
150
|
-
["aztec", "Aztec"],
|
|
151
|
-
["code_128", "Code128"],
|
|
152
|
-
["code_39", "Code39"],
|
|
153
|
-
["code_93", "Code93"],
|
|
154
|
-
["codabar", "Codabar"],
|
|
155
|
-
["databar", "DataBar"],
|
|
156
|
-
["databar_expanded", "DataBarExpanded"],
|
|
157
|
-
["databar_limited", "DataBarLimited"],
|
|
158
|
-
["data_matrix", "DataMatrix"],
|
|
159
|
-
["dx_film_edge", "DXFilmEdge"],
|
|
160
|
-
["ean_13", "EAN-13"],
|
|
161
|
-
["ean_8", "EAN-8"],
|
|
162
|
-
["itf", "ITF"],
|
|
163
|
-
["maxi_code", "MaxiCode"],
|
|
164
|
-
["micro_qr_code", "MicroQRCode"],
|
|
165
|
-
["pdf417", "PDF417"],
|
|
166
|
-
["qr_code", "QRCode"],
|
|
167
|
-
["rm_qr_code", "rMQRCode"],
|
|
168
|
-
["upc_a", "UPC-A"],
|
|
169
|
-
["upc_e", "UPC-E"],
|
|
170
|
-
["linear_codes", "Linear-Codes"],
|
|
171
|
-
["matrix_codes", "Matrix-Codes"],
|
|
172
|
-
["any", "Any"]
|
|
173
|
-
], ["unknown"]].map((e) => e[0]);
|
|
174
|
-
var instance = { value: null };
|
|
175
|
-
async function decode(e) {
|
|
176
|
-
let r = instance.value;
|
|
177
|
-
if (!r) throw Error("Worker not installed");
|
|
178
|
-
let i = `${performance.now()}-${Math.random().toString(36).slice(2)}`;
|
|
179
|
-
return new Promise((a, o) => {
|
|
180
|
-
let s = 0, c = ({ data: { payload: e, type: o } }) => {
|
|
181
|
-
o !== "decode" || e.uuid !== i || (clearTimeout(s), r.removeEventListener("message", c), a(e.data));
|
|
182
|
-
};
|
|
183
|
-
s = setTimeout(() => {
|
|
184
|
-
r.removeEventListener("message", c), o(null);
|
|
185
|
-
}, 1e3), r.addEventListener("message", c), r.postMessage({
|
|
186
|
-
payload: {
|
|
187
|
-
data: e,
|
|
188
|
-
uuid: i
|
|
189
|
-
},
|
|
190
|
-
type: "decode"
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
function install() {
|
|
195
|
-
if (instance.value) return Promise.resolve(instance.value);
|
|
196
|
-
let e = new Worker(new URL(
|
|
197
|
-
/* @vite-ignore */
|
|
198
|
-
"" + new URL("worker.js", import.meta.url).href,
|
|
199
|
-
"" + import.meta.url
|
|
200
|
-
), { type: "module" });
|
|
201
|
-
return new Promise((r, i) => {
|
|
202
|
-
e.addEventListener("message", ({ data: { type: a } }) => {
|
|
203
|
-
a === "init" ? r(e) : i(/* @__PURE__ */ Error("Worker load failed"));
|
|
204
|
-
}, { once: !0 }), instance.value = e;
|
|
205
|
-
});
|
|
151
|
+
function U(t) {
|
|
152
|
+
return new Promise((r) => setTimeout(r, t));
|
|
206
153
|
}
|
|
207
|
-
function
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
154
|
+
async function B(t, {
|
|
155
|
+
debug: r,
|
|
156
|
+
getScanArea: a = k,
|
|
157
|
+
handleDecodeFailure: n = () => {
|
|
158
|
+
},
|
|
159
|
+
handleDecodeSuccess: i = () => {
|
|
160
|
+
},
|
|
161
|
+
lifecycle: o = {},
|
|
162
|
+
scanRate: c = 24
|
|
163
|
+
} = {}) {
|
|
164
|
+
if (!(t instanceof HTMLVideoElement))
|
|
165
|
+
throw new Error("video is not a HTMLVideoElement");
|
|
166
|
+
if (!(i instanceof Function))
|
|
167
|
+
throw new Error("handleDecodeSuccess is not a function");
|
|
168
|
+
if (!(n instanceof Function))
|
|
169
|
+
throw new Error("handleDecodeFailure is not a function");
|
|
170
|
+
const s = document.createElement("canvas"), d = s.getContext("2d", { willReadFrequently: !0 });
|
|
171
|
+
if (!d)
|
|
172
|
+
throw new Error("canvas context is not supported");
|
|
173
|
+
const { decode: w, worker: g } = T(), { state: e } = x({
|
|
174
|
+
decodeFrameTs: performance.now(),
|
|
175
|
+
isDecodeFrameProcessed: !1,
|
|
176
|
+
isDestroyed: !1,
|
|
177
|
+
isVideoActive: !1,
|
|
178
|
+
isVideoPaused: !1,
|
|
179
|
+
scanArea: a(t),
|
|
180
|
+
scanRate: c,
|
|
181
|
+
video: t
|
|
182
|
+
}), v = t.requestVideoFrameCallback?.bind(t) ?? requestAnimationFrame;
|
|
183
|
+
e.video.autoplay = !0, e.video.disablePictureInPicture = !0, e.video.hidden = !1, e.video.muted = !0, e.video.playsInline = !0, o.onCreate && o.onCreate.call(e);
|
|
184
|
+
function b(h, u) {
|
|
185
|
+
v(m);
|
|
186
|
+
async function m() {
|
|
187
|
+
if (e.isDestroyed || e.isVideoActive === !1)
|
|
188
|
+
return;
|
|
189
|
+
if (
|
|
190
|
+
// Skip if the time since the last request frame is less than the scan rate
|
|
191
|
+
performance.now() - e.decodeFrameTs < 1e3 / e.scanRate || // Skip if the frame is already processed
|
|
192
|
+
e.isDecodeFrameProcessed || // Skip if the video is not ready
|
|
193
|
+
e.video.readyState <= 1
|
|
194
|
+
) {
|
|
195
|
+
v(m);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
e.isDecodeFrameProcessed = !0, e.scanArea = a(e.video), o.onBeforeDecode && o.onBeforeDecode.call(e), s.height = e.scanArea.height, s.width = e.scanArea.width, d.clearRect(0, 0, s.width, s.height), d.drawImage(
|
|
199
|
+
e.video,
|
|
200
|
+
e.scanArea.x,
|
|
201
|
+
e.scanArea.y,
|
|
202
|
+
e.scanArea.width,
|
|
203
|
+
e.scanArea.height,
|
|
204
|
+
0,
|
|
205
|
+
0,
|
|
206
|
+
s.width,
|
|
207
|
+
s.height
|
|
208
|
+
);
|
|
209
|
+
const l = d.getImageData(0, 0, s.width, s.height);
|
|
210
|
+
r && window.dispatchEvent(
|
|
211
|
+
new CustomEvent("barcode-scanner:decode-frame", {
|
|
212
|
+
detail: {
|
|
213
|
+
imageData: l
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
);
|
|
217
|
+
try {
|
|
218
|
+
const f = await w(l);
|
|
219
|
+
if (f) {
|
|
220
|
+
const p = f.cornerPoints.map((E) => E.x), y = f.cornerPoints.map((E) => E.y), P = {
|
|
221
|
+
height: Math.max(...y) - Math.min(...y),
|
|
222
|
+
width: Math.max(...p) - Math.min(...p),
|
|
223
|
+
x: Math.min(...p) + e.scanArea.x,
|
|
224
|
+
y: Math.min(...y) + e.scanArea.y
|
|
225
|
+
};
|
|
226
|
+
await Promise.resolve(h.call(e, f.rawValue, P));
|
|
227
|
+
} else
|
|
228
|
+
await Promise.resolve(u.call(e));
|
|
229
|
+
} catch (f) {
|
|
230
|
+
console.warn("Failed to decode barcode"), f && console.error(f);
|
|
231
|
+
} finally {
|
|
232
|
+
o.onDecode && o.onDecode.call(e), e.decodeFrameTs = performance.now(), e.isDecodeFrameProcessed = !1, v(m);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function R() {
|
|
237
|
+
e.isDestroyed || (D(), g.terminate(), e.isDestroyed = !0);
|
|
238
|
+
}
|
|
239
|
+
async function S() {
|
|
240
|
+
if (e.isVideoActive === !1 || e.isVideoPaused || e.isDestroyed)
|
|
241
|
+
return;
|
|
242
|
+
e.video.pause(), s.height = e.video.videoHeight, s.width = e.video.videoWidth, d.drawImage(e.video, 0, 0, s.width, s.height, 0, 0, s.width, s.height);
|
|
243
|
+
const h = await new Promise((u, m) => {
|
|
244
|
+
s.toBlob(
|
|
245
|
+
(l) => {
|
|
246
|
+
l ? u(l) : m(new Error("Failed to convert canvas to blob"));
|
|
247
|
+
},
|
|
248
|
+
"image/jpeg",
|
|
249
|
+
0.9
|
|
250
|
+
);
|
|
251
|
+
});
|
|
252
|
+
e.video.poster.startsWith("blob:") && URL.revokeObjectURL(e.video.poster), e.video.poster = URL.createObjectURL(h), e.video.srcObject instanceof MediaStream && e.video.srcObject.getTracks().forEach((u) => u.stop()), e.isVideoPaused = !0, e.video.srcObject = null;
|
|
253
|
+
}
|
|
254
|
+
async function M({
|
|
255
|
+
facingMode: h = "environment",
|
|
256
|
+
...u
|
|
257
|
+
} = {}) {
|
|
258
|
+
if (o.onBeforeStart && o.onBeforeStart.call(e), !await H())
|
|
259
|
+
throw new Error("No camera access");
|
|
260
|
+
e.video.srcObject instanceof MediaStream || (e.video.srcObject = await navigator.mediaDevices.getUserMedia({
|
|
261
|
+
video: {
|
|
262
|
+
facingMode: h
|
|
263
|
+
}
|
|
264
|
+
}), await e.video.play(), e.isVideoActive = !0, e.isVideoPaused = !1, e.scanArea = a(e.video), e.video.style.transform = h === "user" ? "scaleX(-1)" : "none", o.onStart && o.onStart.call(e), b(u.handleDecodeSuccess ?? i, u.handleDecodeFailure ?? n));
|
|
265
|
+
}
|
|
266
|
+
async function D() {
|
|
267
|
+
e.isVideoActive === !1 || e.isDestroyed || (o.onBeforeStop && o.onBeforeStop.call(e), e.video.srcObject instanceof MediaStream && e.video.srcObject.getTracks().forEach((h) => h.stop()), e.video.poster.startsWith("blob:") && URL.revokeObjectURL(e.video.poster), e.isVideoActive = !1, e.isVideoPaused = !1, e.video.poster = "", e.video.srcObject = null, o.onStop && o.onStop.call(e));
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
destroy: R,
|
|
271
|
+
pause: S,
|
|
272
|
+
start: M,
|
|
273
|
+
state: e,
|
|
274
|
+
stop: D
|
|
275
|
+
};
|
|
287
276
|
}
|
|
288
|
-
|
|
289
|
-
|
|
277
|
+
export {
|
|
278
|
+
B as createBarcodeScanner,
|
|
279
|
+
B as default,
|
|
280
|
+
H as getCameraAccess,
|
|
281
|
+
k as getScanArea,
|
|
282
|
+
A as getVideoRenderOffset,
|
|
283
|
+
W as getVideoRenderSize,
|
|
284
|
+
V as hasCameraAccess,
|
|
285
|
+
L as isBarcodeDetectorAvailable,
|
|
286
|
+
j as translateAreaToVideoRender,
|
|
287
|
+
C as translateAreaToVideoSource,
|
|
288
|
+
U as wait
|
|
289
|
+
};
|