@olenbetong/synergi-react 4.2.3 → 4.2.4
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/package.json +7 -7
- package/src/chrome/AppShell/index.tsx +29 -29
- package/src/chrome/openReport.ts +34 -34
- package/src/colorScheme.ts +81 -81
- package/src/components/DateNavigator/index.tsx +186 -186
- package/src/components/FilePond.tsx +312 -312
- package/src/components/ImageEditor/ImageEditor.tsx +348 -348
- package/src/components/ImageEditor/index.ts +9 -9
- package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
- package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
- package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
- package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
- package/src/components/ImageEditor/useActiveTool.ts +21 -21
- package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
- package/src/components/ImageEditor/useImageHistory.ts +197 -197
- package/src/components/ImageEditor/useOpenCV.ts +184 -184
- package/src/components/LinkedCardList/index.scss +95 -95
- package/src/components/MergeProgressDialog/index.tsx +249 -249
- package/src/components/PageBanner/index.tsx +80 -80
- package/src/components/PdfViewer/PdfViewer.tsx +158 -158
- package/src/components/ProgressBar/index.scss +96 -96
- package/src/components/SplitContainer/index.tsx +333 -333
- package/src/components/Toolbar/index.tsx +232 -232
- package/src/designsystemet-web-types.d.ts +24 -24
- package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
- package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
- package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
- package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
- package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
- package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
- package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
- package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
- package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
- package/src/elements/ElementLookup.tsx +123 -123
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -32
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -118
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -103
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -95
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -118
- package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -21
- package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +90 -90
- package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +280 -280
- package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
- package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
- package/src/elements/ElementLookupDialog/index.tsx +226 -226
- package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
- package/src/elements/ProjectSelect.tsx +130 -130
- package/src/elements/index.ts +15 -15
- package/src/filter/index.ts +34 -34
- package/src/i18n.ts +13 -13
- package/src/index.ts +77 -77
- package/src/mui/MuiThemeProvider.tsx +174 -174
- package/src/mui/index.ts +1 -1
- package/src/test/setup.ts +8 -8
|
@@ -1,280 +1,280 @@
|
|
|
1
|
-
import { type RefObject, useEffect, useRef, useState } from "react";
|
|
2
|
-
import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "./decoder";
|
|
3
|
-
import { createZxingDecoder } from "./decoders/zxing";
|
|
4
|
-
import { createDuplicateFilter } from "./duplicateFilter";
|
|
5
|
-
import { getLocalizedString } from "../../../i18n.js";
|
|
6
|
-
|
|
7
|
-
export type ScannerError = { message: string; icon: "unsupported" | "camera" | "generic" };
|
|
8
|
-
|
|
9
|
-
/** Longest side of the frame handed to the decoder. Larger frames cost decode time without helping DataMatrix reads. */
|
|
10
|
-
const MAX_FRAME_SIDE = 720;
|
|
11
|
-
/** Minimum time between two decode attempts. */
|
|
12
|
-
const DECODE_INTERVAL = 100;
|
|
13
|
-
/** A code that stays in view is reported again only after it has been gone this long. */
|
|
14
|
-
const DUPLICATE_FORGET_TIME = 1000;
|
|
15
|
-
/** How long the highlight of a detected code stays on the overlay. */
|
|
16
|
-
const HIGHLIGHT_TIME = 300;
|
|
17
|
-
|
|
18
|
-
const CAMERA_ERROR_NAMES = [
|
|
19
|
-
"NotAllowedError",
|
|
20
|
-
"NotFoundError",
|
|
21
|
-
"NotReadableError",
|
|
22
|
-
"OverconstrainedError",
|
|
23
|
-
"SecurityError",
|
|
24
|
-
"AbortError",
|
|
25
|
-
"TypeError",
|
|
26
|
-
];
|
|
27
|
-
|
|
28
|
-
const defaultDecoder = createZxingDecoder();
|
|
29
|
-
|
|
30
|
-
function videoConstraints(deviceId: string | null): MediaStreamConstraints {
|
|
31
|
-
let iPhone = /iPhone|iPad/.test(navigator.userAgent);
|
|
32
|
-
let size = iPhone ? 1280 : 720;
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
audio: false,
|
|
36
|
-
video: {
|
|
37
|
-
width: { ideal: size },
|
|
38
|
-
height: { ideal: size },
|
|
39
|
-
...(deviceId ? { deviceId: { exact: deviceId } } : { facingMode: { ideal: "environment" } }),
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function scheduleFrame(video: HTMLVideoElement, callback: (now: number) => void) {
|
|
45
|
-
if ("requestVideoFrameCallback" in video) {
|
|
46
|
-
video.requestVideoFrameCallback((now) => callback(now));
|
|
47
|
-
} else {
|
|
48
|
-
requestAnimationFrame(callback);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Drives the camera and the decode loop behind `BarcodeScanner`. The decoder is created from the
|
|
54
|
-
* factory and replaced whenever a different factory is passed, so callers must pass a stable
|
|
55
|
-
* reference (a module-level constant or a memoised value) and change it only to switch engine.
|
|
56
|
-
*/
|
|
57
|
-
export function useScanner(
|
|
58
|
-
videoRef: RefObject<HTMLVideoElement | null>,
|
|
59
|
-
canvasRef: RefObject<HTMLCanvasElement | null>,
|
|
60
|
-
onRead: (barcode: string) => void,
|
|
61
|
-
decoderFactory: DecoderFactory = defaultDecoder,
|
|
62
|
-
) {
|
|
63
|
-
let [isLoading, setIsLoading] = useState(true);
|
|
64
|
-
let [error, setError] = useState<ScannerError | null>(null);
|
|
65
|
-
let [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
|
|
66
|
-
let [deviceId, setDeviceId] = useState<string | null>(null);
|
|
67
|
-
let [engine, setEngine] = useState<string | null>(null);
|
|
68
|
-
let [highlights, setHighlights] = useState<Array<DecodedBarcode["corners"]>>([]);
|
|
69
|
-
|
|
70
|
-
let onReadRef = useRef(onRead);
|
|
71
|
-
let decoderRef = useRef<BarcodeDecoder | null>(null);
|
|
72
|
-
let currentDeviceId = useRef<string | null>(null);
|
|
73
|
-
let highlightTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
74
|
-
|
|
75
|
-
useEffect(() => {
|
|
76
|
-
onReadRef.current = onRead;
|
|
77
|
-
}, [onRead]);
|
|
78
|
-
|
|
79
|
-
// Create the decoder; replaced when a different factory is passed.
|
|
80
|
-
useEffect(() => {
|
|
81
|
-
let cancelled = false;
|
|
82
|
-
let created: BarcodeDecoder | null = null;
|
|
83
|
-
setEngine(null);
|
|
84
|
-
setError(null);
|
|
85
|
-
|
|
86
|
-
decoderFactory()
|
|
87
|
-
.then((decoder) => {
|
|
88
|
-
if (cancelled) {
|
|
89
|
-
decoder.dispose?.();
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
created = decoder;
|
|
93
|
-
decoderRef.current = decoder;
|
|
94
|
-
setEngine(decoder.name);
|
|
95
|
-
})
|
|
96
|
-
.catch((ex: unknown) => {
|
|
97
|
-
if (cancelled) return;
|
|
98
|
-
let message = ex instanceof Error ? ex.message : String(ex);
|
|
99
|
-
setIsLoading(false);
|
|
100
|
-
|
|
101
|
-
if (/WebAssembly/i.test(message)) {
|
|
102
|
-
setError({
|
|
103
|
-
icon: "unsupported",
|
|
104
|
-
message: getLocalizedString(
|
|
105
|
-
"Your browser is not supported. A browser that supports WebAssembly is required.",
|
|
106
|
-
),
|
|
107
|
-
});
|
|
108
|
-
} else {
|
|
109
|
-
setError({ icon: "generic", message });
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
return () => {
|
|
114
|
-
cancelled = true;
|
|
115
|
-
decoderRef.current = null;
|
|
116
|
-
created?.dispose?.();
|
|
117
|
-
};
|
|
118
|
-
}, [decoderFactory]);
|
|
119
|
-
|
|
120
|
-
// Start the camera and the decode loop; restarted when the user switches camera.
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
let video = videoRef.current;
|
|
123
|
-
if (!video) return;
|
|
124
|
-
|
|
125
|
-
let cancelled = false;
|
|
126
|
-
let stream: MediaStream | null = null;
|
|
127
|
-
let frame = document.createElement("canvas");
|
|
128
|
-
let frameContext = frame.getContext("2d", { willReadFrequently: true });
|
|
129
|
-
let isNew = createDuplicateFilter(DUPLICATE_FORGET_TIME);
|
|
130
|
-
let busy = false;
|
|
131
|
-
let lastDecode = 0;
|
|
132
|
-
|
|
133
|
-
function showHighlights(corners: Array<DecodedBarcode["corners"]>) {
|
|
134
|
-
setHighlights(corners);
|
|
135
|
-
|
|
136
|
-
if (highlightTimeout.current) clearTimeout(highlightTimeout.current);
|
|
137
|
-
highlightTimeout.current = setTimeout(() => {
|
|
138
|
-
if (!cancelled) setHighlights([]);
|
|
139
|
-
}, HIGHLIGHT_TIME);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async function decodeCurrentFrame(now: number) {
|
|
143
|
-
let decoder = decoderRef.current;
|
|
144
|
-
if (!decoder || !frameContext || !video || video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) return;
|
|
145
|
-
|
|
146
|
-
let scale = Math.min(1, MAX_FRAME_SIDE / Math.max(video.videoWidth, video.videoHeight));
|
|
147
|
-
let width = Math.round(video.videoWidth * scale);
|
|
148
|
-
let height = Math.round(video.videoHeight * scale);
|
|
149
|
-
if (width === 0 || height === 0) return;
|
|
150
|
-
|
|
151
|
-
if (frame.width !== width || frame.height !== height) {
|
|
152
|
-
frame.width = width;
|
|
153
|
-
frame.height = height;
|
|
154
|
-
}
|
|
155
|
-
frameContext.drawImage(video, 0, 0, width, height);
|
|
156
|
-
|
|
157
|
-
let results = await decoder.decodeFrame(frame);
|
|
158
|
-
if (cancelled || results.length === 0) return;
|
|
159
|
-
|
|
160
|
-
// Highlights are drawn on an overlay sized like the video, so scale them back up.
|
|
161
|
-
let inverse = 1 / scale;
|
|
162
|
-
showHighlights(
|
|
163
|
-
results.map(
|
|
164
|
-
(result) =>
|
|
165
|
-
result.corners.map((point) => ({
|
|
166
|
-
x: point.x * inverse,
|
|
167
|
-
y: point.y * inverse,
|
|
168
|
-
})) as DecodedBarcode["corners"],
|
|
169
|
-
),
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
for (let result of results) {
|
|
173
|
-
if (result.text && isNew(result.text, now)) {
|
|
174
|
-
onReadRef.current(result.text);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function tick(now: number) {
|
|
180
|
-
if (cancelled || !video) return;
|
|
181
|
-
|
|
182
|
-
if (!busy && now - lastDecode >= DECODE_INTERVAL) {
|
|
183
|
-
busy = true;
|
|
184
|
-
lastDecode = now;
|
|
185
|
-
decodeCurrentFrame(now)
|
|
186
|
-
.catch((ex: unknown) => {
|
|
187
|
-
console.warn("Barcode decode failed", ex);
|
|
188
|
-
})
|
|
189
|
-
.finally(() => {
|
|
190
|
-
busy = false;
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
scheduleFrame(video, tick);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async function start() {
|
|
198
|
-
if (!navigator.mediaDevices?.getUserMedia) {
|
|
199
|
-
throw new DOMException("Camera access is not available", "NotFoundError");
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
stream = await navigator.mediaDevices.getUserMedia(videoConstraints(deviceId));
|
|
203
|
-
if (cancelled || !video) return;
|
|
204
|
-
|
|
205
|
-
video.srcObject = stream;
|
|
206
|
-
await video.play();
|
|
207
|
-
if (cancelled) return;
|
|
208
|
-
|
|
209
|
-
let overlay = canvasRef.current;
|
|
210
|
-
if (overlay) {
|
|
211
|
-
overlay.width = video.videoWidth;
|
|
212
|
-
overlay.height = video.videoHeight;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
currentDeviceId.current = stream.getVideoTracks()[0]?.getSettings().deviceId ?? deviceId;
|
|
216
|
-
|
|
217
|
-
// Labels and the full device list are only available once permission has been granted.
|
|
218
|
-
let all = await navigator.mediaDevices.enumerateDevices();
|
|
219
|
-
if (cancelled) return;
|
|
220
|
-
|
|
221
|
-
setDevices(all.filter((device) => device.kind === "videoinput"));
|
|
222
|
-
setIsLoading(false);
|
|
223
|
-
scheduleFrame(video, tick);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
start().catch((ex: unknown) => {
|
|
227
|
-
if (cancelled) return;
|
|
228
|
-
setIsLoading(false);
|
|
229
|
-
|
|
230
|
-
let name = ex instanceof DOMException || ex instanceof Error ? ex.name : "";
|
|
231
|
-
let message = ex instanceof Error ? ex.message : String(ex);
|
|
232
|
-
|
|
233
|
-
if (CAMERA_ERROR_NAMES.includes(name) || /camera|video|device|media/i.test(message)) {
|
|
234
|
-
setError({ icon: "camera", message: getLocalizedString("No camera available") });
|
|
235
|
-
} else {
|
|
236
|
-
setError({ icon: "generic", message });
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
return () => {
|
|
241
|
-
cancelled = true;
|
|
242
|
-
if (highlightTimeout.current) clearTimeout(highlightTimeout.current);
|
|
243
|
-
stream?.getTracks().forEach((track) => track.stop());
|
|
244
|
-
if (video) video.srcObject = null;
|
|
245
|
-
};
|
|
246
|
-
}, [videoRef, canvasRef, deviceId]);
|
|
247
|
-
|
|
248
|
-
// Draw the highlights of detected codes on the overlay canvas.
|
|
249
|
-
useEffect(() => {
|
|
250
|
-
let overlay = canvasRef.current;
|
|
251
|
-
if (!overlay) return;
|
|
252
|
-
|
|
253
|
-
let context = overlay.getContext("2d");
|
|
254
|
-
if (!context) return;
|
|
255
|
-
|
|
256
|
-
context.clearRect(0, 0, overlay.width, overlay.height);
|
|
257
|
-
context.fillStyle = "rgba(254,180,32,0.3)";
|
|
258
|
-
|
|
259
|
-
for (let corners of highlights) {
|
|
260
|
-
context.beginPath();
|
|
261
|
-
context.moveTo(corners[0].x, corners[0].y);
|
|
262
|
-
context.lineTo(corners[1].x, corners[1].y);
|
|
263
|
-
context.lineTo(corners[2].x, corners[2].y);
|
|
264
|
-
context.lineTo(corners[3].x, corners[3].y);
|
|
265
|
-
context.closePath();
|
|
266
|
-
context.fill();
|
|
267
|
-
}
|
|
268
|
-
}, [canvasRef, highlights]);
|
|
269
|
-
|
|
270
|
-
function setNextDevice() {
|
|
271
|
-
if (devices.length <= 1) return;
|
|
272
|
-
|
|
273
|
-
let index = devices.findIndex((device) => device.deviceId === currentDeviceId.current);
|
|
274
|
-
let next = devices[(index + 1) % devices.length];
|
|
275
|
-
|
|
276
|
-
if (next) setDeviceId(next.deviceId);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return { devices, engine, error, isLoading, setNextDevice };
|
|
280
|
-
}
|
|
1
|
+
import { type RefObject, useEffect, useRef, useState } from "react";
|
|
2
|
+
import type { BarcodeDecoder, DecodedBarcode, DecoderFactory } from "./decoder";
|
|
3
|
+
import { createZxingDecoder } from "./decoders/zxing";
|
|
4
|
+
import { createDuplicateFilter } from "./duplicateFilter";
|
|
5
|
+
import { getLocalizedString } from "../../../i18n.js";
|
|
6
|
+
|
|
7
|
+
export type ScannerError = { message: string; icon: "unsupported" | "camera" | "generic" };
|
|
8
|
+
|
|
9
|
+
/** Longest side of the frame handed to the decoder. Larger frames cost decode time without helping DataMatrix reads. */
|
|
10
|
+
const MAX_FRAME_SIDE = 720;
|
|
11
|
+
/** Minimum time between two decode attempts. */
|
|
12
|
+
const DECODE_INTERVAL = 100;
|
|
13
|
+
/** A code that stays in view is reported again only after it has been gone this long. */
|
|
14
|
+
const DUPLICATE_FORGET_TIME = 1000;
|
|
15
|
+
/** How long the highlight of a detected code stays on the overlay. */
|
|
16
|
+
const HIGHLIGHT_TIME = 300;
|
|
17
|
+
|
|
18
|
+
const CAMERA_ERROR_NAMES = [
|
|
19
|
+
"NotAllowedError",
|
|
20
|
+
"NotFoundError",
|
|
21
|
+
"NotReadableError",
|
|
22
|
+
"OverconstrainedError",
|
|
23
|
+
"SecurityError",
|
|
24
|
+
"AbortError",
|
|
25
|
+
"TypeError",
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
const defaultDecoder = createZxingDecoder();
|
|
29
|
+
|
|
30
|
+
function videoConstraints(deviceId: string | null): MediaStreamConstraints {
|
|
31
|
+
let iPhone = /iPhone|iPad/.test(navigator.userAgent);
|
|
32
|
+
let size = iPhone ? 1280 : 720;
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
audio: false,
|
|
36
|
+
video: {
|
|
37
|
+
width: { ideal: size },
|
|
38
|
+
height: { ideal: size },
|
|
39
|
+
...(deviceId ? { deviceId: { exact: deviceId } } : { facingMode: { ideal: "environment" } }),
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function scheduleFrame(video: HTMLVideoElement, callback: (now: number) => void) {
|
|
45
|
+
if ("requestVideoFrameCallback" in video) {
|
|
46
|
+
video.requestVideoFrameCallback((now) => callback(now));
|
|
47
|
+
} else {
|
|
48
|
+
requestAnimationFrame(callback);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Drives the camera and the decode loop behind `BarcodeScanner`. The decoder is created from the
|
|
54
|
+
* factory and replaced whenever a different factory is passed, so callers must pass a stable
|
|
55
|
+
* reference (a module-level constant or a memoised value) and change it only to switch engine.
|
|
56
|
+
*/
|
|
57
|
+
export function useScanner(
|
|
58
|
+
videoRef: RefObject<HTMLVideoElement | null>,
|
|
59
|
+
canvasRef: RefObject<HTMLCanvasElement | null>,
|
|
60
|
+
onRead: (barcode: string) => void,
|
|
61
|
+
decoderFactory: DecoderFactory = defaultDecoder,
|
|
62
|
+
) {
|
|
63
|
+
let [isLoading, setIsLoading] = useState(true);
|
|
64
|
+
let [error, setError] = useState<ScannerError | null>(null);
|
|
65
|
+
let [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
|
|
66
|
+
let [deviceId, setDeviceId] = useState<string | null>(null);
|
|
67
|
+
let [engine, setEngine] = useState<string | null>(null);
|
|
68
|
+
let [highlights, setHighlights] = useState<Array<DecodedBarcode["corners"]>>([]);
|
|
69
|
+
|
|
70
|
+
let onReadRef = useRef(onRead);
|
|
71
|
+
let decoderRef = useRef<BarcodeDecoder | null>(null);
|
|
72
|
+
let currentDeviceId = useRef<string | null>(null);
|
|
73
|
+
let highlightTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
74
|
+
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
onReadRef.current = onRead;
|
|
77
|
+
}, [onRead]);
|
|
78
|
+
|
|
79
|
+
// Create the decoder; replaced when a different factory is passed.
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
let cancelled = false;
|
|
82
|
+
let created: BarcodeDecoder | null = null;
|
|
83
|
+
setEngine(null);
|
|
84
|
+
setError(null);
|
|
85
|
+
|
|
86
|
+
decoderFactory()
|
|
87
|
+
.then((decoder) => {
|
|
88
|
+
if (cancelled) {
|
|
89
|
+
decoder.dispose?.();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
created = decoder;
|
|
93
|
+
decoderRef.current = decoder;
|
|
94
|
+
setEngine(decoder.name);
|
|
95
|
+
})
|
|
96
|
+
.catch((ex: unknown) => {
|
|
97
|
+
if (cancelled) return;
|
|
98
|
+
let message = ex instanceof Error ? ex.message : String(ex);
|
|
99
|
+
setIsLoading(false);
|
|
100
|
+
|
|
101
|
+
if (/WebAssembly/i.test(message)) {
|
|
102
|
+
setError({
|
|
103
|
+
icon: "unsupported",
|
|
104
|
+
message: getLocalizedString(
|
|
105
|
+
"Your browser is not supported. A browser that supports WebAssembly is required.",
|
|
106
|
+
),
|
|
107
|
+
});
|
|
108
|
+
} else {
|
|
109
|
+
setError({ icon: "generic", message });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return () => {
|
|
114
|
+
cancelled = true;
|
|
115
|
+
decoderRef.current = null;
|
|
116
|
+
created?.dispose?.();
|
|
117
|
+
};
|
|
118
|
+
}, [decoderFactory]);
|
|
119
|
+
|
|
120
|
+
// Start the camera and the decode loop; restarted when the user switches camera.
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
let video = videoRef.current;
|
|
123
|
+
if (!video) return;
|
|
124
|
+
|
|
125
|
+
let cancelled = false;
|
|
126
|
+
let stream: MediaStream | null = null;
|
|
127
|
+
let frame = document.createElement("canvas");
|
|
128
|
+
let frameContext = frame.getContext("2d", { willReadFrequently: true });
|
|
129
|
+
let isNew = createDuplicateFilter(DUPLICATE_FORGET_TIME);
|
|
130
|
+
let busy = false;
|
|
131
|
+
let lastDecode = 0;
|
|
132
|
+
|
|
133
|
+
function showHighlights(corners: Array<DecodedBarcode["corners"]>) {
|
|
134
|
+
setHighlights(corners);
|
|
135
|
+
|
|
136
|
+
if (highlightTimeout.current) clearTimeout(highlightTimeout.current);
|
|
137
|
+
highlightTimeout.current = setTimeout(() => {
|
|
138
|
+
if (!cancelled) setHighlights([]);
|
|
139
|
+
}, HIGHLIGHT_TIME);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function decodeCurrentFrame(now: number) {
|
|
143
|
+
let decoder = decoderRef.current;
|
|
144
|
+
if (!decoder || !frameContext || !video || video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) return;
|
|
145
|
+
|
|
146
|
+
let scale = Math.min(1, MAX_FRAME_SIDE / Math.max(video.videoWidth, video.videoHeight));
|
|
147
|
+
let width = Math.round(video.videoWidth * scale);
|
|
148
|
+
let height = Math.round(video.videoHeight * scale);
|
|
149
|
+
if (width === 0 || height === 0) return;
|
|
150
|
+
|
|
151
|
+
if (frame.width !== width || frame.height !== height) {
|
|
152
|
+
frame.width = width;
|
|
153
|
+
frame.height = height;
|
|
154
|
+
}
|
|
155
|
+
frameContext.drawImage(video, 0, 0, width, height);
|
|
156
|
+
|
|
157
|
+
let results = await decoder.decodeFrame(frame);
|
|
158
|
+
if (cancelled || results.length === 0) return;
|
|
159
|
+
|
|
160
|
+
// Highlights are drawn on an overlay sized like the video, so scale them back up.
|
|
161
|
+
let inverse = 1 / scale;
|
|
162
|
+
showHighlights(
|
|
163
|
+
results.map(
|
|
164
|
+
(result) =>
|
|
165
|
+
result.corners.map((point) => ({
|
|
166
|
+
x: point.x * inverse,
|
|
167
|
+
y: point.y * inverse,
|
|
168
|
+
})) as DecodedBarcode["corners"],
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
for (let result of results) {
|
|
173
|
+
if (result.text && isNew(result.text, now)) {
|
|
174
|
+
onReadRef.current(result.text);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function tick(now: number) {
|
|
180
|
+
if (cancelled || !video) return;
|
|
181
|
+
|
|
182
|
+
if (!busy && now - lastDecode >= DECODE_INTERVAL) {
|
|
183
|
+
busy = true;
|
|
184
|
+
lastDecode = now;
|
|
185
|
+
decodeCurrentFrame(now)
|
|
186
|
+
.catch((ex: unknown) => {
|
|
187
|
+
console.warn("Barcode decode failed", ex);
|
|
188
|
+
})
|
|
189
|
+
.finally(() => {
|
|
190
|
+
busy = false;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
scheduleFrame(video, tick);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function start() {
|
|
198
|
+
if (!navigator.mediaDevices?.getUserMedia) {
|
|
199
|
+
throw new DOMException("Camera access is not available", "NotFoundError");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
stream = await navigator.mediaDevices.getUserMedia(videoConstraints(deviceId));
|
|
203
|
+
if (cancelled || !video) return;
|
|
204
|
+
|
|
205
|
+
video.srcObject = stream;
|
|
206
|
+
await video.play();
|
|
207
|
+
if (cancelled) return;
|
|
208
|
+
|
|
209
|
+
let overlay = canvasRef.current;
|
|
210
|
+
if (overlay) {
|
|
211
|
+
overlay.width = video.videoWidth;
|
|
212
|
+
overlay.height = video.videoHeight;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
currentDeviceId.current = stream.getVideoTracks()[0]?.getSettings().deviceId ?? deviceId;
|
|
216
|
+
|
|
217
|
+
// Labels and the full device list are only available once permission has been granted.
|
|
218
|
+
let all = await navigator.mediaDevices.enumerateDevices();
|
|
219
|
+
if (cancelled) return;
|
|
220
|
+
|
|
221
|
+
setDevices(all.filter((device) => device.kind === "videoinput"));
|
|
222
|
+
setIsLoading(false);
|
|
223
|
+
scheduleFrame(video, tick);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
start().catch((ex: unknown) => {
|
|
227
|
+
if (cancelled) return;
|
|
228
|
+
setIsLoading(false);
|
|
229
|
+
|
|
230
|
+
let name = ex instanceof DOMException || ex instanceof Error ? ex.name : "";
|
|
231
|
+
let message = ex instanceof Error ? ex.message : String(ex);
|
|
232
|
+
|
|
233
|
+
if (CAMERA_ERROR_NAMES.includes(name) || /camera|video|device|media/i.test(message)) {
|
|
234
|
+
setError({ icon: "camera", message: getLocalizedString("No camera available") });
|
|
235
|
+
} else {
|
|
236
|
+
setError({ icon: "generic", message });
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
return () => {
|
|
241
|
+
cancelled = true;
|
|
242
|
+
if (highlightTimeout.current) clearTimeout(highlightTimeout.current);
|
|
243
|
+
stream?.getTracks().forEach((track) => track.stop());
|
|
244
|
+
if (video) video.srcObject = null;
|
|
245
|
+
};
|
|
246
|
+
}, [videoRef, canvasRef, deviceId]);
|
|
247
|
+
|
|
248
|
+
// Draw the highlights of detected codes on the overlay canvas.
|
|
249
|
+
useEffect(() => {
|
|
250
|
+
let overlay = canvasRef.current;
|
|
251
|
+
if (!overlay) return;
|
|
252
|
+
|
|
253
|
+
let context = overlay.getContext("2d");
|
|
254
|
+
if (!context) return;
|
|
255
|
+
|
|
256
|
+
context.clearRect(0, 0, overlay.width, overlay.height);
|
|
257
|
+
context.fillStyle = "rgba(254,180,32,0.3)";
|
|
258
|
+
|
|
259
|
+
for (let corners of highlights) {
|
|
260
|
+
context.beginPath();
|
|
261
|
+
context.moveTo(corners[0].x, corners[0].y);
|
|
262
|
+
context.lineTo(corners[1].x, corners[1].y);
|
|
263
|
+
context.lineTo(corners[2].x, corners[2].y);
|
|
264
|
+
context.lineTo(corners[3].x, corners[3].y);
|
|
265
|
+
context.closePath();
|
|
266
|
+
context.fill();
|
|
267
|
+
}
|
|
268
|
+
}, [canvasRef, highlights]);
|
|
269
|
+
|
|
270
|
+
function setNextDevice() {
|
|
271
|
+
if (devices.length <= 1) return;
|
|
272
|
+
|
|
273
|
+
let index = devices.findIndex((device) => device.deviceId === currentDeviceId.current);
|
|
274
|
+
let next = devices[(index + 1) % devices.length];
|
|
275
|
+
|
|
276
|
+
if (next) setDeviceId(next.deviceId);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return { devices, engine, error, isLoading, setNextDevice };
|
|
280
|
+
}
|