|
1 | | -import React, { useRef, useEffect, MutableRefObject, useCallback } from "react"; |
| 1 | +import React, { useRef, useEffect, MutableRefObject, useCallback, useState } from "react"; |
2 | 2 | import "../../dynamsoft.config"; // import side effects. The license, engineResourcePath, so on. |
3 | 3 | import { EnumCapturedResultItemType } from "dynamsoft-core"; |
4 | 4 | import { BarcodeResultItem } from "dynamsoft-barcode-reader"; |
5 | 5 | import { CaptureVisionRouter } from "dynamsoft-capture-vision-router"; |
6 | 6 | import "./ImageCapture.css"; |
7 | 7 |
|
8 | 8 | function ImageCapture() { |
9 | | - const resultsContainer: MutableRefObject<HTMLDivElement | null> = useRef(null); |
| 9 | + const [resultText, setResultText] = useState(""); |
10 | 10 |
|
11 | 11 | let pCvRouter: MutableRefObject<Promise<CaptureVisionRouter> | null> = useRef(null); |
12 | 12 | let isDestroyed = useRef(false); |
13 | 13 |
|
14 | 14 | const decodeImg = useCallback(async (e: React.ChangeEvent<HTMLInputElement>) => { |
15 | 15 | let files = [...(e.target.files as any as File[])]; |
16 | 16 | e.target.value = ""; // reset input |
17 | | - resultsContainer.current!.innerText = ""; |
| 17 | + let _resultText = ""; |
18 | 18 |
|
19 | 19 | try { |
20 | 20 | // ensure cvRouter is created only once |
21 | 21 | const cvRouter = await (pCvRouter.current = pCvRouter.current || CaptureVisionRouter.createInstance()); |
22 | 22 | if (isDestroyed.current) return; |
23 | 23 |
|
24 | 24 | for (let file of files) { |
25 | | - // Decode selected image with 'ReadBarcodes_SpeedFirst' template. |
26 | | - const result = await cvRouter.capture(file, "ReadBarcodes_SpeedFirst"); |
| 25 | + // Decode selected image with 'ReadBarcodes_ReadRateFirst' template. |
| 26 | + const result = await cvRouter.capture(file, "ReadBarcodes_ReadRateFirst"); |
| 27 | + console.log(result); |
27 | 28 | if (isDestroyed.current) return; |
28 | 29 |
|
| 30 | + let _resultText = ""; |
29 | 31 | // Print file name if there's multiple files |
30 | 32 | if (files.length > 1) { |
31 | | - resultsContainer.current!.innerText += `\n${file.name}:\n`; |
| 33 | + _resultText += `\n${file.name}:\n`; |
32 | 34 | } |
33 | 35 | for (let _item of result.items) { |
34 | 36 | if (_item.type !== EnumCapturedResultItemType.CRIT_BARCODE) { |
35 | 37 | continue; // check if captured result item is a barcode |
36 | 38 | } |
37 | 39 | let item = _item as BarcodeResultItem; |
38 | | - resultsContainer.current!.innerText += item.text + "\n"; // output the decoded barcode text |
39 | | - console.log(item.text); |
| 40 | + _resultText += item.text + "\n"; // output the decoded barcode text |
40 | 41 | } |
41 | 42 | // If no items are found, display that no barcode was detected |
42 | | - if (!result.items.length) resultsContainer.current!.innerText = "No barcode found"; |
| 43 | + if (!result.items.length) _resultText = "No barcode found"; |
| 44 | + setResultText(_resultText); |
43 | 45 | } |
44 | 46 | } catch (ex: any) { |
45 | 47 | let errMsg = ex.message || ex; |
@@ -68,7 +70,7 @@ function ImageCapture() { |
68 | 70 | <div className="input-container"> |
69 | 71 | <input type="file" multiple accept=".jpg,.jpeg,.icon,.gif,.svg,.webp,.png,.bmp" onChange={decodeImg} /> |
70 | 72 | </div> |
71 | | - <div className="results" ref={resultsContainer}></div> |
| 73 | + <div className="results">{resultText}</div> |
72 | 74 | </div> |
73 | 75 | ); |
74 | 76 | } |
|
0 commit comments