|
10 | 10 | <meta name="keywords" content="read barcode from camera, custom style" /> |
11 | 11 | <link |
12 | 12 | rel="canonical" |
13 | | - href="https://demo.dynamsoft.com/Samples/DBR/JS/4.use-case/3.highlight-results-with-custom-style.html" |
| 13 | + href="https://demo.dynamsoft.com/Samples/DBR/JS/4.use-case/3.show-result-texts-on-the-video.html" |
14 | 14 | /> |
15 | 15 | <title> |
16 | | - Dynamsoft Barcode Reader Sample - Highlight Results with Custom Style |
| 16 | + Dynamsoft Barcode Reader Sample - Show result texts on the video |
17 | 17 | </title> |
18 | 18 | <!-- |
19 | 19 | This sample makes use of the library hosted by the CDN jsDelivr. If you would rather use the |
20 | 20 | library offline. Please see the guide on how to host the library: |
21 | 21 | https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=latest#host-the-library-yourself-recommended |
22 | 22 | --> |
23 | 23 | <script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.20/dist/dbr.js"></script> |
| 24 | + <style> |
| 25 | + * { |
| 26 | + margin: 0; |
| 27 | + padding: 0; |
| 28 | + } |
| 29 | + body { |
| 30 | + position: relative; |
| 31 | + } |
| 32 | + </style> |
24 | 33 | </head> |
25 | 34 | <body> |
26 | 35 | <div id="div-ui-container" style="width: 100%; height: 100vh"></div> |
| 36 | + <div id="div-text-containers" style="position: absolute; left: 0; top: 0; width: 100%; height: 100%"></div> |
27 | 37 | <script> |
28 | 38 | if (location.protocol === "file:") { |
29 | 39 | const message = `The page is opened via file:// and "BarcodeScanner" may not work properly. Please open the page via https:// or host it on "http://localhost/".`; |
|
46 | 56 | * LICENSE ALERT - THE END |
47 | 57 | */ |
48 | 58 |
|
| 59 | + const divTextContainers = document.querySelector("#div-text-containers"); |
49 | 60 | let scanner; |
50 | | - const highlightResultMasks = []; |
51 | 61 |
|
52 | 62 | (async () => { |
53 | 63 | try { |
54 | 64 | scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance(); |
| 65 | + await scanner.updateRuntimeSettings("speed"); |
55 | 66 |
|
56 | 67 | // Get ui by 'getUIElement()' and append it to DOM. |
57 | 68 | document |
58 | 69 | .querySelector("#div-ui-container") |
59 | 70 | .append(scanner.getUIElement()); |
60 | 71 |
|
61 | | - // Hide the built-in shape which highlights a found barcode. |
62 | | - scanner.barcodeFillStyle = "transparent"; |
63 | | - scanner.barcodeStrokeStyle = "transparent"; |
64 | | - scanner.barcodeFillStyleBeforeVerification = "transparent"; |
65 | | - scanner.barcodeStrokeStyleBeforeVerification = "transparent"; |
66 | | - |
67 | 72 | /** |
68 | 73 | * 'onFrameRead' is triggered after the library finishes reading a frame image. |
69 | 74 | * There can be multiple barcodes on one image. |
70 | 75 | */ |
71 | 76 | scanner.onFrameRead = (results) => { |
72 | 77 | console.log(results); |
73 | 78 |
|
74 | | - // Clear previous highlight result masks. |
75 | | - highlightResultMasks.forEach((mask) => mask.remove()); |
76 | | - highlightResultMasks.length = 0; |
| 79 | + // Clear previous result text elements. |
| 80 | + divTextContainers.innerText = ""; |
77 | 81 |
|
78 | 82 | for (let result of results) { |
79 | 83 | const p1 = { |
|
105 | 109 | ), |
106 | 110 | }; |
107 | 111 | /** |
108 | | - * 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport. |
| 112 | + * 'convertToPageCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the document. |
109 | 113 | * Then we can place a div element according to the converted coordinate. |
110 | 114 | */ |
111 | | - const convertedP1 = scanner.convertToClientCoordinates(p1); |
112 | | - const convertedP2 = scanner.convertToClientCoordinates(p2); |
113 | | - mask = document.createElement("div"); |
114 | | - mask.style.position = "fixed"; |
115 | | - mask.style.left = convertedP1.x + "px"; |
116 | | - mask.style.top = convertedP1.y + "px"; |
117 | | - mask.style.width = convertedP2.x - convertedP1.x + "px"; |
118 | | - mask.style.height = convertedP2.y - convertedP1.y + "px"; |
119 | | - mask.style.border = "2px dashed red"; |
120 | | - document.body.append(mask); |
121 | | - highlightResultMasks.push(mask); |
| 115 | + const convertedP1 = scanner.convertToPageCoordinates(p1); |
| 116 | + const convertedP2 = scanner.convertToPageCoordinates(p2); |
| 117 | + textContainer = document.createElement("div"); |
| 118 | + textContainer.style.position = "absolute"; |
| 119 | + textContainer.style.left = convertedP1.x + "px"; |
| 120 | + textContainer.style.right = document.body.clientWidth - convertedP2.x + "px"; |
| 121 | + textContainer.style.bottom = document.body.clientHeight - convertedP1.y + "px"; |
| 122 | + textContainer.style.color = "#FE8E14"; |
| 123 | + textContainer.innerText = result.barcodeText; |
| 124 | + divTextContainers.append(textContainer); |
122 | 125 | } |
123 | 126 | }; |
124 | 127 |
|
|
0 commit comments