Skip to content

Commit 9991f7a

Browse files
committed
add 4.use-case/3.highlight-results-with-custom-style
1 parent 70909ce commit 9991f7a

File tree

3 files changed

+141
-4
lines changed

3 files changed

+141
-4
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta
7+
name="description"
8+
content="Read barcodes from camera with Dynamsoft Barcode Reader and hightlight results with custom style."
9+
/>
10+
<meta name="keywords" content="read barcode from camera, custom style" />
11+
<link
12+
rel="canonical"
13+
href="https://demo.dynamsoft.com/Samples/DBR/JS/4.use-case/3.highlight-results-with-custom-style.html"
14+
/>
15+
<title>Dynamsoft Barcode Reader Sample - Highlight Results with Custom Style</title>
16+
<!--
17+
This sample makes use of the library hosted by the CDN jsDelivr. If you would rather use the
18+
library offline. Please see the guide on how to host the library:
19+
https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=latest#host-the-library-yourself-recommended
20+
-->
21+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/dbr.js"></script>
22+
</head>
23+
<body>
24+
<div id="div-ui-container" style="width: 100%; height: 100vh"></div>
25+
<script>
26+
if(location.protocol === "file:") {
27+
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+
console.warn(message);
29+
alert(message);
30+
}
31+
</script>
32+
<script>
33+
/** LICENSE ALERT - README
34+
* To use the library, you need to first specify a license key using the API "license" as shown below.
35+
*/
36+
37+
Dynamsoft.DBR.BarcodeReader.license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
38+
39+
/**
40+
* 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.
41+
* 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.
42+
* For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=9.6.11&utm_source=github#specify-the-license or contact [email protected].
43+
* LICENSE ALERT - THE END
44+
*/
45+
46+
let scanner;
47+
const highlightResultMasks = [];
48+
49+
(async () => {
50+
try {
51+
scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance();
52+
53+
// Get ui by 'getUIElement()' and append it to DOM.
54+
document
55+
.querySelector("#div-ui-container")
56+
.append(scanner.getUIElement());
57+
58+
// Hide the built-in shape which highlights a found barcode.
59+
scanner.barcodeFillStyle = "transparent";
60+
scanner.barcodeStrokeStyle = "transparent";
61+
62+
/**
63+
* 'onFrameRead' is triggered after the library finishes reading a frame image.
64+
* There can be multiple barcodes on one image.
65+
*/
66+
scanner.onFrameRead = (results) => {
67+
console.log(results);
68+
69+
// Clear previous highlight result masks.
70+
highlightResultMasks.forEach(mask => mask.remove());
71+
highlightResultMasks.length = 0;
72+
73+
for (let result of results) {
74+
const p1 = {
75+
x: result.localizationResult.x1,
76+
y: result.localizationResult.y1
77+
};
78+
const p2 = {
79+
x: result.localizationResult.x3,
80+
y: result.localizationResult.y3
81+
};
82+
/**
83+
* 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport.
84+
* Then we can place a div element according to the converted coordinate.
85+
*/
86+
const convertedP1 = scanner.convertToClientCoordinates(p1);
87+
const convertedP2 = scanner.convertToClientCoordinates(p2);
88+
mask = document.createElement("div");
89+
mask.style.position = "fixed";
90+
mask.style.left = convertedP1.x+"px";
91+
mask.style.top = convertedP1.y+"px";
92+
mask.style.width = convertedP2.x-convertedP1.x+"px";
93+
mask.style.height = convertedP2.y-convertedP1.y+"px";
94+
mask.style.border = "2px dashed red";
95+
document.body.append(mask);
96+
highlightResultMasks.push(mask);
97+
}
98+
};
99+
100+
/**
101+
* 'open()' opens the camera.
102+
* After that, the library starts to scan the frame images continuously.
103+
*/
104+
await scanner.open();
105+
} catch (ex) {
106+
let errMsg;
107+
if (ex.message.includes("network connection error")) {
108+
errMsg =
109+
"Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support ([email protected]) to acquire an offline license.";
110+
} else {
111+
errMsg = ex.message || ex;
112+
}
113+
console.error(errMsg);
114+
alert(errMsg);
115+
}
116+
})();
117+
</script>
118+
</body>
119+
</html>

4.use-case/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ The following official sample shows how to use the sdk to read a driver's licens
4444

