@nddeps/barcode-scanner 0.5.2 → 0.6.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/LICENSE +21 -0
- package/dist/index.d.ts +135 -5
- package/dist/index.js +123 -100
- package/package.json +8 -10
- package/dist/constants.d.ts +0 -7
- package/dist/create-barcode-scanner.d.ts +0 -54
- package/dist/create-worker.d.ts +0 -8
- package/dist/utils/get-camera-access.d.ts +0 -2
- package/dist/utils/get-scan-area.d.ts +0 -9
- package/dist/utils/get-video-render-offset.d.ts +0 -8
- package/dist/utils/get-video-render-size.d.ts +0 -7
- package/dist/utils/has-camera-access.d.ts +0 -2
- package/dist/utils/index.d.ts +0 -9
- package/dist/utils/is-barcode-detector-available.d.ts +0 -7
- package/dist/utils/translate-area-to-video-render.d.ts +0 -3
- package/dist/utils/translate-area-to-video-source.d.ts +0 -3
- package/dist/utils/wait.d.ts +0 -2
- package/dist/worker.types.d.ts +0 -29
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 No Deprecated Dependencies
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,135 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { BarcodeDetector } from 'barcode-detector/ponyfill';
|
|
2
|
+
import { BarcodeDetectorOptions } from 'barcode-detector/ponyfill';
|
|
3
|
+
import { BarcodeFormat } from 'barcode-detector/ponyfill';
|
|
4
|
+
import { DetectedBarcode } from 'barcode-detector/ponyfill';
|
|
5
|
+
|
|
6
|
+
export declare const BARCODE_SCANNER_BEFORE_START_EVENT = "barcode-scanner:beforestart";
|
|
7
|
+
|
|
8
|
+
export declare const BARCODE_SCANNER_DECODE_FRAME_EVENT = "barcode-scanner:decode-frame";
|
|
9
|
+
|
|
10
|
+
export declare const BARCODE_SCANNER_START_EVENT = "barcode-scanner:start";
|
|
11
|
+
|
|
12
|
+
export declare type Context = {
|
|
13
|
+
state: State;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
declare function createBarcodeScanner(video: HTMLVideoElement, { debug, formats, getScanArea, handleDecodeFailure, handleDecodeSuccess, lifecycle, scanRate, }?: Options): Promise<{
|
|
17
|
+
decode: (imageData: ImageData) => Promise<DetectedBarcode | null>;
|
|
18
|
+
destroy: () => Promise<void>;
|
|
19
|
+
pause: () => Promise<void>;
|
|
20
|
+
start: ({ facingMode, ...rest }?: {
|
|
21
|
+
facingMode?: "environment" | "user";
|
|
22
|
+
handleDecodeFailure?: DecodeFailureHandler;
|
|
23
|
+
handleDecodeSuccess?: DecodeSuccessHandler;
|
|
24
|
+
}) => Promise<void>;
|
|
25
|
+
state: State;
|
|
26
|
+
stop: () => Promise<void>;
|
|
27
|
+
}>;
|
|
28
|
+
export { createBarcodeScanner }
|
|
29
|
+
export default createBarcodeScanner;
|
|
30
|
+
|
|
31
|
+
export declare function createWorker({ formats }: {
|
|
32
|
+
formats: BarcodeFormat[];
|
|
33
|
+
}): {
|
|
34
|
+
decode: (imageData: ImageData) => Promise<DetectedBarcode | null>;
|
|
35
|
+
worker: Worker;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export declare type DecodeFailureHandler = () => Promise<void> | void;
|
|
39
|
+
|
|
40
|
+
export declare type DecodeSuccessHandler = (data: string, area: ScanArea) => Promise<void> | void;
|
|
41
|
+
|
|
42
|
+
export declare function getCameraAccess(): Promise<boolean>;
|
|
43
|
+
|
|
44
|
+
export declare function getScanArea(video: HTMLVideoElement): ScanArea;
|
|
45
|
+
|
|
46
|
+
export declare function getVideoRenderOffset(video: HTMLVideoElement, renderSize: RenderSize): RenderOffset;
|
|
47
|
+
|
|
48
|
+
export declare function getVideoRenderSize(video: HTMLVideoElement): RenderSize;
|
|
49
|
+
|
|
50
|
+
export declare function hasCameraAccess(): Promise<boolean>;
|
|
51
|
+
|
|
52
|
+
export declare function isBarcodeDetectorAvailable<T extends object>(value: T): value is {
|
|
53
|
+
BarcodeDetector: {
|
|
54
|
+
new (barcodeDetectorOptions?: BarcodeDetectorOptions): BarcodeDetector;
|
|
55
|
+
} & BarcodeDetector;
|
|
56
|
+
} & T;
|
|
57
|
+
|
|
58
|
+
export declare type Lifecycle = {
|
|
59
|
+
onBeforeCreate?: LifecycleHook;
|
|
60
|
+
onBeforeDecode?: LifecycleHook;
|
|
61
|
+
onBeforePause?: LifecycleHook;
|
|
62
|
+
onBeforeStart?: LifecycleHook;
|
|
63
|
+
onBeforeStop?: LifecycleHook;
|
|
64
|
+
onCreate?: LifecycleHook;
|
|
65
|
+
onDecode?: LifecycleHook;
|
|
66
|
+
onPause?: LifecycleHook;
|
|
67
|
+
onStart?: LifecycleHook;
|
|
68
|
+
onStop?: LifecycleHook;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export declare type LifecycleHook = (ctx: Context) => void;
|
|
72
|
+
|
|
73
|
+
export declare type Options = {
|
|
74
|
+
debug?: boolean;
|
|
75
|
+
formats?: BarcodeFormat[];
|
|
76
|
+
getScanArea?: (video: HTMLVideoElement) => ScanArea;
|
|
77
|
+
handleDecodeFailure?: DecodeFailureHandler;
|
|
78
|
+
handleDecodeSuccess?: DecodeSuccessHandler;
|
|
79
|
+
lifecycle?: Lifecycle;
|
|
80
|
+
scanRate?: number;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export declare type RenderOffset = {
|
|
84
|
+
x: number;
|
|
85
|
+
y: number;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export declare type RenderSize = {
|
|
89
|
+
height: number;
|
|
90
|
+
width: number;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export declare type ScanArea = {
|
|
94
|
+
height: number;
|
|
95
|
+
width: number;
|
|
96
|
+
x: number;
|
|
97
|
+
y: number;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export declare type State = {
|
|
101
|
+
decodeFrameTs: number;
|
|
102
|
+
isDecodeFrameProcessed: boolean;
|
|
103
|
+
isDestroyed: boolean;
|
|
104
|
+
isVideoActive: boolean;
|
|
105
|
+
isVideoPaused: boolean;
|
|
106
|
+
isWorkerLoadFailure: boolean;
|
|
107
|
+
scanArea: ScanArea;
|
|
108
|
+
scanRate: number;
|
|
109
|
+
video: HTMLVideoElement;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export declare function translateAreaToVideoRender(video: HTMLVideoElement, area: ScanArea): ScanArea;
|
|
113
|
+
|
|
114
|
+
export declare function translateAreaToVideoSource(video: HTMLVideoElement, area: ScanArea): ScanArea;
|
|
115
|
+
|
|
116
|
+
export declare function wait(ms: number): Promise<unknown>;
|
|
117
|
+
|
|
118
|
+
export declare function waitForEvent(event: string, { resolveOnError, timeout }?: {
|
|
119
|
+
resolveOnError?: boolean;
|
|
120
|
+
timeout?: number;
|
|
121
|
+
}): Promise<unknown>;
|
|
122
|
+
|
|
123
|
+
export declare const WORKER_DECODE_FAILURE_CAUSE = "decode-failure";
|
|
124
|
+
|
|
125
|
+
export declare const WORKER_DECODE_TIMEOUT: number;
|
|
126
|
+
|
|
127
|
+
export declare const WORKER_DECODE_TIMEOUT_CAUSE = "decode-timeout";
|
|
128
|
+
|
|
129
|
+
export declare const WORKER_LOAD_FAILURE_CAUSE = "worker-load-failure";
|
|
130
|
+
|
|
131
|
+
export declare const WORKER_LOAD_TIMEOUT: number;
|
|
132
|
+
|
|
133
|
+
export declare const WORKER_LOAD_TIMEOUT_CAUSE = "worker-load-timeout";
|
|
134
|
+
|
|
135
|
+
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,36 +1,35 @@
|
|
|
1
|
-
const L = "decode-timeout",
|
|
2
|
-
|
|
3
|
-
function H({ formats: e }) {
|
|
1
|
+
const B = "barcode-scanner:beforestart", L = "barcode-scanner:decode-frame", j = "barcode-scanner:start", N = "decode-failure", K = 16e3, V = "decode-timeout", y = "worker-load-failure", q = 32e3, S = "worker-load-timeout", b = "" + new URL("zxing-reader.wasm", import.meta.url).href;
|
|
2
|
+
function k({ formats: e }) {
|
|
4
3
|
const o = new Worker(new URL(
|
|
5
4
|
/* @vite-ignore */
|
|
6
5
|
"" + new URL("worker.js", import.meta.url).href,
|
|
7
6
|
import.meta.url
|
|
8
|
-
), { type: "module" }),
|
|
7
|
+
), { type: "module" }), i = new Promise((a, s) => {
|
|
9
8
|
const r = setTimeout(() => {
|
|
10
|
-
o.removeEventListener("message", h), s(new Error(
|
|
11
|
-
}, 32e3), h = ({ data: { payload: c, type:
|
|
12
|
-
|
|
9
|
+
o.removeEventListener("message", h), s(new Error(S));
|
|
10
|
+
}, 32e3), h = ({ data: { payload: c, type: f } }) => {
|
|
11
|
+
f === "init" && (clearTimeout(r), o.removeEventListener("message", h), c.status === "success" ? a(!0) : s(new Error(y)));
|
|
13
12
|
};
|
|
14
13
|
o.addEventListener("message", h), o.postMessage({
|
|
15
14
|
payload: {
|
|
16
15
|
formats: e,
|
|
17
|
-
wasmUrl:
|
|
16
|
+
wasmUrl: b
|
|
18
17
|
},
|
|
19
18
|
type: "config"
|
|
20
19
|
});
|
|
21
20
|
});
|
|
22
|
-
async function n(
|
|
23
|
-
await
|
|
21
|
+
async function n(a) {
|
|
22
|
+
await i;
|
|
24
23
|
const s = `${performance.now()}-${Math.random().toString(36).slice(2)}`;
|
|
25
24
|
return new Promise((r, h) => {
|
|
26
25
|
const c = setTimeout(() => {
|
|
27
|
-
o.removeEventListener("message",
|
|
28
|
-
}, 16e3),
|
|
29
|
-
|
|
26
|
+
o.removeEventListener("message", f), h(new Error(V));
|
|
27
|
+
}, 16e3), f = ({ data: { payload: E, type: A } }) => {
|
|
28
|
+
A !== "decode" || E.uuid !== s || (clearTimeout(c), o.removeEventListener("message", f), r(E.data));
|
|
30
29
|
};
|
|
31
|
-
o.addEventListener("message",
|
|
30
|
+
o.addEventListener("message", f), o.postMessage({
|
|
32
31
|
payload: {
|
|
33
|
-
data:
|
|
32
|
+
data: a,
|
|
34
33
|
uuid: s
|
|
35
34
|
},
|
|
36
35
|
type: "decode"
|
|
@@ -42,26 +41,26 @@ function H({ formats: e }) {
|
|
|
42
41
|
worker: o
|
|
43
42
|
};
|
|
44
43
|
}
|
|
45
|
-
async function
|
|
44
|
+
async function H() {
|
|
46
45
|
try {
|
|
47
46
|
return (await navigator.permissions.query({ name: "camera" })).state === "granted";
|
|
48
47
|
} catch {
|
|
49
|
-
return (await navigator.mediaDevices.enumerateDevices()).filter((
|
|
48
|
+
return (await navigator.mediaDevices.enumerateDevices()).filter((i) => i.deviceId && i.kind === "videoinput").length > 0;
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
|
-
async function
|
|
53
|
-
if (await
|
|
51
|
+
async function U() {
|
|
52
|
+
if (await H())
|
|
54
53
|
return !0;
|
|
55
54
|
try {
|
|
56
55
|
const o = (await navigator.mediaDevices.getUserMedia({ video: !0 })).getTracks();
|
|
57
|
-
for (const
|
|
58
|
-
|
|
56
|
+
for (const i of o)
|
|
57
|
+
i.stop();
|
|
59
58
|
return !0;
|
|
60
59
|
} catch {
|
|
61
60
|
return !1;
|
|
62
61
|
}
|
|
63
62
|
}
|
|
64
|
-
function
|
|
63
|
+
function I(e) {
|
|
65
64
|
const o = Math.round(0.6666666666666666 * Math.min(e.videoWidth, e.videoHeight));
|
|
66
65
|
return {
|
|
67
66
|
height: o,
|
|
@@ -70,17 +69,17 @@ function C(e) {
|
|
|
70
69
|
y: Math.round((e.videoHeight - o) / 2)
|
|
71
70
|
};
|
|
72
71
|
}
|
|
73
|
-
function
|
|
74
|
-
const
|
|
72
|
+
function M(e, o) {
|
|
73
|
+
const i = window.getComputedStyle(e), [n, a] = i.objectPosition.split(" ").map(
|
|
75
74
|
(s, r) => s.endsWith("%") ? (r === 0 ? e.offsetWidth - o.width : e.offsetHeight - o.height) * parseFloat(s) / 100 : parseFloat(s)
|
|
76
75
|
);
|
|
77
76
|
return {
|
|
78
77
|
x: n,
|
|
79
|
-
y:
|
|
78
|
+
y: a
|
|
80
79
|
};
|
|
81
80
|
}
|
|
82
|
-
function
|
|
83
|
-
const o = window.getComputedStyle(e),
|
|
81
|
+
function _(e) {
|
|
82
|
+
const o = window.getComputedStyle(e), i = { height: e.offsetHeight, width: e.offsetWidth }, n = i.width / i.height, a = { height: e.videoHeight, width: e.videoWidth }, s = a.width / a.height;
|
|
84
83
|
switch (o.objectFit) {
|
|
85
84
|
case "contain":
|
|
86
85
|
return {
|
|
@@ -93,9 +92,9 @@ function S(e) {
|
|
|
93
92
|
width: s > n ? e.offsetHeight * s : e.offsetWidth
|
|
94
93
|
};
|
|
95
94
|
case "fill":
|
|
96
|
-
return a;
|
|
97
|
-
case "none":
|
|
98
95
|
return i;
|
|
96
|
+
case "none":
|
|
97
|
+
return a;
|
|
99
98
|
case "scale-down":
|
|
100
99
|
return {
|
|
101
100
|
height: Math.min(
|
|
@@ -108,23 +107,23 @@ function S(e) {
|
|
|
108
107
|
)
|
|
109
108
|
};
|
|
110
109
|
default:
|
|
111
|
-
return
|
|
110
|
+
return a;
|
|
112
111
|
}
|
|
113
112
|
}
|
|
114
|
-
function
|
|
113
|
+
function z(e) {
|
|
115
114
|
return "BarcodeDetector" in e;
|
|
116
115
|
}
|
|
117
|
-
function
|
|
118
|
-
const
|
|
116
|
+
function X(e, o) {
|
|
117
|
+
const i = /scaleX\(-1\)/.test(e.style.transform), n = _(e), a = M(e, n), s = i ? e.videoWidth - o.x - o.width : o.x, r = o.y;
|
|
119
118
|
return {
|
|
120
119
|
height: o.height / e.videoHeight * n.height,
|
|
121
120
|
width: o.width / e.videoWidth * n.width,
|
|
122
|
-
x: s / e.videoWidth * n.width +
|
|
123
|
-
y: r / e.videoHeight * n.height +
|
|
121
|
+
x: s / e.videoWidth * n.width + a.x,
|
|
122
|
+
y: r / e.videoHeight * n.height + a.y
|
|
124
123
|
};
|
|
125
124
|
}
|
|
126
|
-
function
|
|
127
|
-
const
|
|
125
|
+
function Y(e, o) {
|
|
126
|
+
const i = /scaleX\(-1\)/.test(e.style.transform), n = _(e), a = M(e, n), s = i ? n.width - (o.x - a.x) - o.width : o.x - a.x, r = o.y - a.y, h = e.videoHeight / n.height, c = e.videoWidth / n.width;
|
|
128
127
|
return {
|
|
129
128
|
height: o.height * h,
|
|
130
129
|
width: o.width * c,
|
|
@@ -132,14 +131,24 @@ function B(e, o) {
|
|
|
132
131
|
y: r * h
|
|
133
132
|
};
|
|
134
133
|
}
|
|
135
|
-
function
|
|
134
|
+
function $(e) {
|
|
136
135
|
return new Promise((o) => setTimeout(o, e));
|
|
137
136
|
}
|
|
138
|
-
|
|
137
|
+
function G(e, { resolveOnError: o = !1, timeout: i = 1e3 * 8 } = {}) {
|
|
138
|
+
return new Promise((n, a) => {
|
|
139
|
+
const s = setTimeout(() => {
|
|
140
|
+
window.removeEventListener(e, r), o ? n(null) : a(new Error(`Event ${e} not received`));
|
|
141
|
+
}, i), r = () => {
|
|
142
|
+
clearTimeout(s), window.removeEventListener(e, r), n(null);
|
|
143
|
+
};
|
|
144
|
+
window.addEventListener(e, r);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
async function J(e, {
|
|
139
148
|
debug: o,
|
|
140
|
-
formats:
|
|
141
|
-
getScanArea: n =
|
|
142
|
-
handleDecodeFailure:
|
|
149
|
+
formats: i = ["qr_code"],
|
|
150
|
+
getScanArea: n = I,
|
|
151
|
+
handleDecodeFailure: a,
|
|
143
152
|
handleDecodeSuccess: s,
|
|
144
153
|
lifecycle: r = {},
|
|
145
154
|
scanRate: h = 24
|
|
@@ -148,12 +157,12 @@ async function q(e, {
|
|
|
148
157
|
throw new Error("video is not a HTMLVideoElement");
|
|
149
158
|
if (!(s instanceof Function))
|
|
150
159
|
throw new Error("handleDecodeSuccess is not a function");
|
|
151
|
-
if (!(
|
|
160
|
+
if (!(a instanceof Function))
|
|
152
161
|
throw new Error("handleDecodeFailure is not a function");
|
|
153
|
-
const c = document.createElement("canvas"),
|
|
154
|
-
if (!
|
|
162
|
+
const c = document.createElement("canvas"), f = c.getContext("2d", { willReadFrequently: !0 });
|
|
163
|
+
if (!f)
|
|
155
164
|
throw new Error("canvas context is not supported");
|
|
156
|
-
const { decode:
|
|
165
|
+
const { decode: E, worker: A } = k({ formats: i }), t = {
|
|
157
166
|
decodeFrameTs: performance.now(),
|
|
158
167
|
isDecodeFrameProcessed: !1,
|
|
159
168
|
isDestroyed: !1,
|
|
@@ -163,12 +172,13 @@ async function q(e, {
|
|
|
163
172
|
scanArea: n(e),
|
|
164
173
|
scanRate: h,
|
|
165
174
|
video: e
|
|
166
|
-
},
|
|
167
|
-
t.video.autoplay = !0, t.video.disablePictureInPicture = !0, t.video.hidden = !1, t.video.muted = !0, t.video.playsInline = !0, r.onCreate && r.onCreate(
|
|
168
|
-
|
|
175
|
+
}, m = { state: t }, D = e.requestVideoFrameCallback?.bind(e) ?? requestAnimationFrame;
|
|
176
|
+
t.video.autoplay = !0, t.video.disablePictureInPicture = !0, t.video.hidden = !1, t.video.muted = !0, t.video.playsInline = !0, r.onCreate && r.onCreate(m);
|
|
177
|
+
let w = null;
|
|
178
|
+
function W(d, g = () => {
|
|
169
179
|
}) {
|
|
170
|
-
|
|
171
|
-
async function
|
|
180
|
+
D(l);
|
|
181
|
+
async function l() {
|
|
172
182
|
if (t.isDestroyed || t.isVideoActive === !1)
|
|
173
183
|
return;
|
|
174
184
|
if (
|
|
@@ -177,10 +187,10 @@ async function q(e, {
|
|
|
177
187
|
t.isDecodeFrameProcessed || // Skip if the video is not ready
|
|
178
188
|
t.video.readyState <= 1
|
|
179
189
|
) {
|
|
180
|
-
|
|
190
|
+
D(l);
|
|
181
191
|
return;
|
|
182
192
|
}
|
|
183
|
-
t.isDecodeFrameProcessed = !0, t.scanArea = n(t.video), r.onBeforeDecode && r.onBeforeDecode(
|
|
193
|
+
t.isDecodeFrameProcessed = !0, t.scanArea = n(t.video), r.onBeforeDecode && r.onBeforeDecode(m), c.height = t.scanArea.height, c.width = t.scanArea.width, f.clearRect(0, 0, c.width, c.height), f.drawImage(
|
|
184
194
|
t.video,
|
|
185
195
|
t.scanArea.x,
|
|
186
196
|
t.scanArea.y,
|
|
@@ -191,77 +201,90 @@ async function q(e, {
|
|
|
191
201
|
c.width,
|
|
192
202
|
c.height
|
|
193
203
|
);
|
|
194
|
-
const
|
|
204
|
+
const v = f.getImageData(0, 0, c.width, c.height);
|
|
195
205
|
o && window.dispatchEvent(
|
|
196
|
-
new CustomEvent(
|
|
206
|
+
new CustomEvent(L, {
|
|
197
207
|
detail: {
|
|
198
|
-
imageData:
|
|
208
|
+
imageData: v
|
|
199
209
|
}
|
|
200
210
|
})
|
|
201
211
|
);
|
|
202
212
|
try {
|
|
203
|
-
const
|
|
204
|
-
if (
|
|
205
|
-
const
|
|
206
|
-
height: Math.max(...
|
|
207
|
-
width: Math.max(...
|
|
208
|
-
x: Math.min(...
|
|
209
|
-
y: Math.min(...
|
|
213
|
+
const u = await E(v);
|
|
214
|
+
if (u) {
|
|
215
|
+
const R = u.cornerPoints.map((O) => O.x), p = u.cornerPoints.map((O) => O.y), x = {
|
|
216
|
+
height: Math.max(...p) - Math.min(...p),
|
|
217
|
+
width: Math.max(...R) - Math.min(...R),
|
|
218
|
+
x: Math.min(...R) + t.scanArea.x,
|
|
219
|
+
y: Math.min(...p) + t.scanArea.y
|
|
210
220
|
};
|
|
211
|
-
await Promise.resolve(
|
|
221
|
+
await Promise.resolve(d(u.rawValue, x));
|
|
212
222
|
} else
|
|
213
|
-
await Promise.resolve(
|
|
214
|
-
} catch (
|
|
215
|
-
console.warn("Failed to decode barcode"),
|
|
223
|
+
await Promise.resolve(g());
|
|
224
|
+
} catch (u) {
|
|
225
|
+
console.warn("Failed to decode barcode"), u && (console.error(u), u instanceof Error && (u.cause === y || u.cause === S) && (t.isWorkerLoadFailure = !0));
|
|
216
226
|
} finally {
|
|
217
|
-
r.onDecode && r.onDecode(
|
|
227
|
+
r.onDecode && r.onDecode(m), t.isWorkerLoadFailure === !1 && (t.isDecodeFrameProcessed = !1, t.decodeFrameTs = performance.now(), D(l));
|
|
218
228
|
}
|
|
219
229
|
}
|
|
220
230
|
}
|
|
221
|
-
function F() {
|
|
222
|
-
t.isDestroyed || (
|
|
231
|
+
async function F() {
|
|
232
|
+
t.isDestroyed || (await T(), A.terminate(), t.isDestroyed = !0);
|
|
223
233
|
}
|
|
224
234
|
async function P() {
|
|
225
|
-
t.isVideoActive === !1 || t.isVideoPaused || t.isDestroyed || (r.onBeforePause && r.onBeforePause(
|
|
235
|
+
t.isVideoActive === !1 || t.isVideoPaused || t.isDestroyed || (w && await w.catch((d) => console.error(d)), r.onBeforePause && r.onBeforePause(m), t.video.srcObject instanceof MediaStream && (t.video.srcObject.getTracks().forEach((d) => d.stop()), t.video.srcObject = null), t.isVideoPaused = !0, r.onPause && r.onPause(m));
|
|
226
236
|
}
|
|
227
|
-
async function
|
|
228
|
-
facingMode:
|
|
229
|
-
...
|
|
237
|
+
async function C({
|
|
238
|
+
facingMode: d = "environment",
|
|
239
|
+
...g
|
|
230
240
|
} = {}) {
|
|
231
|
-
const w = l.handleDecodeSuccess ?? s, E = l.handleDecodeFailure ?? i;
|
|
232
241
|
if (!w)
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
242
|
+
return w = (async () => {
|
|
243
|
+
const l = g.handleDecodeSuccess ?? s, v = g.handleDecodeFailure ?? a;
|
|
244
|
+
if (!l)
|
|
245
|
+
throw new Error("handleDecodeSuccess is required");
|
|
246
|
+
if (r.onBeforeStart && r.onBeforeStart(m), !await U())
|
|
247
|
+
throw new Error("No camera access");
|
|
248
|
+
t.video.srcObject instanceof MediaStream || (t.video.srcObject = await navigator.mediaDevices.getUserMedia({
|
|
249
|
+
video: {
|
|
250
|
+
facingMode: d
|
|
251
|
+
}
|
|
252
|
+
}), await t.video.play(), t.isVideoActive = !0, t.isVideoPaused = !1, t.scanArea = n(t.video), t.video.style.transform = d === "user" ? "scaleX(-1)" : "none", r.onStart && r.onStart(m), W(l, v));
|
|
253
|
+
})().finally(() => w = null), w;
|
|
241
254
|
}
|
|
242
|
-
async function
|
|
243
|
-
t.
|
|
255
|
+
async function T() {
|
|
256
|
+
t.isDestroyed || (w && await w.catch((d) => console.error(d)), r.onBeforeStop && r.onBeforeStop(m), t.video.srcObject instanceof MediaStream && (t.video.srcObject.getTracks().forEach((d) => d.stop()), t.video.srcObject = null), t.isVideoActive = !1, t.isVideoPaused = !1, t.video.poster = "", r.onStop && r.onStop(m));
|
|
244
257
|
}
|
|
245
258
|
return {
|
|
246
|
-
decode:
|
|
259
|
+
decode: E,
|
|
247
260
|
destroy: F,
|
|
248
261
|
pause: P,
|
|
249
|
-
start:
|
|
262
|
+
start: C,
|
|
250
263
|
state: t,
|
|
251
|
-
stop:
|
|
264
|
+
stop: T
|
|
252
265
|
};
|
|
253
266
|
}
|
|
254
267
|
export {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
268
|
+
B as BARCODE_SCANNER_BEFORE_START_EVENT,
|
|
269
|
+
L as BARCODE_SCANNER_DECODE_FRAME_EVENT,
|
|
270
|
+
j as BARCODE_SCANNER_START_EVENT,
|
|
271
|
+
N as WORKER_DECODE_FAILURE_CAUSE,
|
|
272
|
+
K as WORKER_DECODE_TIMEOUT,
|
|
273
|
+
V as WORKER_DECODE_TIMEOUT_CAUSE,
|
|
274
|
+
y as WORKER_LOAD_FAILURE_CAUSE,
|
|
275
|
+
q as WORKER_LOAD_TIMEOUT,
|
|
276
|
+
S as WORKER_LOAD_TIMEOUT_CAUSE,
|
|
277
|
+
J as createBarcodeScanner,
|
|
278
|
+
k as createWorker,
|
|
279
|
+
J as default,
|
|
280
|
+
U as getCameraAccess,
|
|
281
|
+
I as getScanArea,
|
|
282
|
+
M as getVideoRenderOffset,
|
|
283
|
+
_ as getVideoRenderSize,
|
|
284
|
+
H as hasCameraAccess,
|
|
285
|
+
z as isBarcodeDetectorAvailable,
|
|
286
|
+
X as translateAreaToVideoRender,
|
|
287
|
+
Y as translateAreaToVideoSource,
|
|
288
|
+
$ as wait,
|
|
289
|
+
G as waitForEvent
|
|
267
290
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nddeps/barcode-scanner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"barcode",
|
|
@@ -16,13 +16,10 @@
|
|
|
16
16
|
"author": "Aleksei Saenko",
|
|
17
17
|
"type": "module",
|
|
18
18
|
"exports": {
|
|
19
|
-
".":
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"./worker": {
|
|
24
|
-
"import": "./dist/worker.js"
|
|
25
|
-
}
|
|
19
|
+
".": "./dist/index.js",
|
|
20
|
+
"./package.json": "./package.json",
|
|
21
|
+
"./wasm": "./dist/zxing-reader.wasm",
|
|
22
|
+
"./worker": "./dist/worker.js"
|
|
26
23
|
},
|
|
27
24
|
"main": "./dist/index.js",
|
|
28
25
|
"types": "./dist/index.d.ts",
|
|
@@ -30,7 +27,7 @@
|
|
|
30
27
|
"./dist"
|
|
31
28
|
],
|
|
32
29
|
"scripts": {
|
|
33
|
-
"build": "vite build
|
|
30
|
+
"build": "vite build",
|
|
34
31
|
"dev": "vite",
|
|
35
32
|
"preview": "vite preview"
|
|
36
33
|
},
|
|
@@ -60,6 +57,7 @@
|
|
|
60
57
|
"stylelint-order": "^7.0.1",
|
|
61
58
|
"typescript": "~5.9.3",
|
|
62
59
|
"typescript-eslint": "^8.54.0",
|
|
63
|
-
"vite": "^7.3.1"
|
|
60
|
+
"vite": "^7.3.1",
|
|
61
|
+
"vite-plugin-dts": "^4.5.4"
|
|
64
62
|
}
|
|
65
63
|
}
|
package/dist/constants.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
declare const WORKER_DECODE_TIMEOUT: number;
|
|
2
|
-
declare const WORKER_DECODE_TIMEOUT_CAUSE = "decode-timeout";
|
|
3
|
-
declare const WORKER_DECODE_FAILURE_CAUSE = "decode-failure";
|
|
4
|
-
declare const WORKER_LOAD_FAILURE_CAUSE = "worker-load-failure";
|
|
5
|
-
declare const WORKER_LOAD_TIMEOUT: number;
|
|
6
|
-
declare const WORKER_LOAD_TIMEOUT_CAUSE = "worker-load-timeout";
|
|
7
|
-
export { WORKER_DECODE_FAILURE_CAUSE, WORKER_DECODE_TIMEOUT, WORKER_DECODE_TIMEOUT_CAUSE, WORKER_LOAD_FAILURE_CAUSE, WORKER_LOAD_TIMEOUT, WORKER_LOAD_TIMEOUT_CAUSE, };
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { BarcodeFormat } from 'barcode-detector/ponyfill';
|
|
2
|
-
import { type ScanArea } from './utils';
|
|
3
|
-
type Context = {
|
|
4
|
-
state: State;
|
|
5
|
-
};
|
|
6
|
-
type DecodeFailureHandler = () => Promise<void> | void;
|
|
7
|
-
type DecodeSuccessHandler = (data: string, area: ScanArea) => Promise<void> | void;
|
|
8
|
-
type Lifecycle = {
|
|
9
|
-
onBeforeCreate?: LifecycleHook;
|
|
10
|
-
onBeforeDecode?: LifecycleHook;
|
|
11
|
-
onBeforePause?: LifecycleHook;
|
|
12
|
-
onBeforeStart?: LifecycleHook;
|
|
13
|
-
onBeforeStop?: LifecycleHook;
|
|
14
|
-
onCreate?: LifecycleHook;
|
|
15
|
-
onDecode?: LifecycleHook;
|
|
16
|
-
onPause?: LifecycleHook;
|
|
17
|
-
onStart?: LifecycleHook;
|
|
18
|
-
onStop?: LifecycleHook;
|
|
19
|
-
};
|
|
20
|
-
type LifecycleHook = (ctx: Context) => void;
|
|
21
|
-
type Options = {
|
|
22
|
-
debug?: boolean;
|
|
23
|
-
formats?: BarcodeFormat[];
|
|
24
|
-
getScanArea?: (video: HTMLVideoElement) => ScanArea;
|
|
25
|
-
handleDecodeFailure?: DecodeFailureHandler;
|
|
26
|
-
handleDecodeSuccess?: DecodeSuccessHandler;
|
|
27
|
-
lifecycle?: Lifecycle;
|
|
28
|
-
scanRate?: number;
|
|
29
|
-
};
|
|
30
|
-
type State = {
|
|
31
|
-
decodeFrameTs: number;
|
|
32
|
-
isDecodeFrameProcessed: boolean;
|
|
33
|
-
isDestroyed: boolean;
|
|
34
|
-
isVideoActive: boolean;
|
|
35
|
-
isVideoPaused: boolean;
|
|
36
|
-
isWorkerLoadFailure: boolean;
|
|
37
|
-
scanArea: ScanArea;
|
|
38
|
-
scanRate: number;
|
|
39
|
-
video: HTMLVideoElement;
|
|
40
|
-
};
|
|
41
|
-
declare function createBarcodeScanner(video: HTMLVideoElement, { debug, formats, getScanArea, handleDecodeFailure, handleDecodeSuccess, lifecycle, scanRate, }?: Options): Promise<{
|
|
42
|
-
decode: (imageData: ImageData) => Promise<import("barcode-detector/ponyfill").DetectedBarcode | null>;
|
|
43
|
-
destroy: () => void;
|
|
44
|
-
pause: () => Promise<void>;
|
|
45
|
-
start: ({ facingMode, ...rest }?: {
|
|
46
|
-
facingMode?: "environment" | "user";
|
|
47
|
-
handleDecodeFailure?: DecodeFailureHandler;
|
|
48
|
-
handleDecodeSuccess?: DecodeSuccessHandler;
|
|
49
|
-
}) => Promise<void>;
|
|
50
|
-
state: State;
|
|
51
|
-
stop: () => Promise<void>;
|
|
52
|
-
}>;
|
|
53
|
-
export type { DecodeFailureHandler, DecodeSuccessHandler, LifecycleHook, State };
|
|
54
|
-
export { createBarcodeScanner };
|
package/dist/create-worker.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { BarcodeFormat, DetectedBarcode } from 'barcode-detector/ponyfill';
|
|
2
|
-
declare function createWorker({ formats }: {
|
|
3
|
-
formats: BarcodeFormat[];
|
|
4
|
-
}): {
|
|
5
|
-
decode: (imageData: ImageData) => Promise<DetectedBarcode | null>;
|
|
6
|
-
worker: Worker;
|
|
7
|
-
};
|
|
8
|
-
export { createWorker };
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { RenderSize } from './get-video-render-size';
|
|
2
|
-
type RenderOffset = {
|
|
3
|
-
x: number;
|
|
4
|
-
y: number;
|
|
5
|
-
};
|
|
6
|
-
declare function getVideoRenderOffset(video: HTMLVideoElement, renderSize: RenderSize): RenderOffset;
|
|
7
|
-
export type { RenderOffset };
|
|
8
|
-
export { getVideoRenderOffset };
|
package/dist/utils/index.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export * from './get-camera-access';
|
|
2
|
-
export * from './get-scan-area';
|
|
3
|
-
export * from './get-video-render-offset';
|
|
4
|
-
export * from './get-video-render-size';
|
|
5
|
-
export * from './has-camera-access';
|
|
6
|
-
export * from './is-barcode-detector-available';
|
|
7
|
-
export * from './translate-area-to-video-render';
|
|
8
|
-
export * from './translate-area-to-video-source';
|
|
9
|
-
export * from './wait';
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { BarcodeDetector, BarcodeDetectorOptions } from 'barcode-detector/ponyfill';
|
|
2
|
-
declare function isBarcodeDetectorAvailable<T extends object>(value: T): value is {
|
|
3
|
-
BarcodeDetector: {
|
|
4
|
-
new (barcodeDetectorOptions?: BarcodeDetectorOptions): BarcodeDetector;
|
|
5
|
-
} & BarcodeDetector;
|
|
6
|
-
} & T;
|
|
7
|
-
export { isBarcodeDetectorAvailable };
|
package/dist/utils/wait.d.ts
DELETED
package/dist/worker.types.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { BarcodeFormat, DetectedBarcode } from 'barcode-detector/ponyfill';
|
|
2
|
-
type Config = {
|
|
3
|
-
payload: {
|
|
4
|
-
formats: BarcodeFormat[];
|
|
5
|
-
wasmUrl: string;
|
|
6
|
-
};
|
|
7
|
-
type: 'config';
|
|
8
|
-
};
|
|
9
|
-
type DecodeRequest = {
|
|
10
|
-
payload: {
|
|
11
|
-
data: ImageData;
|
|
12
|
-
uuid: string;
|
|
13
|
-
};
|
|
14
|
-
type: 'decode';
|
|
15
|
-
};
|
|
16
|
-
type DecodeResponse = {
|
|
17
|
-
payload: {
|
|
18
|
-
data: DetectedBarcode | null;
|
|
19
|
-
uuid: string;
|
|
20
|
-
};
|
|
21
|
-
type: 'decode';
|
|
22
|
-
};
|
|
23
|
-
type Init = {
|
|
24
|
-
payload: {
|
|
25
|
-
status: 'failure' | 'success';
|
|
26
|
-
};
|
|
27
|
-
type: 'init';
|
|
28
|
-
};
|
|
29
|
-
export type { Config, DecodeRequest, DecodeResponse, Init };
|