@olenbetong/synergi-react 3.2.2 → 4.0.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/es/chrome/AppShell/index.d.ts +10 -2
- package/es/chrome/AppShell/index.js +13 -97
- package/es/chrome/openReport.d.ts +22 -0
- package/es/chrome/openReport.js +21 -0
- package/es/colorScheme.d.ts +4 -4
- package/es/components/Toolbar/index.d.ts +20 -11
- package/es/components/Toolbar/index.js +47 -6
- package/es/elements/ElementLookup.d.ts +7 -2
- package/es/elements/ElementLookup.js +4 -6
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoder.d.ts +32 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.d.ts +28 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.js +81 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/native.d.ts +26 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/native.js +70 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.d.ts +20 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +69 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +103 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.d.ts +6 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.js +19 -0
- package/es/elements/ElementLookupDialog/BarcodeScanner/index.d.ts +14 -7
- package/es/elements/ElementLookupDialog/BarcodeScanner/index.js +10 -6
- package/es/elements/ElementLookupDialog/BarcodeScanner/useScanner.d.ts +14 -12
- package/es/elements/ElementLookupDialog/BarcodeScanner/useScanner.js +222 -116
- package/es/elements/ElementLookupDialog/index.d.ts +6 -0
- package/es/elements/ElementLookupDialog/index.js +2 -2
- package/es/index.d.ts +3 -4
- package/es/index.js +5 -4
- package/package.json +9 -8
- package/src/chrome/AppShell/index.tsx +13 -134
- package/src/chrome/openReport.ts +34 -0
- package/src/colorScheme.ts +4 -4
- package/src/components/Toolbar/index.tsx +65 -14
- package/src/elements/ElementLookup.tsx +12 -5
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -0
- package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +29 -14
- package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +246 -136
- package/src/elements/ElementLookupDialog/index.tsx +12 -4
- package/src/index.ts +6 -37
- package/es/chrome/AppMenu/AppMenu.css +0 -648
- package/es/chrome/AppMenu/AppMenu.d.ts +0 -56
- package/es/chrome/AppMenu/AppMenu.js +0 -145
- package/es/chrome/AppMenu/AppMenuColorScheme.d.ts +0 -2
- package/es/chrome/AppMenu/AppMenuColorScheme.js +0 -18
- package/es/chrome/AppMenu/AppMenuFavoriteButton.d.ts +0 -18
- package/es/chrome/AppMenu/AppMenuFavoriteButton.js +0 -11
- package/es/chrome/AppMenu/AppMenuHeader.d.ts +0 -24
- package/es/chrome/AppMenu/AppMenuHeader.js +0 -18
- package/es/chrome/AppMenu/AppMenuList.d.ts +0 -18
- package/es/chrome/AppMenu/AppMenuList.js +0 -39
- package/es/chrome/AppMenu/AppMenuRail.d.ts +0 -26
- package/es/chrome/AppMenu/AppMenuRail.js +0 -30
- package/es/chrome/AppMenu/AppMenuToggle.d.ts +0 -15
- package/es/chrome/AppMenu/AppMenuToggle.js +0 -14
- package/es/chrome/AppMenu/AppMenuUserMenu.d.ts +0 -32
- package/es/chrome/AppMenu/AppMenuUserMenu.js +0 -110
- package/es/chrome/AppMenu/developerCategories.d.ts +0 -24
- package/es/chrome/AppMenu/developerCategories.js +0 -116
- package/es/chrome/AppMenu/icons.d.ts +0 -15
- package/es/chrome/AppMenu/icons.js +0 -13
- package/es/chrome/AppMenu/index.d.ts +0 -12
- package/es/chrome/AppMenu/index.js +0 -10
- package/es/chrome/AppMenu/menuEntries.d.ts +0 -51
- package/es/chrome/AppMenu/menuEntries.js +0 -93
- package/es/chrome/AppMenu/types.d.ts +0 -43
- package/es/chrome/AppMenu/useFavorites.d.ts +0 -13
- package/es/chrome/AppMenu/useFavorites.js +0 -28
- package/es/chrome/AppMenu/useRovingTabIndex.d.ts +0 -28
- package/es/chrome/AppMenu/useRovingTabIndex.js +0 -114
- package/es/chrome/AppMenu/useServiceWorkerUpdate.d.ts +0 -16
- package/es/chrome/AppMenu/useServiceWorkerUpdate.js +0 -62
- package/es/report/ReportLauncher.css +0 -147
- package/es/report/ReportLauncher.d.ts +0 -55
- package/es/report/ReportLauncher.js +0 -142
- package/es/report/ReportLauncher.test.js +0 -231
- package/es/report/ReportParameters.css +0 -29
- package/es/report/ReportParameters.d.ts +0 -46
- package/es/report/ReportParameters.js +0 -65
- package/es/report/ReportParameters.test.d.ts +0 -1
- package/es/report/ReportParameters.test.js +0 -84
- package/es/report/index.d.ts +0 -2
- package/es/report/index.js +0 -2
- package/src/chrome/AppMenu/AppMenu.css +0 -648
- package/src/chrome/AppMenu/AppMenu.tsx +0 -285
- package/src/chrome/AppMenu/AppMenuColorScheme.tsx +0 -41
- package/src/chrome/AppMenu/AppMenuFavoriteButton.tsx +0 -47
- package/src/chrome/AppMenu/AppMenuHeader.tsx +0 -79
- package/src/chrome/AppMenu/AppMenuList.tsx +0 -89
- package/src/chrome/AppMenu/AppMenuRail.tsx +0 -117
- package/src/chrome/AppMenu/AppMenuToggle.tsx +0 -51
- package/src/chrome/AppMenu/AppMenuUserMenu.tsx +0 -190
- package/src/chrome/AppMenu/developerCategories.ts +0 -142
- package/src/chrome/AppMenu/icons.tsx +0 -76
- package/src/chrome/AppMenu/index.ts +0 -17
- package/src/chrome/AppMenu/menuEntries.ts +0 -128
- package/src/chrome/AppMenu/types.ts +0 -45
- package/src/chrome/AppMenu/useFavorites.ts +0 -40
- package/src/chrome/AppMenu/useRovingTabIndex.ts +0 -130
- package/src/chrome/AppMenu/useServiceWorkerUpdate.ts +0 -68
- package/src/report/ReportLauncher.css +0 -147
- package/src/report/ReportLauncher.test.tsx +0 -281
- package/src/report/ReportLauncher.tsx +0 -420
- package/src/report/ReportParameters.css +0 -29
- package/src/report/ReportParameters.test.tsx +0 -133
- package/src/report/ReportParameters.tsx +0 -198
- package/src/report/index.ts +0 -2
- /package/es/{chrome/AppMenu/types.js → elements/ElementLookupDialog/BarcodeScanner/decoder.js} +0 -0
- /package/es/{report/ReportLauncher.test.d.ts → elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts} +0 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const defaultFrameOptions = {
|
|
2
|
+
formats: ["DataMatrix"],
|
|
3
|
+
tryHarder: false,
|
|
4
|
+
tryRotate: true,
|
|
5
|
+
tryInvert: false,
|
|
6
|
+
tryDownscale: true,
|
|
7
|
+
maxNumberOfSymbols: 4,
|
|
8
|
+
};
|
|
9
|
+
const defaultImageOptions = {
|
|
10
|
+
formats: ["DataMatrix"],
|
|
11
|
+
tryHarder: true,
|
|
12
|
+
tryRotate: true,
|
|
13
|
+
tryInvert: true,
|
|
14
|
+
tryDownscale: true,
|
|
15
|
+
maxNumberOfSymbols: 8,
|
|
16
|
+
};
|
|
17
|
+
export function mapZxingResult(result) {
|
|
18
|
+
let { topLeft, topRight, bottomRight, bottomLeft } = result.position;
|
|
19
|
+
return {
|
|
20
|
+
text: result.text,
|
|
21
|
+
format: result.format,
|
|
22
|
+
corners: [
|
|
23
|
+
{ x: topLeft.x, y: topLeft.y },
|
|
24
|
+
{ x: topRight.x, y: topRight.y },
|
|
25
|
+
{ x: bottomRight.x, y: bottomRight.y },
|
|
26
|
+
{ x: bottomLeft.x, y: bottomLeft.y },
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Decoder backed by zxing-cpp compiled to WebAssembly (`zxing-wasm`). Free, runs everywhere
|
|
32
|
+
* WebAssembly does, including iOS Safari. The module is imported when the factory runs, so apps
|
|
33
|
+
* that never open the scanner do not download it.
|
|
34
|
+
*/
|
|
35
|
+
export function createZxingDecoder(options = {}) {
|
|
36
|
+
let frameOptions = { ...defaultFrameOptions, ...options.frameOptions };
|
|
37
|
+
let imageOptions = { ...defaultImageOptions, ...options.imageOptions };
|
|
38
|
+
return async () => {
|
|
39
|
+
if (typeof WebAssembly === "undefined") {
|
|
40
|
+
throw new Error("WebAssembly is not supported");
|
|
41
|
+
}
|
|
42
|
+
let zxing = await import("zxing-wasm/reader");
|
|
43
|
+
let { wasmUrl } = options;
|
|
44
|
+
if (wasmUrl) {
|
|
45
|
+
zxing.prepareZXingModule({
|
|
46
|
+
overrides: {
|
|
47
|
+
locateFile: (path, prefix) => (path.endsWith(".wasm") ? wasmUrl : prefix + path),
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
// Instantiate up front so a broken download surfaces as a scanner error instead of on the first frame.
|
|
52
|
+
await zxing.prepareZXingModule({ fireImmediately: true });
|
|
53
|
+
return {
|
|
54
|
+
name: "zxing",
|
|
55
|
+
async decodeFrame(frame) {
|
|
56
|
+
let context = frame.getContext("2d", { willReadFrequently: true });
|
|
57
|
+
if (!context)
|
|
58
|
+
return [];
|
|
59
|
+
let imageData = context.getImageData(0, 0, frame.width, frame.height);
|
|
60
|
+
let results = await zxing.readBarcodes(imageData, frameOptions);
|
|
61
|
+
return results.filter((result) => result.isValid).map(mapZxingResult);
|
|
62
|
+
},
|
|
63
|
+
async decodeImage(file) {
|
|
64
|
+
let results = await zxing.readBarcodes(file, imageOptions);
|
|
65
|
+
return results.filter((result) => result.isValid).map(mapZxingResult);
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { mapDbrResult } from "./decoders/dbr";
|
|
3
|
+
import { mapNativeResult } from "./decoders/native";
|
|
4
|
+
import { mapZxingResult } from "./decoders/zxing";
|
|
5
|
+
import { createDuplicateFilter } from "./duplicateFilter";
|
|
6
|
+
const GS = "";
|
|
7
|
+
const GIAI = `8004${"7043210"}${GS}${"12345"}`;
|
|
8
|
+
describe("result mapping", () => {
|
|
9
|
+
it("maps a zxing result to corners in reading order", () => {
|
|
10
|
+
let mapped = mapZxingResult({
|
|
11
|
+
text: GIAI,
|
|
12
|
+
format: "DataMatrix",
|
|
13
|
+
isValid: true,
|
|
14
|
+
position: {
|
|
15
|
+
topLeft: { x: 10, y: 20 },
|
|
16
|
+
topRight: { x: 110, y: 20 },
|
|
17
|
+
bottomRight: { x: 110, y: 120 },
|
|
18
|
+
bottomLeft: { x: 10, y: 120 },
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
expect(mapped.text).toBe(GIAI);
|
|
22
|
+
expect(mapped.format).toBe("DataMatrix");
|
|
23
|
+
expect(mapped.corners).toEqual([
|
|
24
|
+
{ x: 10, y: 20 },
|
|
25
|
+
{ x: 110, y: 20 },
|
|
26
|
+
{ x: 110, y: 120 },
|
|
27
|
+
{ x: 10, y: 120 },
|
|
28
|
+
]);
|
|
29
|
+
});
|
|
30
|
+
it("maps a native result from its corner points", () => {
|
|
31
|
+
let result = {
|
|
32
|
+
rawValue: GIAI,
|
|
33
|
+
format: "data_matrix",
|
|
34
|
+
cornerPoints: [
|
|
35
|
+
{ x: 1, y: 2 },
|
|
36
|
+
{ x: 3, y: 2 },
|
|
37
|
+
{ x: 3, y: 4 },
|
|
38
|
+
{ x: 1, y: 4 },
|
|
39
|
+
],
|
|
40
|
+
boundingBox: { left: 0, top: 0, right: 0, bottom: 0 },
|
|
41
|
+
};
|
|
42
|
+
expect(mapNativeResult(result)).toEqual({
|
|
43
|
+
text: GIAI,
|
|
44
|
+
format: "data_matrix",
|
|
45
|
+
corners: [
|
|
46
|
+
{ x: 1, y: 2 },
|
|
47
|
+
{ x: 3, y: 2 },
|
|
48
|
+
{ x: 3, y: 4 },
|
|
49
|
+
{ x: 1, y: 4 },
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
it("falls back to the bounding box when a native result has no corner points", () => {
|
|
54
|
+
let result = {
|
|
55
|
+
rawValue: GIAI,
|
|
56
|
+
format: "data_matrix",
|
|
57
|
+
cornerPoints: [],
|
|
58
|
+
boundingBox: { left: 5, top: 6, right: 15, bottom: 16 },
|
|
59
|
+
};
|
|
60
|
+
expect(mapNativeResult(result).corners).toEqual([
|
|
61
|
+
{ x: 5, y: 6 },
|
|
62
|
+
{ x: 15, y: 6 },
|
|
63
|
+
{ x: 15, y: 16 },
|
|
64
|
+
{ x: 5, y: 16 },
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
it("maps a Dynamsoft result from its localization result", () => {
|
|
68
|
+
let mapped = mapDbrResult({
|
|
69
|
+
BarcodeText: GIAI,
|
|
70
|
+
BarcodeFormat: 134217728,
|
|
71
|
+
LocalizationResult: { X1: 1, Y1: 2, X2: 3, Y2: 2, X3: 3, Y3: 4, X4: 1, Y4: 4 },
|
|
72
|
+
});
|
|
73
|
+
expect(mapped.text).toBe(GIAI);
|
|
74
|
+
expect(mapped.corners).toEqual([
|
|
75
|
+
{ x: 1, y: 2 },
|
|
76
|
+
{ x: 3, y: 2 },
|
|
77
|
+
{ x: 3, y: 4 },
|
|
78
|
+
{ x: 1, y: 4 },
|
|
79
|
+
]);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
describe("createDuplicateFilter", () => {
|
|
83
|
+
it("reports a code once while it stays in view", () => {
|
|
84
|
+
let isNew = createDuplicateFilter(1000);
|
|
85
|
+
expect(isNew("a", 0)).toBe(true);
|
|
86
|
+
expect(isNew("a", 100)).toBe(false);
|
|
87
|
+
expect(isNew("a", 900)).toBe(false);
|
|
88
|
+
// Still in view: every sighting refreshes the timer.
|
|
89
|
+
expect(isNew("a", 1500)).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
it("reports a code again once it has been out of view for the forget time", () => {
|
|
92
|
+
let isNew = createDuplicateFilter(1000);
|
|
93
|
+
expect(isNew("a", 0)).toBe(true);
|
|
94
|
+
expect(isNew("a", 1001)).toBe(true);
|
|
95
|
+
});
|
|
96
|
+
it("tracks codes independently", () => {
|
|
97
|
+
let isNew = createDuplicateFilter(1000);
|
|
98
|
+
expect(isNew("a", 0)).toBe(true);
|
|
99
|
+
expect(isNew("b", 0)).toBe(true);
|
|
100
|
+
expect(isNew("a", 10)).toBe(false);
|
|
101
|
+
expect(isNew("b", 10)).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Suppresses repeated reads of a code that stays in view. A value is reported once, and again only
|
|
3
|
+
* after it has been out of view for `forgetTime` milliseconds — every sighting refreshes the timer,
|
|
4
|
+
* matching the `duplicateForgetTime` behaviour of the Dynamsoft scanner the apps were built around.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createDuplicateFilter(forgetTime: number): (text: string, now: number) => boolean;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Suppresses repeated reads of a code that stays in view. A value is reported once, and again only
|
|
3
|
+
* after it has been out of view for `forgetTime` milliseconds — every sighting refreshes the timer,
|
|
4
|
+
* matching the `duplicateForgetTime` behaviour of the Dynamsoft scanner the apps were built around.
|
|
5
|
+
*/
|
|
6
|
+
export function createDuplicateFilter(forgetTime) {
|
|
7
|
+
let lastSeen = new Map();
|
|
8
|
+
return function isNew(text, now) {
|
|
9
|
+
let previous = lastSeen.get(text);
|
|
10
|
+
lastSeen.set(text, now);
|
|
11
|
+
if (lastSeen.size > 64) {
|
|
12
|
+
for (let [key, seen] of lastSeen) {
|
|
13
|
+
if (now - seen > forgetTime)
|
|
14
|
+
lastSeen.delete(key);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return previous === undefined || now - previous > forgetTime;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import type { DecoderFactory } from "./decoder";
|
|
3
|
+
export type { BarcodeDecoder, DecodedBarcode, DecodedPoint, DecoderFactory } from "./decoder";
|
|
4
|
+
export { createDbrDecoder, type DbrDecoderOptions } from "./decoders/dbr";
|
|
5
|
+
export { createNativeDecoder, isNativeDecoderAvailable } from "./decoders/native";
|
|
6
|
+
export { createZxingDecoder, type ZxingDecoderOptions } from "./decoders/zxing";
|
|
7
7
|
export type BarcodeScannerProps = {
|
|
8
8
|
onRead: (barcode: string) => void;
|
|
9
|
+
/**
|
|
10
|
+
* Engine used to decode the camera frames. Defaults to zxing-wasm. Pass a stable reference and
|
|
11
|
+
* change it only to switch engine — the decoder is recreated whenever it changes.
|
|
12
|
+
*/
|
|
13
|
+
decoder?: DecoderFactory;
|
|
14
|
+
/** Extra controls rendered in the scanner footer, next to the engine name — e.g. an engine picker. */
|
|
15
|
+
actions?: ReactNode;
|
|
9
16
|
};
|
|
10
|
-
export declare function BarcodeScanner({ onRead }: BarcodeScannerProps): import("react").JSX.Element;
|
|
17
|
+
export declare function BarcodeScanner({ onRead, decoder, actions }: BarcodeScannerProps): import("react").JSX.Element;
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import SwitchCameraOutlined from "@mui/icons-material/SwitchCameraOutlined";
|
|
3
|
-
import { Box, IconButton } from "@mui/material";
|
|
3
|
+
import { Box, IconButton, Typography } from "@mui/material";
|
|
4
4
|
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
5
5
|
import { useRef } from "react";
|
|
6
6
|
import { ErrorIcon } from "./Error";
|
|
7
7
|
import { useScanner } from "./useScanner";
|
|
8
|
+
export { createDbrDecoder } from "./decoders/dbr";
|
|
9
|
+
export { createNativeDecoder, isNativeDecoderAvailable } from "./decoders/native";
|
|
10
|
+
export { createZxingDecoder } from "./decoders/zxing";
|
|
8
11
|
const frameSx = {
|
|
9
12
|
position: "absolute",
|
|
10
13
|
bottom: 0,
|
|
@@ -13,16 +16,17 @@ const frameSx = {
|
|
|
13
16
|
top: 0,
|
|
14
17
|
height: "100%",
|
|
15
18
|
width: "100%",
|
|
19
|
+
objectFit: "contain",
|
|
16
20
|
};
|
|
17
|
-
export function BarcodeScanner({ onRead }) {
|
|
18
|
-
let
|
|
21
|
+
export function BarcodeScanner({ onRead, decoder, actions }) {
|
|
22
|
+
let video = useRef(null);
|
|
19
23
|
let canvas = useRef(null);
|
|
20
|
-
let { devices, error, setNextDevice } = useScanner(
|
|
24
|
+
let { devices, engine, error, setNextDevice } = useScanner(video, canvas, onRead, decoder);
|
|
21
25
|
return (_jsxs(Box, { sx: {
|
|
22
26
|
position: "relative",
|
|
23
27
|
bgcolor: "black",
|
|
24
28
|
height: "100%",
|
|
25
|
-
}, children: [_jsxs(Box, { sx: { height: "100%" },
|
|
29
|
+
}, children: [_jsxs(Box, { sx: { height: "100%" }, children: [_jsx(Box, { component: "video", playsInline: true, muted: true, autoPlay: true, sx: frameSx, ref: video }), _jsx(Box, { component: "canvas", sx: frameSx, ref: canvas }), error && _jsx(ErrorIcon, { ...error })] }), _jsxs(Box, { sx: {
|
|
26
30
|
display: "flex",
|
|
27
31
|
alignItems: "center",
|
|
28
32
|
justifyContent: "space-between",
|
|
@@ -33,5 +37,5 @@ export function BarcodeScanner({ onRead }) {
|
|
|
33
37
|
py: 1,
|
|
34
38
|
px: 3,
|
|
35
39
|
color: "white",
|
|
36
|
-
}, children: [devices.length <= 1 && _jsx("span", {}), devices.length > 1 && (_jsx(IconButton, { color: "inherit", "aria-label": getLocalizedString("Switch camera"), onClick: () => setNextDevice(), size: "large", children: _jsx(SwitchCameraOutlined, {}) }))] })] }));
|
|
40
|
+
}, children: [devices.length <= 1 && _jsx("span", {}), devices.length > 1 && (_jsx(IconButton, { color: "inherit", "aria-label": getLocalizedString("Switch camera"), onClick: () => setNextDevice(), size: "large", children: _jsx(SwitchCameraOutlined, {}) })), _jsxs(Box, { sx: { display: "flex", alignItems: "center", gap: 1 }, children: [engine && (_jsx(Typography, { variant: "caption", sx: { opacity: 0.7 }, "data-testid": "barcode-engine", children: engine })), actions] })] })] }));
|
|
37
41
|
}
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import type BarcodeReaderType from "dynamsoft-javascript-barcode";
|
|
2
1
|
import { type RefObject } from "react";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
2
|
+
import type { DecoderFactory } from "./decoder";
|
|
3
|
+
export type ScannerError = {
|
|
4
|
+
message: string;
|
|
5
|
+
icon: "unsupported" | "camera" | "generic";
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Drives the camera and the decode loop behind `BarcodeScanner`. The decoder is created from the
|
|
9
|
+
* factory and replaced whenever a different factory is passed, so callers must pass a stable
|
|
10
|
+
* reference (a module-level constant or a memoised value) and change it only to switch engine.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useScanner(videoRef: RefObject<HTMLVideoElement | null>, canvasRef: RefObject<HTMLCanvasElement | null>, onRead: (barcode: string) => void, decoderFactory?: DecoderFactory): {
|
|
13
|
+
devices: MediaDeviceInfo[];
|
|
14
|
+
engine: string | null;
|
|
15
|
+
error: ScannerError | null;
|
|
14
16
|
isLoading: boolean;
|
|
15
17
|
setNextDevice: () => void;
|
|
16
18
|
};
|
|
@@ -1,135 +1,241 @@
|
|
|
1
1
|
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
2
|
-
// import BarcodeReader from "dynamsoft-javascript-barcode";
|
|
3
2
|
import { useEffect, useRef, useState } from "react";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
import { createZxingDecoder } from "./decoders/zxing";
|
|
4
|
+
import { createDuplicateFilter } from "./duplicateFilter";
|
|
5
|
+
/** Longest side of the frame handed to the decoder. Larger frames cost decode time without helping DataMatrix reads. */
|
|
6
|
+
const MAX_FRAME_SIDE = 720;
|
|
7
|
+
/** Minimum time between two decode attempts. */
|
|
8
|
+
const DECODE_INTERVAL = 100;
|
|
9
|
+
/** A code that stays in view is reported again only after it has been gone this long. */
|
|
10
|
+
const DUPLICATE_FORGET_TIME = 1000;
|
|
11
|
+
/** How long the highlight of a detected code stays on the overlay. */
|
|
12
|
+
const HIGHLIGHT_TIME = 300;
|
|
13
|
+
const CAMERA_ERROR_NAMES = [
|
|
14
|
+
"NotAllowedError",
|
|
15
|
+
"NotFoundError",
|
|
16
|
+
"NotReadableError",
|
|
17
|
+
"OverconstrainedError",
|
|
18
|
+
"SecurityError",
|
|
19
|
+
"AbortError",
|
|
20
|
+
"TypeError",
|
|
21
|
+
];
|
|
22
|
+
const defaultDecoder = createZxingDecoder();
|
|
23
|
+
function videoConstraints(deviceId) {
|
|
24
|
+
let iPhone = /iPhone|iPad/.test(navigator.userAgent);
|
|
25
|
+
let size = iPhone ? 1280 : 720;
|
|
26
|
+
return {
|
|
27
|
+
audio: false,
|
|
28
|
+
video: {
|
|
29
|
+
width: { ideal: size },
|
|
30
|
+
height: { ideal: size },
|
|
31
|
+
...(deviceId ? { deviceId: { exact: deviceId } } : { facingMode: { ideal: "environment" } }),
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function scheduleFrame(video, callback) {
|
|
36
|
+
if ("requestVideoFrameCallback" in video) {
|
|
37
|
+
video.requestVideoFrameCallback((now) => callback(now));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
requestAnimationFrame(callback);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Drives the camera and the decode loop behind `BarcodeScanner`. The decoder is created from the
|
|
45
|
+
* factory and replaced whenever a different factory is passed, so callers must pass a stable
|
|
46
|
+
* reference (a module-level constant or a memoised value) and change it only to switch engine.
|
|
47
|
+
*/
|
|
48
|
+
export function useScanner(videoRef, canvasRef, onRead, decoderFactory = defaultDecoder) {
|
|
49
|
+
let [isLoading, setIsLoading] = useState(true);
|
|
50
|
+
let [error, setError] = useState(null);
|
|
51
|
+
let [devices, setDevices] = useState([]);
|
|
52
|
+
let [deviceId, setDeviceId] = useState(null);
|
|
53
|
+
let [engine, setEngine] = useState(null);
|
|
54
|
+
let [highlights, setHighlights] = useState([]);
|
|
55
|
+
let onReadRef = useRef(onRead);
|
|
56
|
+
let decoderRef = useRef(null);
|
|
57
|
+
let currentDeviceId = useRef(null);
|
|
58
|
+
let highlightTimeout = useRef(null);
|
|
19
59
|
useEffect(() => {
|
|
20
60
|
onReadRef.current = onRead;
|
|
21
61
|
}, [onRead]);
|
|
62
|
+
// Create the decoder; replaced when a different factory is passed.
|
|
22
63
|
useEffect(() => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
width: { ideal: iPhone ? 1280 : 720 },
|
|
33
|
-
height: { ideal: iPhone ? 1280 : 720 },
|
|
34
|
-
facingMode: { ideal: "environment" },
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
runtimeSettings: {
|
|
38
|
-
mBarcodeFormatIds: BarcodeReader.EnumBarcodeFormat.DATAMATRIX,
|
|
39
|
-
mAntiDamageLevel: 3,
|
|
40
|
-
mDeblurLevel: 0,
|
|
41
|
-
},
|
|
42
|
-
onFrameRead: (results) => {
|
|
43
|
-
const newRects = [];
|
|
44
|
-
for (let result of results) {
|
|
45
|
-
newRects.push(result.LocalizationResult);
|
|
46
|
-
}
|
|
47
|
-
if (newRects.length > 0) {
|
|
48
|
-
setRects((rects) => [...rects, ...newRects]);
|
|
49
|
-
setTimeout(() => {
|
|
50
|
-
if (!cancel) {
|
|
51
|
-
setRects((rects) => rects.slice(newRects.length));
|
|
52
|
-
}
|
|
53
|
-
}, 300);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
onNewCodeRead: (txt) => {
|
|
57
|
-
if (txt) {
|
|
58
|
-
onReadRef.current(txt);
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
scanner.current
|
|
63
|
-
.open()
|
|
64
|
-
.then((videoDeviceInfo) => {
|
|
65
|
-
if (cancel === false) {
|
|
66
|
-
const { all, current } = videoDeviceInfo;
|
|
67
|
-
currentDevice.current = 0;
|
|
68
|
-
if (current?.deviceId) {
|
|
69
|
-
for (let i = 0; i < all.length; i++) {
|
|
70
|
-
if (all[i].deviceId === current.deviceId) {
|
|
71
|
-
currentDevice.current = i;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
setDevices(videoDeviceInfo);
|
|
76
|
-
setIsLoading(false);
|
|
64
|
+
let cancelled = false;
|
|
65
|
+
let created = null;
|
|
66
|
+
setEngine(null);
|
|
67
|
+
setError(null);
|
|
68
|
+
decoderFactory()
|
|
69
|
+
.then((decoder) => {
|
|
70
|
+
if (cancelled) {
|
|
71
|
+
decoder.dispose?.();
|
|
72
|
+
return;
|
|
77
73
|
}
|
|
74
|
+
created = decoder;
|
|
75
|
+
decoderRef.current = decoder;
|
|
76
|
+
setEngine(decoder.name);
|
|
78
77
|
})
|
|
79
78
|
.catch((ex) => {
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
/Media/i.test(error) ||
|
|
93
|
-
/agent/.test(error) ||
|
|
94
|
-
/found/.test(error))
|
|
95
|
-
setError({
|
|
96
|
-
icon: "camera",
|
|
97
|
-
message: getLocalizedString("No camera available"),
|
|
98
|
-
});
|
|
99
|
-
else {
|
|
100
|
-
setError({ icon: "generic", message: error });
|
|
101
|
-
}
|
|
79
|
+
if (cancelled)
|
|
80
|
+
return;
|
|
81
|
+
let message = ex instanceof Error ? ex.message : String(ex);
|
|
82
|
+
setIsLoading(false);
|
|
83
|
+
if (/WebAssembly/i.test(message)) {
|
|
84
|
+
setError({
|
|
85
|
+
icon: "unsupported",
|
|
86
|
+
message: getLocalizedString("Your browser is not supported. A browser that supports WebAssembly is required."),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
setError({ icon: "generic", message });
|
|
102
91
|
}
|
|
103
92
|
});
|
|
104
93
|
return () => {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
94
|
+
cancelled = true;
|
|
95
|
+
decoderRef.current = null;
|
|
96
|
+
created?.dispose?.();
|
|
109
97
|
};
|
|
110
|
-
}, [
|
|
98
|
+
}, [decoderFactory]);
|
|
99
|
+
// Start the camera and the decode loop; restarted when the user switches camera.
|
|
111
100
|
useEffect(() => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
101
|
+
let video = videoRef.current;
|
|
102
|
+
if (!video)
|
|
103
|
+
return;
|
|
104
|
+
let cancelled = false;
|
|
105
|
+
let stream = null;
|
|
106
|
+
let frame = document.createElement("canvas");
|
|
107
|
+
let frameContext = frame.getContext("2d", { willReadFrequently: true });
|
|
108
|
+
let isNew = createDuplicateFilter(DUPLICATE_FORGET_TIME);
|
|
109
|
+
let busy = false;
|
|
110
|
+
let lastDecode = 0;
|
|
111
|
+
function showHighlights(corners) {
|
|
112
|
+
setHighlights(corners);
|
|
113
|
+
if (highlightTimeout.current)
|
|
114
|
+
clearTimeout(highlightTimeout.current);
|
|
115
|
+
highlightTimeout.current = setTimeout(() => {
|
|
116
|
+
if (!cancelled)
|
|
117
|
+
setHighlights([]);
|
|
118
|
+
}, HIGHLIGHT_TIME);
|
|
119
|
+
}
|
|
120
|
+
async function decodeCurrentFrame(now) {
|
|
121
|
+
let decoder = decoderRef.current;
|
|
122
|
+
if (!decoder || !frameContext || !video || video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA)
|
|
123
|
+
return;
|
|
124
|
+
let scale = Math.min(1, MAX_FRAME_SIDE / Math.max(video.videoWidth, video.videoHeight));
|
|
125
|
+
let width = Math.round(video.videoWidth * scale);
|
|
126
|
+
let height = Math.round(video.videoHeight * scale);
|
|
127
|
+
if (width === 0 || height === 0)
|
|
128
|
+
return;
|
|
129
|
+
if (frame.width !== width || frame.height !== height) {
|
|
130
|
+
frame.width = width;
|
|
131
|
+
frame.height = height;
|
|
127
132
|
}
|
|
133
|
+
frameContext.drawImage(video, 0, 0, width, height);
|
|
134
|
+
let results = await decoder.decodeFrame(frame);
|
|
135
|
+
if (cancelled || results.length === 0)
|
|
136
|
+
return;
|
|
137
|
+
// Highlights are drawn on an overlay sized like the video, so scale them back up.
|
|
138
|
+
let inverse = 1 / scale;
|
|
139
|
+
showHighlights(results.map((result) => result.corners.map((point) => ({
|
|
140
|
+
x: point.x * inverse,
|
|
141
|
+
y: point.y * inverse,
|
|
142
|
+
}))));
|
|
143
|
+
for (let result of results) {
|
|
144
|
+
if (result.text && isNew(result.text, now)) {
|
|
145
|
+
onReadRef.current(result.text);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function tick(now) {
|
|
150
|
+
if (cancelled || !video)
|
|
151
|
+
return;
|
|
152
|
+
if (!busy && now - lastDecode >= DECODE_INTERVAL) {
|
|
153
|
+
busy = true;
|
|
154
|
+
lastDecode = now;
|
|
155
|
+
decodeCurrentFrame(now)
|
|
156
|
+
.catch((ex) => {
|
|
157
|
+
console.warn("Barcode decode failed", ex);
|
|
158
|
+
})
|
|
159
|
+
.finally(() => {
|
|
160
|
+
busy = false;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
scheduleFrame(video, tick);
|
|
164
|
+
}
|
|
165
|
+
async function start() {
|
|
166
|
+
if (!navigator.mediaDevices?.getUserMedia) {
|
|
167
|
+
throw new DOMException("Camera access is not available", "NotFoundError");
|
|
168
|
+
}
|
|
169
|
+
stream = await navigator.mediaDevices.getUserMedia(videoConstraints(deviceId));
|
|
170
|
+
if (cancelled || !video)
|
|
171
|
+
return;
|
|
172
|
+
video.srcObject = stream;
|
|
173
|
+
await video.play();
|
|
174
|
+
if (cancelled)
|
|
175
|
+
return;
|
|
176
|
+
let overlay = canvasRef.current;
|
|
177
|
+
if (overlay) {
|
|
178
|
+
overlay.width = video.videoWidth;
|
|
179
|
+
overlay.height = video.videoHeight;
|
|
180
|
+
}
|
|
181
|
+
currentDeviceId.current = stream.getVideoTracks()[0]?.getSettings().deviceId ?? deviceId;
|
|
182
|
+
// Labels and the full device list are only available once permission has been granted.
|
|
183
|
+
let all = await navigator.mediaDevices.enumerateDevices();
|
|
184
|
+
if (cancelled)
|
|
185
|
+
return;
|
|
186
|
+
setDevices(all.filter((device) => device.kind === "videoinput"));
|
|
187
|
+
setIsLoading(false);
|
|
188
|
+
scheduleFrame(video, tick);
|
|
189
|
+
}
|
|
190
|
+
start().catch((ex) => {
|
|
191
|
+
if (cancelled)
|
|
192
|
+
return;
|
|
193
|
+
setIsLoading(false);
|
|
194
|
+
let name = ex instanceof DOMException || ex instanceof Error ? ex.name : "";
|
|
195
|
+
let message = ex instanceof Error ? ex.message : String(ex);
|
|
196
|
+
if (CAMERA_ERROR_NAMES.includes(name) || /camera|video|device|media/i.test(message)) {
|
|
197
|
+
setError({ icon: "camera", message: getLocalizedString("No camera available") });
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
setError({ icon: "generic", message });
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
return () => {
|
|
204
|
+
cancelled = true;
|
|
205
|
+
if (highlightTimeout.current)
|
|
206
|
+
clearTimeout(highlightTimeout.current);
|
|
207
|
+
stream?.getTracks().forEach((track) => track.stop());
|
|
208
|
+
if (video)
|
|
209
|
+
video.srcObject = null;
|
|
210
|
+
};
|
|
211
|
+
}, [videoRef, canvasRef, deviceId]);
|
|
212
|
+
// Draw the highlights of detected codes on the overlay canvas.
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
let overlay = canvasRef.current;
|
|
215
|
+
if (!overlay)
|
|
216
|
+
return;
|
|
217
|
+
let context = overlay.getContext("2d");
|
|
218
|
+
if (!context)
|
|
219
|
+
return;
|
|
220
|
+
context.clearRect(0, 0, overlay.width, overlay.height);
|
|
221
|
+
context.fillStyle = "rgba(254,180,32,0.3)";
|
|
222
|
+
for (let corners of highlights) {
|
|
223
|
+
context.beginPath();
|
|
224
|
+
context.moveTo(corners[0].x, corners[0].y);
|
|
225
|
+
context.lineTo(corners[1].x, corners[1].y);
|
|
226
|
+
context.lineTo(corners[2].x, corners[2].y);
|
|
227
|
+
context.lineTo(corners[3].x, corners[3].y);
|
|
228
|
+
context.closePath();
|
|
229
|
+
context.fill();
|
|
128
230
|
}
|
|
129
|
-
}, [
|
|
231
|
+
}, [canvasRef, highlights]);
|
|
130
232
|
function setNextDevice() {
|
|
131
|
-
|
|
132
|
-
|
|
233
|
+
if (devices.length <= 1)
|
|
234
|
+
return;
|
|
235
|
+
let index = devices.findIndex((device) => device.deviceId === currentDeviceId.current);
|
|
236
|
+
let next = devices[(index + 1) % devices.length];
|
|
237
|
+
if (next)
|
|
238
|
+
setDeviceId(next.deviceId);
|
|
133
239
|
}
|
|
134
|
-
return { devices
|
|
240
|
+
return { devices, engine, error, isLoading, setNextDevice };
|
|
135
241
|
}
|