@nddeps/barcode-scanner 0.5.1 → 0.6.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/dist/index.d.ts +113 -4
- package/dist/index.js +146 -128
- 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,4 +1,113 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
declare type Context = {
|
|
7
|
+
state: State;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
declare function createBarcodeScanner(video: HTMLVideoElement, { debug, formats, getScanArea, handleDecodeFailure, handleDecodeSuccess, lifecycle, scanRate, }?: Options): Promise<{
|
|
11
|
+
decode: (imageData: ImageData) => Promise<DetectedBarcode | null>;
|
|
12
|
+
destroy: () => Promise<void>;
|
|
13
|
+
pause: () => Promise<void>;
|
|
14
|
+
start: ({ facingMode, ...rest }?: {
|
|
15
|
+
facingMode?: "environment" | "user";
|
|
16
|
+
handleDecodeFailure?: DecodeFailureHandler;
|
|
17
|
+
handleDecodeSuccess?: DecodeSuccessHandler;
|
|
18
|
+
}) => Promise<void>;
|
|
19
|
+
startCallbackProcessed: boolean;
|
|
20
|
+
state: State;
|
|
21
|
+
stop: () => Promise<void>;
|
|
22
|
+
}>;
|
|
23
|
+
export { createBarcodeScanner }
|
|
24
|
+
export default createBarcodeScanner;
|
|
25
|
+
|
|
26
|
+
export declare function createWorker({ formats }: {
|
|
27
|
+
formats: BarcodeFormat[];
|
|
28
|
+
}): {
|
|
29
|
+
decode: (imageData: ImageData) => Promise<DetectedBarcode | null>;
|
|
30
|
+
worker: Worker;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
declare type DecodeFailureHandler = () => Promise<void> | void;
|
|
34
|
+
|
|
35
|
+
declare type DecodeSuccessHandler = (data: string, area: ScanArea) => Promise<void> | void;
|
|
36
|
+
|
|
37
|
+
export declare function getCameraAccess(): Promise<boolean>;
|
|
38
|
+
|
|
39
|
+
export declare function getScanArea(video: HTMLVideoElement): ScanArea;
|
|
40
|
+
|
|
41
|
+
export declare function getVideoRenderOffset(video: HTMLVideoElement, renderSize: RenderSize): RenderOffset;
|
|
42
|
+
|
|
43
|
+
export declare function getVideoRenderSize(video: HTMLVideoElement): RenderSize;
|
|
44
|
+
|
|
45
|
+
export declare function hasCameraAccess(): Promise<boolean>;
|
|
46
|
+
|
|
47
|
+
export declare function isBarcodeDetectorAvailable<T extends object>(value: T): value is {
|
|
48
|
+
BarcodeDetector: {
|
|
49
|
+
new (barcodeDetectorOptions?: BarcodeDetectorOptions): BarcodeDetector;
|
|
50
|
+
} & BarcodeDetector;
|
|
51
|
+
} & T;
|
|
52
|
+
|
|
53
|
+
declare type Lifecycle = {
|
|
54
|
+
onBeforeCreate?: LifecycleHook;
|
|
55
|
+
onBeforeDecode?: LifecycleHook;
|
|
56
|
+
onBeforePause?: LifecycleHook;
|
|
57
|
+
onBeforeStart?: LifecycleHook;
|
|
58
|
+
onBeforeStop?: LifecycleHook;
|
|
59
|
+
onCreate?: LifecycleHook;
|
|
60
|
+
onDecode?: LifecycleHook;
|
|
61
|
+
onPause?: LifecycleHook;
|
|
62
|
+
onStart?: LifecycleHook;
|
|
63
|
+
onStop?: LifecycleHook;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
declare type LifecycleHook = (ctx: Context) => void;
|
|
67
|
+
|
|
68
|
+
declare type Options = {
|
|
69
|
+
debug?: boolean;
|
|
70
|
+
formats?: BarcodeFormat[];
|
|
71
|
+
getScanArea?: (video: HTMLVideoElement) => ScanArea;
|
|
72
|
+
handleDecodeFailure?: DecodeFailureHandler;
|
|
73
|
+
handleDecodeSuccess?: DecodeSuccessHandler;
|
|
74
|
+
lifecycle?: Lifecycle;
|
|
75
|
+
scanRate?: number;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export declare type RenderOffset = {
|
|
79
|
+
x: number;
|
|
80
|
+
y: number;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export declare type RenderSize = {
|
|
84
|
+
height: number;
|
|
85
|
+
width: number;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export declare type ScanArea = {
|
|
89
|
+
height: number;
|
|
90
|
+
width: number;
|
|
91
|
+
x: number;
|
|
92
|
+
y: number;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
declare type State = {
|
|
96
|
+
decodeFrameTs: number;
|
|
97
|
+
isDecodeFrameProcessed: boolean;
|
|
98
|
+
isDestroyed: boolean;
|
|
99
|
+
isVideoActive: boolean;
|
|
100
|
+
isVideoPaused: boolean;
|
|
101
|
+
isWorkerLoadFailure: boolean;
|
|
102
|
+
scanArea: ScanArea;
|
|
103
|
+
scanRate: number;
|
|
104
|
+
video: HTMLVideoElement;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export declare function translateAreaToVideoRender(video: HTMLVideoElement, area: ScanArea): ScanArea;
|
|
108
|
+
|
|
109
|
+
export declare function translateAreaToVideoSource(video: HTMLVideoElement, area: ScanArea): ScanArea;
|
|
110
|
+
|
|
111
|
+
export declare function wait(ms: number): Promise<unknown>;
|
|
112
|
+
|
|
113
|
+
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,174 +1,176 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const k = "decode-timeout", R = "worker-load-failure";
|
|
2
|
+
const T = "worker-load-timeout", C = "" + new URL("zxing-reader.wasm", import.meta.url).href;
|
|
3
3
|
function H({ formats: e }) {
|
|
4
|
-
const
|
|
4
|
+
const n = new Worker(new URL(
|
|
5
5
|
/* @vite-ignore */
|
|
6
6
|
"" + new URL("worker.js", import.meta.url).href,
|
|
7
7
|
import.meta.url
|
|
8
|
-
), { type: "module" }),
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
}, 32e3),
|
|
12
|
-
|
|
8
|
+
), { type: "module" }), i = new Promise((a, r) => {
|
|
9
|
+
const o = setTimeout(() => {
|
|
10
|
+
n.removeEventListener("message", f), r(new Error(T));
|
|
11
|
+
}, 32e3), f = ({ data: { payload: c, type: m } }) => {
|
|
12
|
+
m === "init" && (clearTimeout(o), n.removeEventListener("message", f), c.status === "success" ? a(!0) : r(new Error(R)));
|
|
13
13
|
};
|
|
14
|
-
|
|
14
|
+
n.addEventListener("message", f), n.postMessage({
|
|
15
15
|
payload: {
|
|
16
16
|
formats: e,
|
|
17
|
-
wasmUrl:
|
|
17
|
+
wasmUrl: C
|
|
18
18
|
},
|
|
19
19
|
type: "config"
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
|
-
async function
|
|
23
|
-
await
|
|
24
|
-
const
|
|
25
|
-
return new Promise((
|
|
22
|
+
async function s(a) {
|
|
23
|
+
await i;
|
|
24
|
+
const r = `${performance.now()}-${Math.random().toString(36).slice(2)}`;
|
|
25
|
+
return new Promise((o, f) => {
|
|
26
26
|
const c = setTimeout(() => {
|
|
27
|
-
|
|
28
|
-
}, 16e3),
|
|
29
|
-
p !== "decode" || g.uuid !==
|
|
27
|
+
n.removeEventListener("message", m), f(new Error(k));
|
|
28
|
+
}, 16e3), m = ({ data: { payload: g, type: p } }) => {
|
|
29
|
+
p !== "decode" || g.uuid !== r || (clearTimeout(c), n.removeEventListener("message", m), o(g.data));
|
|
30
30
|
};
|
|
31
|
-
|
|
31
|
+
n.addEventListener("message", m), n.postMessage({
|
|
32
32
|
payload: {
|
|
33
|
-
data:
|
|
34
|
-
uuid:
|
|
33
|
+
data: a,
|
|
34
|
+
uuid: r
|
|
35
35
|
},
|
|
36
36
|
type: "decode"
|
|
37
37
|
});
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
return {
|
|
41
|
-
decode:
|
|
42
|
-
worker:
|
|
41
|
+
decode: s,
|
|
42
|
+
worker: n
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
|
-
async function
|
|
45
|
+
async function V() {
|
|
46
46
|
try {
|
|
47
47
|
return (await navigator.permissions.query({ name: "camera" })).state === "granted";
|
|
48
48
|
} catch {
|
|
49
|
-
return (await navigator.mediaDevices.enumerateDevices()).filter((
|
|
49
|
+
return (await navigator.mediaDevices.enumerateDevices()).filter((i) => i.deviceId && i.kind === "videoinput").length > 0;
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
async function
|
|
53
|
-
if (await
|
|
52
|
+
async function _() {
|
|
53
|
+
if (await V())
|
|
54
54
|
return !0;
|
|
55
55
|
try {
|
|
56
|
-
const
|
|
57
|
-
for (const
|
|
58
|
-
|
|
56
|
+
const n = (await navigator.mediaDevices.getUserMedia({ video: !0 })).getTracks();
|
|
57
|
+
for (const i of n)
|
|
58
|
+
i.stop();
|
|
59
59
|
return !0;
|
|
60
60
|
} catch {
|
|
61
61
|
return !1;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
-
function
|
|
65
|
-
const
|
|
64
|
+
function I(e) {
|
|
65
|
+
const n = Math.round(0.6666666666666666 * Math.min(e.videoWidth, e.videoHeight));
|
|
66
66
|
return {
|
|
67
|
-
height:
|
|
68
|
-
width:
|
|
69
|
-
x: Math.round((e.videoWidth -
|
|
70
|
-
y: Math.round((e.videoHeight -
|
|
67
|
+
height: n,
|
|
68
|
+
width: n,
|
|
69
|
+
x: Math.round((e.videoWidth - n) / 2),
|
|
70
|
+
y: Math.round((e.videoHeight - n) / 2)
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
|
-
function W(e,
|
|
74
|
-
const
|
|
75
|
-
(
|
|
73
|
+
function W(e, n) {
|
|
74
|
+
const i = window.getComputedStyle(e), [s, a] = i.objectPosition.split(" ").map(
|
|
75
|
+
(r, o) => r.endsWith("%") ? (o === 0 ? e.offsetWidth - n.width : e.offsetHeight - n.height) * parseFloat(r) / 100 : parseFloat(r)
|
|
76
76
|
);
|
|
77
77
|
return {
|
|
78
|
-
x:
|
|
79
|
-
y:
|
|
78
|
+
x: s,
|
|
79
|
+
y: a
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
|
-
function
|
|
83
|
-
const
|
|
84
|
-
switch (
|
|
82
|
+
function b(e) {
|
|
83
|
+
const n = window.getComputedStyle(e), i = { height: e.offsetHeight, width: e.offsetWidth }, s = i.width / i.height, a = { height: e.videoHeight, width: e.videoWidth }, r = a.width / a.height;
|
|
84
|
+
switch (n.objectFit) {
|
|
85
85
|
case "contain":
|
|
86
86
|
return {
|
|
87
|
-
height:
|
|
88
|
-
width:
|
|
87
|
+
height: r < s ? e.offsetHeight : e.offsetWidth / r,
|
|
88
|
+
width: r < s ? e.offsetHeight * r : e.offsetWidth
|
|
89
89
|
};
|
|
90
90
|
case "cover":
|
|
91
91
|
return {
|
|
92
|
-
height:
|
|
93
|
-
width:
|
|
92
|
+
height: r > s ? e.offsetHeight : e.offsetWidth / r,
|
|
93
|
+
width: r > s ? e.offsetHeight * r : e.offsetWidth
|
|
94
94
|
};
|
|
95
95
|
case "fill":
|
|
96
|
-
return a;
|
|
97
|
-
case "none":
|
|
98
96
|
return i;
|
|
97
|
+
case "none":
|
|
98
|
+
return a;
|
|
99
99
|
case "scale-down":
|
|
100
100
|
return {
|
|
101
101
|
height: Math.min(
|
|
102
|
-
|
|
102
|
+
r < s ? e.offsetHeight : e.offsetWidth / r,
|
|
103
103
|
e.videoHeight
|
|
104
104
|
),
|
|
105
105
|
width: Math.min(
|
|
106
|
-
|
|
106
|
+
r < s ? e.offsetHeight * r : e.offsetWidth,
|
|
107
107
|
e.videoWidth
|
|
108
108
|
)
|
|
109
109
|
};
|
|
110
110
|
default:
|
|
111
|
-
return
|
|
111
|
+
return a;
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
function U(e) {
|
|
115
115
|
return "BarcodeDetector" in e;
|
|
116
116
|
}
|
|
117
|
-
function
|
|
118
|
-
const
|
|
117
|
+
function B(e, n) {
|
|
118
|
+
const i = /scaleX\(-1\)/.test(e.style.transform), s = b(e), a = W(e, s), r = i ? e.videoWidth - n.x - n.width : n.x, o = n.y;
|
|
119
119
|
return {
|
|
120
|
-
height:
|
|
121
|
-
width:
|
|
122
|
-
x:
|
|
123
|
-
y:
|
|
120
|
+
height: n.height / e.videoHeight * s.height,
|
|
121
|
+
width: n.width / e.videoWidth * s.width,
|
|
122
|
+
x: r / e.videoWidth * s.width + a.x,
|
|
123
|
+
y: o / e.videoHeight * s.height + a.y
|
|
124
124
|
};
|
|
125
125
|
}
|
|
126
|
-
function
|
|
127
|
-
const
|
|
126
|
+
function j(e, n) {
|
|
127
|
+
const i = /scaleX\(-1\)/.test(e.style.transform), s = b(e), a = W(e, s), r = i ? s.width - (n.x - a.x) - n.width : n.x - a.x, o = n.y - a.y, f = e.videoHeight / s.height, c = e.videoWidth / s.width;
|
|
128
128
|
return {
|
|
129
|
-
height:
|
|
130
|
-
width:
|
|
131
|
-
x:
|
|
132
|
-
y:
|
|
129
|
+
height: n.height * f,
|
|
130
|
+
width: n.width * c,
|
|
131
|
+
x: r * c,
|
|
132
|
+
y: o * f
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
|
-
function
|
|
136
|
-
return new Promise((
|
|
135
|
+
function q(e) {
|
|
136
|
+
return new Promise((n) => setTimeout(n, e));
|
|
137
137
|
}
|
|
138
|
-
async function
|
|
139
|
-
debug:
|
|
140
|
-
formats:
|
|
141
|
-
getScanArea:
|
|
142
|
-
handleDecodeFailure:
|
|
143
|
-
handleDecodeSuccess:
|
|
144
|
-
lifecycle:
|
|
145
|
-
scanRate:
|
|
138
|
+
async function K(e, {
|
|
139
|
+
debug: n,
|
|
140
|
+
formats: i = ["qr_code"],
|
|
141
|
+
getScanArea: s = I,
|
|
142
|
+
handleDecodeFailure: a,
|
|
143
|
+
handleDecodeSuccess: r,
|
|
144
|
+
lifecycle: o = {},
|
|
145
|
+
scanRate: f = 24
|
|
146
146
|
} = {}) {
|
|
147
147
|
if (!(e instanceof HTMLVideoElement))
|
|
148
148
|
throw new Error("video is not a HTMLVideoElement");
|
|
149
|
-
if (!(
|
|
149
|
+
if (!(r instanceof Function))
|
|
150
150
|
throw new Error("handleDecodeSuccess is not a function");
|
|
151
|
-
if (!(
|
|
151
|
+
if (!(a instanceof Function))
|
|
152
152
|
throw new Error("handleDecodeFailure is not a function");
|
|
153
|
-
const c = document.createElement("canvas"),
|
|
154
|
-
if (!
|
|
153
|
+
const c = document.createElement("canvas"), m = c.getContext("2d", { willReadFrequently: !0 });
|
|
154
|
+
if (!m)
|
|
155
155
|
throw new Error("canvas context is not supported");
|
|
156
|
-
const { decode: g, worker: p } = H({ formats:
|
|
156
|
+
const { decode: g, worker: p } = H({ formats: i }), t = {
|
|
157
157
|
decodeFrameTs: performance.now(),
|
|
158
158
|
isDecodeFrameProcessed: !1,
|
|
159
159
|
isDestroyed: !1,
|
|
160
160
|
isVideoActive: !1,
|
|
161
161
|
isVideoPaused: !1,
|
|
162
162
|
isWorkerLoadFailure: !1,
|
|
163
|
-
scanArea:
|
|
164
|
-
scanRate:
|
|
163
|
+
scanArea: s(e),
|
|
164
|
+
scanRate: f,
|
|
165
165
|
video: e
|
|
166
|
-
},
|
|
167
|
-
t.video.autoplay = !0, t.video.disablePictureInPicture = !0, t.video.hidden = !1, t.video.muted = !0, t.video.playsInline = !0,
|
|
168
|
-
|
|
166
|
+
}, w = { state: t }, D = e.requestVideoFrameCallback?.bind(e) ?? requestAnimationFrame;
|
|
167
|
+
t.video.autoplay = !0, t.video.disablePictureInPicture = !0, t.video.hidden = !1, t.video.muted = !0, t.video.playsInline = !0, o.onCreate && o.onCreate(w);
|
|
168
|
+
let E = !1;
|
|
169
|
+
document.addEventListener("barcode-scanner:beforestart", () => E = !0), document.addEventListener("barcode-scanner:start", () => E = !1);
|
|
170
|
+
function S(d, l = () => {
|
|
169
171
|
}) {
|
|
170
|
-
|
|
171
|
-
async function
|
|
172
|
+
D(u);
|
|
173
|
+
async function u() {
|
|
172
174
|
if (t.isDestroyed || t.isVideoActive === !1)
|
|
173
175
|
return;
|
|
174
176
|
if (
|
|
@@ -177,10 +179,10 @@ async function q(e, {
|
|
|
177
179
|
t.isDecodeFrameProcessed || // Skip if the video is not ready
|
|
178
180
|
t.video.readyState <= 1
|
|
179
181
|
) {
|
|
180
|
-
|
|
182
|
+
D(u);
|
|
181
183
|
return;
|
|
182
184
|
}
|
|
183
|
-
t.isDecodeFrameProcessed = !0, t.scanArea =
|
|
185
|
+
t.isDecodeFrameProcessed = !0, t.scanArea = s(t.video), o.onBeforeDecode && o.onBeforeDecode(w), c.height = t.scanArea.height, c.width = t.scanArea.width, m.clearRect(0, 0, c.width, c.height), m.drawImage(
|
|
184
186
|
t.video,
|
|
185
187
|
t.scanArea.x,
|
|
186
188
|
t.scanArea.y,
|
|
@@ -191,76 +193,92 @@ async function q(e, {
|
|
|
191
193
|
c.width,
|
|
192
194
|
c.height
|
|
193
195
|
);
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
+
const v = m.getImageData(0, 0, c.width, c.height);
|
|
197
|
+
n && window.dispatchEvent(
|
|
196
198
|
new CustomEvent("barcode-scanner:decode-frame", {
|
|
197
199
|
detail: {
|
|
198
|
-
imageData:
|
|
200
|
+
imageData: v
|
|
199
201
|
}
|
|
200
202
|
})
|
|
201
203
|
);
|
|
202
204
|
try {
|
|
203
|
-
const
|
|
204
|
-
if (
|
|
205
|
-
const
|
|
206
|
-
height: Math.max(...
|
|
207
|
-
width: Math.max(...
|
|
208
|
-
x: Math.min(...
|
|
209
|
-
y: Math.min(...
|
|
205
|
+
const h = await g(v);
|
|
206
|
+
if (h) {
|
|
207
|
+
const A = h.cornerPoints.map((M) => M.x), y = h.cornerPoints.map((M) => M.y), x = {
|
|
208
|
+
height: Math.max(...y) - Math.min(...y),
|
|
209
|
+
width: Math.max(...A) - Math.min(...A),
|
|
210
|
+
x: Math.min(...A) + t.scanArea.x,
|
|
211
|
+
y: Math.min(...y) + t.scanArea.y
|
|
210
212
|
};
|
|
211
|
-
await Promise.resolve(
|
|
213
|
+
await Promise.resolve(d(h.rawValue, x));
|
|
212
214
|
} else
|
|
213
215
|
await Promise.resolve(l());
|
|
214
|
-
} catch (
|
|
215
|
-
console.warn("Failed to decode barcode"),
|
|
216
|
+
} catch (h) {
|
|
217
|
+
console.warn("Failed to decode barcode"), h && (console.error(h), h instanceof Error && (h.cause === R || h.cause === T) && (t.isWorkerLoadFailure = !0));
|
|
216
218
|
} finally {
|
|
217
|
-
|
|
219
|
+
o.onDecode && o.onDecode(w), t.isWorkerLoadFailure === !1 && (t.isDecodeFrameProcessed = !1, t.decodeFrameTs = performance.now(), D(u));
|
|
218
220
|
}
|
|
219
221
|
}
|
|
220
222
|
}
|
|
221
|
-
function F() {
|
|
222
|
-
t.isDestroyed || (M(), p.terminate(), t.isDestroyed = !0);
|
|
223
|
-
}
|
|
224
223
|
async function P() {
|
|
225
|
-
t.
|
|
224
|
+
t.isDestroyed || (await O(), p.terminate(), t.isDestroyed = !0);
|
|
226
225
|
}
|
|
227
|
-
async function
|
|
228
|
-
|
|
226
|
+
async function L() {
|
|
227
|
+
t.isVideoActive === !1 || t.isVideoPaused || t.isDestroyed || (E && await new Promise((d) => {
|
|
228
|
+
const l = setTimeout(() => {
|
|
229
|
+
document.removeEventListener("barcode-scanner:start", u), d(null);
|
|
230
|
+
}, 8e3), u = () => {
|
|
231
|
+
clearTimeout(l), document.removeEventListener("barcode-scanner:start", u), d(null);
|
|
232
|
+
};
|
|
233
|
+
document.addEventListener("barcode-scanner:start", u);
|
|
234
|
+
}), o.onBeforePause && o.onBeforePause(w), t.video.srcObject instanceof MediaStream && (t.video.srcObject.getTracks().forEach((d) => d.stop()), t.video.srcObject = null), t.isVideoPaused = !0, o.onPause && o.onPause(w));
|
|
235
|
+
}
|
|
236
|
+
async function F({
|
|
237
|
+
facingMode: d = "environment",
|
|
229
238
|
...l
|
|
230
239
|
} = {}) {
|
|
231
|
-
const
|
|
232
|
-
if (!
|
|
240
|
+
const u = l.handleDecodeSuccess ?? r, v = l.handleDecodeFailure ?? a;
|
|
241
|
+
if (!u)
|
|
233
242
|
throw new Error("handleDecodeSuccess is required");
|
|
234
|
-
if (
|
|
243
|
+
if (document.dispatchEvent(new CustomEvent("barcode-scanner:beforestart")), o.onBeforeStart && o.onBeforeStart(w), !await _())
|
|
235
244
|
throw new Error("No camera access");
|
|
236
245
|
t.video.srcObject instanceof MediaStream || (t.video.srcObject = await navigator.mediaDevices.getUserMedia({
|
|
237
246
|
video: {
|
|
238
|
-
facingMode:
|
|
247
|
+
facingMode: d
|
|
239
248
|
}
|
|
240
|
-
}), await t.video.play(), t.isVideoActive = !0, t.isVideoPaused = !1, t.scanArea =
|
|
249
|
+
}), await t.video.play(), t.isVideoActive = !0, t.isVideoPaused = !1, t.scanArea = s(t.video), t.video.style.transform = d === "user" ? "scaleX(-1)" : "none", document.dispatchEvent(new CustomEvent("barcode-scanner:start")), o.onStart && o.onStart(w), S(u, v));
|
|
241
250
|
}
|
|
242
|
-
async function
|
|
243
|
-
t.
|
|
251
|
+
async function O() {
|
|
252
|
+
t.isDestroyed || (E && await new Promise((d) => {
|
|
253
|
+
const l = setTimeout(() => {
|
|
254
|
+
document.removeEventListener("barcode-scanner:start", u), d(null);
|
|
255
|
+
}, 8e3), u = () => {
|
|
256
|
+
clearTimeout(l), document.removeEventListener("barcode-scanner:start", u), d(null);
|
|
257
|
+
};
|
|
258
|
+
document.addEventListener("barcode-scanner:start", u);
|
|
259
|
+
}), o.onBeforeStop && o.onBeforeStop(w), 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 = "", o.onStop && o.onStop(w));
|
|
244
260
|
}
|
|
245
261
|
return {
|
|
246
262
|
decode: g,
|
|
247
|
-
destroy:
|
|
248
|
-
pause:
|
|
249
|
-
start:
|
|
263
|
+
destroy: P,
|
|
264
|
+
pause: L,
|
|
265
|
+
start: F,
|
|
266
|
+
startCallbackProcessed: E,
|
|
250
267
|
state: t,
|
|
251
|
-
stop:
|
|
268
|
+
stop: O
|
|
252
269
|
};
|
|
253
270
|
}
|
|
254
271
|
export {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
272
|
+
K as createBarcodeScanner,
|
|
273
|
+
H as createWorker,
|
|
274
|
+
K as default,
|
|
275
|
+
_ as getCameraAccess,
|
|
276
|
+
I as getScanArea,
|
|
259
277
|
W as getVideoRenderOffset,
|
|
260
|
-
|
|
261
|
-
|
|
278
|
+
b as getVideoRenderSize,
|
|
279
|
+
V as hasCameraAccess,
|
|
262
280
|
U as isBarcodeDetectorAvailable,
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
281
|
+
B as translateAreaToVideoRender,
|
|
282
|
+
j as translateAreaToVideoSource,
|
|
283
|
+
q as wait
|
|
266
284
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nddeps/barcode-scanner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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 };
|