Skip to content

Commit be3ff2b

Browse files
committed
Update 3.highlight-results-with-custom-style.html
1 parent a26db78 commit be3ff2b

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

4.use-case/3.highlight-results-with-custom-style.html

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
rel="canonical"
1313
href="https://demo.dynamsoft.com/Samples/DBR/JS/4.use-case/3.highlight-results-with-custom-style.html"
1414
/>
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>
1618
<!--
1719
This sample makes use of the library hosted by the CDN jsDelivr. If you would rather use the
1820
library offline. Please see the guide on how to host the library:
@@ -23,7 +25,7 @@
2325
<body>
2426
<div id="div-ui-container" style="width: 100%; height: 100vh"></div>
2527
<script>
26-
if(location.protocol === "file:") {
28+
if (location.protocol === "file:") {
2729
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/".`;
2830
console.warn(message);
2931
alert(message);
@@ -34,7 +36,8 @@
3436
* To use the library, you need to first specify a license key using the API "license" as shown below.
3537
*/
3638

37-
Dynamsoft.DBR.BarcodeReader.license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
39+
Dynamsoft.DBR.BarcodeReader.license =
40+
"DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
3841

3942
/**
4043
* 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,46 +64,66 @@
6164
scanner.barcodeFillStyleBeforeVerification = "transparent";
6265
scanner.barcodeStrokeStyleBeforeVerification = "transparent";
6366

64-
/**
67+
/**
6568
* 'onFrameRead' is triggered after the library finishes reading a frame image.
6669
* There can be multiple barcodes on one image.
6770
*/
6871
scanner.onFrameRead = (results) => {
6972
console.log(results);
7073

7174
// Clear previous highlight result masks.
72-
highlightResultMasks.forEach(mask => mask.remove());
75+
highlightResultMasks.forEach((mask) => mask.remove());
7376
highlightResultMasks.length = 0;
7477

7578
for (let result of results) {
7679
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+
),
7992
};
8093
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+
),
83106
};
84-
/**
107+
/**
85108
* 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport.
86109
* Then we can place a div element according to the converted coordinate.
87110
*/
88111
const convertedP1 = scanner.convertToClientCoordinates(p1);
89112
const convertedP2 = scanner.convertToClientCoordinates(p2);
90113
mask = document.createElement("div");
91114
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";
96119
mask.style.border = "2px dashed red";
97120
document.body.append(mask);
98121
highlightResultMasks.push(mask);
99122
}
100123
};
101124

102125
/**
103-
* 'open()' opens the camera.
126+
* 'open()' opens the camera.
104127
* After that, the library starts to scan the frame images continuously.
105128
*/
106129
await scanner.open();

0 commit comments

Comments
 (0)