|
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.show-result-texts-on-the-video.html" |
| 13 | + href="https://demo.dynamsoft.com/Samples/DBR/JS/use-case/show-result-texts-on-the-video.html" |
14 | 14 | /> |
15 | 15 | <title> |
16 | 16 | Dynamsoft Barcode Reader Sample - Show Result Texts on the Video |
17 | 17 | </title> |
| 18 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-core@3.0.20-dev-20231010152155/dist/core.js"></script> |
| 19 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-utility@1.0.10-dev-20231023103736/dist/utility.js"></script> |
| 20 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-barcode-reader@10.0.20-dev-20231020140243/dist/dbr.js"></script> |
| 21 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-capture-vision-router@2.0.20-dev-20231027145739/dist/cvr.js"></script> |
| 22 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-camera-enhancer@4.0.1-dev-20231023131759/dist/dce.js"></script> |
18 | 23 | <style> |
19 | 24 | .bubble-box-container { |
20 | 25 | position: absolute; |
21 | 26 | display: flex; |
22 | 27 | justify-content: center; |
23 | 28 | } |
24 | | - .bubble-box-container>.bubble-box{ |
| 29 | + .bubble-box-container > .bubble-box { |
25 | 30 | position: absolute; |
26 | 31 | width: max-content; |
27 | 32 | max-width: 20rem; |
28 | 33 | background: white; |
29 | | - border-radius: 4px; |
| 34 | + border-radius: 4px; |
30 | 35 | padding: 4px; |
31 | 36 | word-break: break-all; |
32 | 37 | word-wrap: break-word; |
33 | | - box-shadow: 5px 5px 5px 3px rgba(0,0,0,0.2); |
| 38 | + box-shadow: 5px 5px 5px 3px rgba(0, 0, 0, 0.2); |
34 | 39 | transform: translateY(calc(-100% - 4px)); |
35 | 40 | } |
36 | | - .bubble-box-container>.bubble-box::after{ |
37 | | - content: ''; |
| 41 | + .bubble-box-container > .bubble-box::after { |
| 42 | + content: ""; |
38 | 43 | position: absolute; |
39 | 44 | bottom: -8px; |
40 | 45 | left: calc(50% - 8px); |
|
47 | 52 | <body> |
48 | 53 | <div id="div-ui-container" style="width: 100%; height: 90vh"></div> |
49 | 54 | <div id="div-information-containers"></div> |
50 | | - <!-- |
51 | | - This sample makes use of the library hosted by the CDN jsDelivr. If you would rather use the |
52 | | - library offline. Please see the guide on how to host the library: |
53 | | - https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=latest#host-the-library-yourself-recommended |
54 | | - --> |
55 | | - <script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.31/dist/dbr.js"></script> |
56 | 55 | <script> |
57 | 56 | if (location.protocol === "file:") { |
58 | | - 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/".`; |
| 57 | + const message = `The page is opened via file:// and our SDKs may not work properly. Please open the page via https:// or host it on "http://localhost/".`; |
59 | 58 | console.warn(message); |
60 | 59 | alert(message); |
61 | 60 | } |
62 | 61 | </script> |
63 | 62 | <script> |
64 | 63 | /** LICENSE ALERT - README |
65 | | - * To use the library, you need to first specify a license key using the API "license" as shown below. |
| 64 | + * To use the library, you need to first specify a license key using the API "initLicense()" as shown below. |
66 | 65 | */ |
67 | 66 |
|
68 | | - Dynamsoft.DBR.BarcodeReader.license = |
69 | | - "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"; |
| 67 | + Dynamsoft.License.LicenseManager.initLicense( |
| 68 | + "DLS2eyJoYW5kc2hha2VDb2RlIjoiNjY2Ni03Nzc3IiwibWFpblNlcnZlclVSTCI6Imh0dHBzOi8vMTkyLjE2OC44LjEyMi9kbHMvIiwib3JnYW5pemF0aW9uSUQiOiI2NjY2IiwiY2hlY2tDb2RlIjoxNTEyMTgzMzg3fQ==" |
| 69 | + ); |
70 | 70 |
|
71 | 71 | /** |
72 | 72 | * You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=github&product=dbr&package=js to get your own trial license good for 30 days. |
73 | 73 | * Note that if you downloaded this sample from Dynamsoft while logged in, the above license key may already be your own 30-day trial license. |
74 | | - * For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=9.6.31&utm_source=github#specify-the-license or contact support@dynamsoft.com. |
| 74 | + * For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=10.0.20&utm_source=github#specify-the-license or contact support@dynamsoft.com. |
75 | 75 | * LICENSE ALERT - THE END |
76 | 76 | */ |
77 | 77 |
|
78 | | - const divInfContainers = document.querySelector("#div-information-containers"); |
79 | | - let scanner; |
| 78 | + const divInfContainers = document.querySelector( |
| 79 | + "#div-information-containers" |
| 80 | + ); |
80 | 81 |
|
81 | 82 | (async () => { |
82 | 83 | try { |
83 | | - scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance(); |
84 | | - await scanner.updateRuntimeSettings("speed"); |
85 | | - |
86 | | - // Get ui by 'getUIElement()' and append it to DOM. |
| 84 | + // Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control. |
| 85 | + const cameraView = await Dynamsoft.DCE.CameraView.createInstance(); |
| 86 | + const cameraEnhancer = |
| 87 | + await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView); |
87 | 88 | document |
88 | 89 | .querySelector("#div-ui-container") |
89 | | - .append(scanner.getUIElement()); |
| 90 | + .append(cameraView.getUIElement()); // Get default UI and append it to DOM. |
90 | 91 |
|
91 | | - /** |
92 | | - * 'onFrameRead' is triggered after the library finishes reading a frame image. |
93 | | - * There can be multiple barcodes on one image. |
94 | | - */ |
95 | | - scanner.onFrameRead = (results) => { |
96 | | - console.log(results); |
| 92 | + // Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source. |
| 93 | + const router = |
| 94 | + await Dynamsoft.CVR.CaptureVisionRouter.createInstance(); |
| 95 | + router.setInput(cameraEnhancer); |
97 | 96 |
|
98 | | - // Clear previous result text elements. |
| 97 | + // Define a callback for results. |
| 98 | + const resultReceiver = new Dynamsoft.CVR.CapturedResultReceiver(); |
| 99 | + resultReceiver.onCapturedResultReceived = (result) => { |
99 | 100 | divInfContainers.innerText = ""; |
100 | 101 |
|
101 | | - for (let result of results) { |
102 | | - const l = result.localizationResult; |
| 102 | + const resultItems = result.items; |
| 103 | + for (let item of resultItems) { |
| 104 | + if ( |
| 105 | + item.type != |
| 106 | + Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE |
| 107 | + ) |
| 108 | + continue; |
| 109 | + |
| 110 | + const l = item.location; |
103 | 111 | const p1 = { |
104 | | - x: Math.min(l.x1, l.x2, l.x3, l.x4), |
105 | | - y: Math.min(l.y1, l.y2, l.y3, l.y4), |
| 112 | + x: Math.min( |
| 113 | + l.points[0].x, |
| 114 | + l.points[1].x, |
| 115 | + l.points[2].x, |
| 116 | + l.points[3].x |
| 117 | + ), |
| 118 | + y: Math.min( |
| 119 | + l.points[0].y, |
| 120 | + l.points[1].y, |
| 121 | + l.points[2].y, |
| 122 | + l.points[3].y |
| 123 | + ), |
106 | 124 | }; |
107 | 125 | const p2 = { |
108 | | - x: Math.max(l.x1, l.x2, l.x3, l.x4), |
109 | | - y: Math.max(l.y1, l.y2, l.y3, l.y4), |
| 126 | + x: Math.max( |
| 127 | + l.points[0].x, |
| 128 | + l.points[1].x, |
| 129 | + l.points[2].x, |
| 130 | + l.points[3].x |
| 131 | + ), |
| 132 | + y: Math.max( |
| 133 | + l.points[0].y, |
| 134 | + l.points[1].y, |
| 135 | + l.points[2].y, |
| 136 | + l.points[3].y |
| 137 | + ), |
110 | 138 | }; |
111 | | - |
| 139 | + |
112 | 140 | const divInfContainer = document.createElement("div"); |
113 | 141 | divInfContainer.className = "bubble-box-container"; |
114 | 142 | const divInf = document.createElement("div"); |
115 | 143 | divInf.className = "bubble-box"; |
116 | | - divInf.innerText = result.barcodeText; |
| 144 | + divInf.innerText = item.text; |
117 | 145 | divInfContainer.append(divInf); |
118 | 146 | const bodyStyle = getComputedStyle(document.body); |
119 | 147 | if (bodyStyle.position === "static") { |
120 | 148 | /** |
121 | 149 | * 'convertToPageCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the document. |
122 | 150 | * Then we can place a div element according to the converted coordinate. |
123 | 151 | */ |
124 | | - const pageP1 = scanner.convertToPageCoordinates(p1); |
125 | | - const pageP2 = scanner.convertToPageCoordinates(p2); |
| 152 | + const pageP1 = cameraEnhancer.convertToPageCoordinates(p1); |
| 153 | + const pageP2 = cameraEnhancer.convertToPageCoordinates(p2); |
126 | 154 | const pageMidX = (pageP1.x + pageP2.x) / 2; |
127 | 155 | divInfContainer.style.top = `${pageP1.y}px`; |
128 | 156 | divInfContainer.style.left = `${pageMidX}px`; |
129 | | - } else { // if you set body `position` as `relative`, `absolute`, and so on, things can get complicated. |
| 157 | + } else { |
| 158 | + // if you set body `position` as `relative`, `absolute`, and so on, things can get complicated. |
130 | 159 | /** |
131 | 160 | * 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport. |
132 | 161 | * Then we can place a div element according to the converted coordinate. |
133 | 162 | */ |
134 | | - const clientP1 = scanner.convertToClientCoordinates(p1); |
135 | | - const clientP2 = scanner.convertToClientCoordinates(p2); |
| 163 | + const clientP1 = cameraEnhancer.convertToClientCoordinates(p1); |
| 164 | + const clientP2 = cameraEnhancer.convertToClientCoordinates(p2); |
136 | 165 | const clientMidX = (clientP1.x + clientP2.x) / 2; |
137 | 166 | const bodyRect = document.body.getBoundingClientRect(); |
138 | | - divInfContainer.style.top = `${clientP1.y - bodyRect.top - parseFloat(bodyStyle.borderTopWidth)}px`; |
139 | | - divInfContainer.style.left = `${clientMidX - bodyRect.left - parseFloat(bodyStyle.borderLeftWidth)}px`; |
| 167 | + divInfContainer.style.top = `${ |
| 168 | + clientP1.y - |
| 169 | + bodyRect.top - |
| 170 | + parseFloat(bodyStyle.borderTopWidth) |
| 171 | + }px`; |
| 172 | + divInfContainer.style.left = `${ |
| 173 | + clientMidX - |
| 174 | + bodyRect.left - |
| 175 | + parseFloat(bodyStyle.borderLeftWidth) |
| 176 | + }px`; |
140 | 177 | } |
141 | 178 | divInfContainers.append(divInfContainer); |
142 | 179 |
|
|
145 | 182 | */ |
146 | 183 | } |
147 | 184 | }; |
| 185 | + router.addResultReceiver(resultReceiver); |
| 186 | + |
| 187 | + // Filter out unchecked and duplicate results. |
| 188 | + const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter(); |
| 189 | + filter.enableResultCrossVerification( |
| 190 | + Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE, |
| 191 | + true |
| 192 | + ); // Filter out unchecked barcodes. |
148 | 193 |
|
149 | | - /** |
150 | | - * 'open()' opens the camera. |
151 | | - * After that, the library starts to scan the frame images continuously. |
152 | | - */ |
153 | | - await scanner.open(); |
| 194 | + // Open camera and start scanning single barcode. |
| 195 | + await cameraEnhancer.open(); |
| 196 | + await router.startCapturing("ReadBarcodes_SpeedFirst"); |
154 | 197 | } catch (ex) { |
155 | 198 | let errMsg; |
156 | 199 | if (ex.message.includes("network connection error")) { |
|
0 commit comments