4545
Also see [Driver's License Scanner SDK for Mobile and Web](https://www.dynamsoft.com/use-cases/driver-license/).
4646

47+
# Read barcodes via camera and highlight results with custom style
48+
49+
When the SDK find barcodes in camera, it will highlight them with built-in style automatically. But it is also possible to customize the highlight style freely using the method 'convertToClientCoordinates()'.
50+
51+
The following official sample shows how to hightlight results with custom style.
52+
53+
* <a target = "_blank" href="https://demo.dynamsoft.com/Samples/DBR/JS/4.use-case/3.3.highlight-results-with-custom-style.html">Read the PDF417 Barcode on the Driver&apos;s License - Demo</a>
54+
* <a target = "_blank" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/blob/main/4.use-case/3.highlight-results-with-custom-style.html">Read the PDF417 Barcode on the Driver&apos;s License - Source Code</a>
4755

4856
## Support
4957

index.html

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,19 @@ <h3>Barcode Reader Samples</h3>
129129
<a class="github" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/tree/main/1.hello-world/12.read-video-es6.html" title="Check code on GitHub"></a>
130130
</div>
131131
<div class="file"><a data-balloon-length="fit" data-balloon-pos="down" aria-label="Decode video stream in a React
132-
Application from a USB-connected or built-in camera (mobile or desktop)." class="button title" onclick="linkRedirect('react_ts')" href="javascript:void(0);">
132+
Application from a USB-connected or built-in camera (mobile or desktop)." class="button title" onclick="linkRedirect('react_hooks')" href="javascript:void(0);">
133133
Hello World in
134-
React(with TypeScript)(<img alt="React logo"
134+
React(using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jItNDAtMjIuNS02MC45IDkuMi0yMC44IDE2LjYtNDEuMSAyMi4yLTYwLjYgOS45IDMuMSAxOS4zIDYuNSAyOC4xIDEwLjIgMzUuNCAxNS4xIDU4LjMgMzQuOSA1OC4zIDUwLjYtLjEgMTUuNy0yMyAzNS42LTU4LjQgNTAuNnpNMzIwLjggNzguNHoiLz48Y2lyY2xlIGN4PSI0MjAuOSIgY3k9IjI5Ni41IiByPSI0NS43Ii8+PHBhdGggZD0iTTUyMC41IDc4LjF6Ii8+PC9nPjwvc3ZnPg==" />)</a>
136136
<span id="icon013" class="tooltipIcon"></span>
137-
<a class="github" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/tree/main/1.hello-world/13.read-video-react-ts" title="Check code on GitHub"></a>
137+
<a class="github" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/tree/main/1.hello-world/13.read-video-react-hooks" title="Check code on GitHub"></a>
138138
</div>
139+
<!-- TODO: update link -->
140+
<div class="file"><a data-balloon-length="fit" data-balloon-pos="down" aria-label="Decode video stream in WebView from a USB-connected or built-in camera (mobile or desktop)." target="_blank" class="button title" href="https://www.dynamsoft.com/barcode-reader/programming/javascript/samples-demos/helloworld-nextjs.html?ver=latest">
141+
Hello World in
142+
WebView </a><span id="icon007" class="tooltipIcon"></span>
143+
<a class="github" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/tree/main/1.hello-world/14.read-video-webview" title="Check code on GitHub"></a>
144+
</div>
139145
</div>
140146
<div class="file">Customize Camera UI</div>
141147
<div class="children">
@@ -205,6 +211,9 @@ <h3>Barcode Reader Samples</h3>
205211
Driver's License</a><span id="icon042" class="tooltipIcon"></span>
206212
<a class="github" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/blob/main/4.use-case/2.read-a-drivers-license.html" title="Check code on GitHub"></a>
207213
</div>
214+
<div class="file"><a data-balloon-length="fit" data-balloon-pos="up" aria-label="Read barcodes from camera with Dynamsoft Barcode Reader and hightlight results with custom style." class="button title" href="4.use-case/3.highlight-results-with-custom-style.html">Hightlight results with custom style</a><span id="icon042" class="tooltipIcon"></span>
215+
<a class="github" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/blob/main/4.use-case/3.highlight-results-with-custom-style.html" title="Check code on GitHub"></a>
216+
</div>
208217
</div>
209218
<div class="file">Others</div>
210219
<div class="children">
@@ -369,8 +378,9 @@ <h3>Barcode Reader Samples</h3>
369378
localPath = "1.hello-world/6.read-video-vue3/dist/";
370379
onlineGuidePath = "https://www.dynamsoft.com/barcode-reader/programming/javascript/samples-demos/helloworld-vuejsv3.html?ver=latest";
371380
break;
372-
case "react_ts":
381+
case "react_hooks":
373382
localPath = "1.hello-world/13.read-video-react-ts/build/";
383+
// TODO: update link
374384
onlineGuidePath = "https://www.dynamsoft.com/barcode-reader/programming/javascript/samples-demos/helloworld-reactjs-ts.html?ver=latest";
375385
break;
376386
}

0 commit comments

Comments
 (0)