|
12 | 12 | rel="canonical" |
13 | 13 | href="https://demo.dynamsoft.com/Samples/DBR/JS/4.use-case/3.highlight-results-with-custom-style.html" |
14 | 14 | /> |
15 | | - <title>Dynamsoft Barcode Reader Sample - Highlight Results with Custom Style</title> |
| 15 | + <title> |
| 16 | + Dynamsoft Barcode Reader Sample - Highlight Results with Custom Style |
| 17 | + </title> |
16 | 18 | <!-- |
17 | 19 | This sample makes use of the library hosted by the CDN jsDelivr. If you would rather use the |
18 | 20 | library offline. Please see the guide on how to host the library: |
|
23 | 25 | <body> |
24 | 26 | <div id="div-ui-container" style="width: 100%; height: 100vh"></div> |
25 | 27 | <script> |
26 | | - if(location.protocol === "file:") { |
| 28 | + if (location.protocol === "file:") { |
27 | 29 | 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/".`; |
28 | 30 | console.warn(message); |
29 | 31 | alert(message); |
|
34 | 36 | * To use the library, you need to first specify a license key using the API "license" as shown below. |
35 | 37 | */ |
36 | 38 |
|
37 | | - Dynamsoft.DBR.BarcodeReader.license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"; |
| 39 | + Dynamsoft.DBR.BarcodeReader.license = |
| 40 | + "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"; |
38 | 41 |
|
39 | 42 | /** |
40 | 43 | * 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. |
|
61 | 64 | scanner.barcodeFillStyleBeforeVerification = "transparent"; |
62 | 65 | scanner.barcodeStrokeStyleBeforeVerification = "transparent"; |
63 | 66 |
|
64 | | - /** |
| 67 | + /** |
65 | 68 | * 'onFrameRead' is triggered after the library finishes reading a frame image. |
66 | 69 | * There can be multiple barcodes on one image. |
67 | 70 | */ |
68 | 71 | scanner.onFrameRead = (results) => { |
69 | 72 | console.log(results); |
70 | 73 |
|
71 | 74 | // Clear previous highlight result masks. |
72 | | - highlightResultMasks.forEach(mask => mask.remove()); |
| 75 | + highlightResultMasks.forEach((mask) => mask.remove()); |
73 | 76 | highlightResultMasks.length = 0; |
74 | 77 |
|
75 | 78 | for (let result of results) { |
76 | 79 | const p1 = { |
77 | | - x: result.localizationResult.x1, |
78 | | - y: result.localizationResult.y1 |
| 80 | + x: Math.min( |
| 81 | + result.localizationResult.x1, |
| 82 | + result.localizationResult.x2, |
| 83 | + result.localizationResult.x3, |
| 84 | + result.localizationResult.x4 |
| 85 | + ), |
| 86 | + y: Math.min( |
| 87 | + result.localizationResult.y1, |
| 88 | + result.localizationResult.y2, |
| 89 | + result.localizationResult.y3, |
| 90 | + result.localizationResult.y4 |
| 91 | + ), |
79 | 92 | }; |
80 | 93 | const p2 = { |
81 | | - x: result.localizationResult.x3, |
82 | | - y: result.localizationResult.y3 |
| 94 | + x: Math.max( |
| 95 | + result.localizationResult.x1, |
| 96 | + result.localizationResult.x2, |
| 97 | + result.localizationResult.x3, |
| 98 | + result.localizationResult.x4 |
| 99 | + ), |
| 100 | + y: Math.max( |
| 101 | + result.localizationResult.y1, |
| 102 | + result.localizationResult.y2, |
| 103 | + result.localizationResult.y3, |
| 104 | + result.localizationResult.y4 |
| 105 | + ), |
83 | 106 | }; |
84 | | - /** |
| 107 | + /** |
85 | 108 | * 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport. |
86 | 109 | * Then we can place a div element according to the converted coordinate. |
87 | 110 | */ |
88 | 111 | const convertedP1 = scanner.convertToClientCoordinates(p1); |
89 | 112 | const convertedP2 = scanner.convertToClientCoordinates(p2); |
90 | 113 | mask = document.createElement("div"); |
91 | 114 | mask.style.position = "fixed"; |
92 | | - mask.style.left = Math.min(convertedP1.x, convertedP2.x)+"px"; |
93 | | - mask.style.top = Math.min(convertedP1.y, convertedP2.y)+"px"; |
94 | | - mask.style.width = Math.abs(convertedP2.x-convertedP1.x)+"px"; |
95 | | - mask.style.height = Math.abs(convertedP2.y-convertedP1.y)+"px"; |
| 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"; |
96 | 119 | mask.style.border = "2px dashed red"; |
97 | 120 | document.body.append(mask); |
98 | 121 | highlightResultMasks.push(mask); |
99 | 122 | } |
100 | 123 | }; |
101 | 124 |
|
102 | 125 | /** |
103 | | - * 'open()' opens the camera. |
| 126 | + * 'open()' opens the camera. |
104 | 127 | * After that, the library starts to scan the frame images continuously. |
105 | 128 | */ |
106 | 129 | await scanner.open(); |
|
0 commit comments