-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathImageCapture.tsx
More file actions
78 lines (68 loc) · 2.78 KB
/
ImageCapture.tsx
File metadata and controls
78 lines (68 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React, { useRef, useEffect, MutableRefObject, useState } from "react";
import "../../dynamsoft.config"; // import side effects. The license, engineResourcePath, so on.
import { EnumCapturedResultItemType } from "dynamsoft-core";
import { BarcodeResultItem } from "dynamsoft-barcode-reader";
import { CaptureVisionRouter } from "dynamsoft-capture-vision-router";
import "./ImageCapture.css";
function ImageCapture() {
const [resultText, setResultText] = useState("");
let pCvRouter: MutableRefObject<Promise<CaptureVisionRouter> | null> = useRef(null);
let isDestroyed = useRef(false);
const decodeImg = async (e: React.ChangeEvent<HTMLInputElement>) => {
let files = [...(e.target.files as any as File[])];
e.target.value = ""; // reset input
setResultText("");
try {
// ensure cvRouter is created only once
const cvRouter = await (pCvRouter.current = pCvRouter.current || CaptureVisionRouter.createInstance());
if (isDestroyed.current) return;
let _resultText = "";
for (let file of files) {
// Decode selected image with 'ReadBarcodes_ReadRateFirst' template.
const result = await cvRouter.capture(file, "ReadBarcodes_ReadRateFirst");
console.log(result);
if (isDestroyed.current) return;
// Print file name if there's multiple files
if (files.length > 1) {
_resultText += `\n${file.name}:\n`;
}
for (let _item of result.items) {
if (_item.type !== EnumCapturedResultItemType.CRIT_BARCODE) {
continue; // check if captured result item is a barcode
}
let item = _item as BarcodeResultItem;
_resultText += item.formatString + ": " + item.text + "\n"
}
setResultText(_resultText);
// If no items are found, display that no barcode was detected
if (!result.items.length) setResultText(_resultText + "No barcode found");
}
} catch (ex: any) {
let errMsg = ex.message || ex;
console.error(errMsg);
alert(errMsg);
}
};
useEffect((): any => {
// In 'development', React runs setup and cleanup one extra time before the actual setup in Strict Mode.
isDestroyed.current = false;
// componentWillUnmount. dispose cvRouter when it's no longer needed
return () => {
isDestroyed.current = true;
if (pCvRouter.current) {
pCvRouter.current.then((cvRouter) => {
cvRouter.dispose();
}).catch((_) => { })
}
};
}, []);
return (
<div className="image-capture-container">
<div className="input-container">
<input type="file" multiple accept=".jpg,.jpeg,.icon,.gif,.svg,.webp,.png,.bmp" onChange={decodeImg} />
</div>
<div className="results">{resultText}</div>
</div>
);
}
export default ImageCapture;