/
read-a-drivers-license.html
543 lines (489 loc) · 19 KB
/
read-a-drivers-license.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<meta
name="description"
content="Read the PDF417 barcode on Driver's Licenses instantly with this barcode reader."
/>
<meta name="keywords" content="read driver's license barcode" />
<link
rel="canonical"
href="https://demo.dynamsoft.com/samples/dbr/js/use-case/read-a-drivers-license.html"
/>
<title>Dynamsoft Barcode Reader Simple Sample - Read Driver License</title>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-core@3.0.32/dist/core.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-license@3.0.20/dist/license.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-utility@1.0.21/dist/utility.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader@10.0.20/dist/dbr.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-capture-vision-router@2.0.31/dist/cvr.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-camera-enhancer@4.0.1/dist/dce.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-code-parser@1.1.0/dist/dcp.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: hidden;
width: 100vw;
height: 100vh;
}
#span-tip {
position: absolute;
font-size: 1rem;
color: #fe8e14;
top: 1vh;
}
h1 {
font-size: 1.5em;
}
a {
color: #fe8d14;
}
a:hover {
color: #fe8d14a1;
}
strong {
color: rgb(254, 142, 20);
}
#svg-start {
width: 100px;
height: 100px;
fill: #aaa;
cursor: pointer;
}
#div-main {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
width: 50vw;
height: 60vh;
}
.dce-bg-camera {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
width: 40%;
height: 40%;
fill: #aaa;
}
#div-ui-container {
display: none;
width: 100%;
height: 100%;
}
#div-result-container {
display: none;
position: fixed;
width: 50vw;
height: 70vh;
z-index: 10;
color: white;
font-size: small;
background: #222222;
margin-top: 30px;
}
#div-result-container .div-result-header {
width: 100%;
height: 30px;
background-color: #000;
font-size: 16px;
line-height: 30px;
padding-left: 10px;
box-sizing: border-box;
}
#div-result-container .div-result-body {
height: calc(70vh - 30px);
overflow-y: auto;
padding: 5px;
box-sizing: border-box;
}
#div-result-container p {
padding: 5px;
margin: 0;
}
#div-message-container {
display: none;
align-items: center;
width: 50vw;
padding: 8px 16px;
margin: -92px 0 10px 0;
box-sizing: border-box;
border-radius: 4px;
position: relative;
background-color: #f4f4f5;
color: #909399;
overflow: hidden;
transition: opacity 0.2s;
}
#div-message-container .em-icon {
font-size: 28px;
width: 28px;
line-height: 0;
}
#div-message-container .div-message-content {
display: table-cell;
padding: 0 8px;
}
.div-message-content > span {
font-weight: 700;
font-size: 14px;
line-height: 18px;
}
.div-message-content > p {
font-size: 13px;
margin: 5px 0 0;
line-height: 20px;
}
.api_highlight {
font-weight: 700;
background-color: #ddd;
border-radius: 4px;
padding: 2px;
}
.em-close {
position: absolute;
top: 12px;
right: 15px;
cursor: pointer;
line-height: 0;
}
/* Small devices */
@media only screen and (max-width: 1000px) {
#div-message-container {
width: 90vw;
}
#div-main {
width: 80vw;
}
#div-result-container {
width: 80vw;
}
}
</style>
</head>
<body>
<span id="span-tip">Aim at the barcode on the driver's license.</span>
<h1>Read A Driver's License</h1>
<div id="div-message-container">
<em class="em-icon"
><svg
viewBox="0 0 1024 1024"
xmlns="http://www.w3.org/2000/svg"
p-id="3665"
width="32"
height="32"
>
<path
d="M544 789.333333h105.173333a10.666667 10.666667 0 0 0 10.666667-10.666666v-0.362667l-3.232-94.304a32 32 0 0 1 14.293333-27.765333C750.933333 603.189333 800 513.792 800 416c0-159.061333-128.938667-288-288-288S224 256.938667 224 416c0 97.813333 49.098667 187.232 129.141333 240.266667a32 32 0 0 1 14.314667 25.578666l3.338667 97.184a10.666667 10.666667 0 0 0 10.666666 10.304H480V572.693333l-76.64-81.429333a32 32 0 0 1 46.613333-43.861333l63.36 67.328 72.693334-68.661334a32 32 0 0 1 43.946666 46.528L544 573.792V789.333333z m320-373.333333a351.530667 351.530667 0 0 1-142.826667 283.146667l2.634667 76.96A74.666667 74.666667 0 0 1 649.173333 853.333333h-267.733333a74.666667 74.666667 0 0 1-74.624-72.106666l-2.784-81.194667A351.541333 351.541333 0 0 1 160 416C160 221.6 317.6 64 512 64s352 157.6 352 352zM416 960a32 32 0 0 1 0-64h192a32 32 0 0 1 0 64H416z"
p-id="3666"
fill="#909399"
></path>
</svg>
</em>
<div class="div-message-content">
<span>Can't get scanning result for a long time?</span>
<p>
After setting the
<span class="api_highlight">singleFrameMode</span> as true, you can
open the camera to shoot (mobile device) or choose from local to get a
high-definition picture for recognition.
<a href="javascript:void(0)" id="a-switch">Have a try</a>
</p>
<em class="em-close"
><svg
viewBox="0 0 1024 1024"
xmlns="http://www.w3.org/2000/svg"
p-id="4604"
width="10"
height="10"
>
<path
d="M512 456.310154L94.247385 38.557538a39.542154 39.542154 0 0 0-55.689847 0 39.266462 39.266462 0 0 0 0 55.689847L456.310154 512 38.557538 929.752615a39.542154 39.542154 0 0 0 0 55.689847 39.266462 39.266462 0 0 0 55.689847 0L512 567.689846l417.752615 417.752616c15.163077 15.163077 40.290462 15.36 55.689847 0a39.266462 39.266462 0 0 0 0-55.689847L567.689846 512 985.442462 94.247385a39.542154 39.542154 0 0 0 0-55.689847 39.266462 39.266462 0 0 0-55.689847 0L512 456.310154z"
p-id="4605"
fill="#c0c4cc"
></path>
</svg>
</em>
</div>
</div>
<div id="div-main">
<svg id="svg-start" viewBox="0 0 2048 1792">
<path
d="M1024 672q119 0 203.5 84.5t84.5 203.5-84.5 203.5-203.5 84.5-203.5-84.5-84.5-203.5 84.5-203.5 203.5-84.5zm704-416q106 0 181 75t75 181v896q0 106-75 181t-181 75h-1408q-106 0-181-75t-75-181v-896q0-106 75-181t181-75h224l51-136q19-49 69.5-84.5t103.5-35.5h512q53 0 103.5 35.5t69.5 84.5l51 136h224zm-704 1152q185 0 316.5-131.5t131.5-316.5-131.5-316.5-316.5-131.5-316.5 131.5-131.5 316.5 131.5 316.5 316.5 131.5z"
/>
</svg>
<div id="div-ui-container"></div>
</div>
<div id="div-result-container">
<div class="div-result-header">
Results:
<em class="em-close" class="message_box_closebtn"
><svg
viewBox="0 0 1024 1024"
xmlns="http://www.w3.org/2000/svg"
p-id="4604"
width="16"
height="16"
>
<path
d="M512 456.310154L94.247385 38.557538a39.542154 39.542154 0 0 0-55.689847 0 39.266462 39.266462 0 0 0 0 55.689847L456.310154 512 38.557538 929.752615a39.542154 39.542154 0 0 0 0 55.689847 39.266462 39.266462 0 0 0 55.689847 0L512 567.689846l417.752615 417.752616c15.163077 15.163077 40.290462 15.36 55.689847 0a39.266462 39.266462 0 0 0 0-55.689847L567.689846 512 985.442462 94.247385a39.542154 39.542154 0 0 0 0-55.689847 39.266462 39.266462 0 0 0-55.689847 0L512 456.310154z"
p-id="4605"
fill="#ffffff"
></path>
</svg>
</em>
</div>
<div class="div-result-body"></div>
</div>
<script>
if (location.protocol === "file:") {
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/".`;
console.warn(message);
alert(message);
}
</script>
<script>
/** LICENSE ALERT - README
* To use the library, you need to first specify a license key using the API "initLicense()" as shown below.
*/
Dynamsoft.License.LicenseManager.initLicense(
"DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"
);
/**
* 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.
* 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.
* 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.
* LICENSE ALERT - THE END
*/
/** LICENSE ALERT - README
* DCP
* To use the library, you need to first specify a license key using the API "license" as shown below.
*/
Dynamsoft.DCP.CodeParser.license =
"DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
/**
* You can contact Dynamsoft Support Team (https://www.dynamsoft.com/Company/Contact.aspx) to obtain a license.
* LICENSE ALERT - THE END
*/
// Preload "BarcodeReader" module for reading barcodes. It will save time on the initial decoding by skipping the module loading.
Dynamsoft.Core.CoreModule.loadWasm(["DBR"]);
// Preload "CodePaser" module for code parsing. It will save time on the initial parsing by skipping the module loading.
Dynamsoft.DCP.CodeParser.loadWasm();
const svgStart = document.querySelector("#svg-start");
const openImageBtn = document.querySelector("#btn-open-image");
const divUIContainer = document.querySelector("#div-ui-container");
const divResultContainer = document.querySelector(
"#div-result-container"
);
// timer for displaying MessageBox
let timer;
const init = async () => {
try {
const parser = await Dynamsoft.DCP.CodeParser.createInstance();
await parser.setCodeFormat(Dynamsoft.DCP.EnumCodeFormat.CF_AUTO);
// Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control.
const cameraView = await Dynamsoft.DCE.CameraView.createInstance();
const cameraEnhancer =
await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView);
divUIContainer.append(cameraView.getUIElement()); // Get default UI and append it to DOM.
cameraEnhancer.setScanRegion({
x: 5,
y: 5,
width: 90,
height: 90,
isMeasuredInPercentage: true,
}); // Set a scan region
// Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source.
const router =
await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
router.setInput(cameraEnhancer);
// Filter out unchecked and duplicate results.
const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter();
filter.enableResultCrossVerification(
Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE,
true
); // Filter out unchecked barcodes.
await router.addResultFilter(filter);
// Define a callback for results.
const resultReceiver = new Dynamsoft.CVR.CapturedResultReceiver();
resultReceiver.onCapturedResultReceived = async (result) => {
const resultItems = result.items;
const barcodeResult = resultItems.find(
(item) =>
item.type ===
Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE
);
if (!barcodeResult) {
if (cameraEnhancer.singleFrameMode !== "disabled") {
document.querySelector(".div-result-body").innerText =
"No PDF417 Barcode Found!";
divResultContainer.style.display = "block";
document.addEventListener("mousedown", clickToHide);
}
return;
}
if (
barcodeResult &&
cameraEnhancer.singleFrameMode === "disabled"
) {
Dynamsoft.DCE.Feedback.beep(); // Trigger a beep.
}
document.querySelector("#div-message-container").style.display =
"none";
clearTimeout(timer);
extractResultAlert(barcodeResult.bytes);
};
router.addResultReceiver(resultReceiver);
let ss = await router.getSimplifiedSettings("ReadDenseBarcodes");
ss.barcodeSettings.barcodeFormatIds =
Dynamsoft.DBR.EnumBarcodeFormat.BF_PDF417;
await router.updateSettings("ReadDenseBarcodes", ss);
return {
parser,
cameraView,
cameraEnhancer,
router,
};
} catch (ex) {
let errMsg = ex.message || ex;
console.error(errMsg);
alert(errMsg);
}
};
const pInit = init();
// decode video from camera
svgStart.addEventListener("click", async () => {
document.querySelector("h1").style.display = "none";
svgStart.style.display = "none";
await switchToVideo();
});
document
.querySelector("#div-result-container .em-close")
.addEventListener("click", closeResultAlert);
document
.querySelector("#a-switch")
.addEventListener("click", async () => {
document.querySelector("#span-tip").innerHTML =
"Click below to select a picture to read. <a href='javascript:void(0)' onclick='backToCameraMode()'>Back to camera mode</a>";
await switchToSingleFrameMode();
document.querySelector("#div-message-container").style.display =
"none";
});
document
.querySelector(".div-message-content .em-close")
.addEventListener("click", () => {
document.querySelector("#div-message-container").style.display =
"none";
});
async function backToCameraMode() {
closeResultAlert();
document.querySelector("#span-tip").innerText =
"Aim at the barcode on the driver's license.";
await switchToVideo();
}
async function switchToSingleFrameMode() {
const { cameraEnhancer, router } = await pInit;
router.stopCapturing();
cameraEnhancer.close();
cameraEnhancer.singleFrameMode = "image";
await cameraEnhancer.open();
await router.startCapturing("ReadDenseBarcodes");
}
async function switchToVideo() {
document.querySelector("#div-main").style.display = "block";
document.querySelector("#span-tip").innerText =
"Aim at the barcode on the driver's license.";
document.querySelector("#span-tip").hidden = false;
divUIContainer.style.display = "block";
const { cameraEnhancer, router } = await pInit;
router.stopCapturing();
cameraEnhancer.close();
cameraEnhancer.singleFrameMode = "disabled";
await cameraEnhancer.open();
await router.startCapturing("ReadDenseBarcodes");
timer = setTimeout(() => {
if (cameraEnhancer.singleFrameMode === "disabled") {
document.querySelector("#div-message-container").style.display =
"flex";
}
}, 3000);
}
async function extractResultAlert(bytesToParse) {
function createNode(type, txt) {
let node = document.createElement(type);
if (txt) node.innerText = txt;
return node;
}
try {
const { parser } = await pInit;
let parsedDLInfo = await parser.parseData(bytesToParse);
let resultShowNode = document.createElement("p");
let oriParseInfo;
if (parsedDLInfo.resultInfo && parsedDLInfo.resultInfo.AAMVADLInfo) {
oriParseInfo = {
...parsedDLInfo.basicPersonalInfo,
...parsedDLInfo.resultInfo,
...parsedDLInfo.resultInfo.AAMVADLInfo,
};
delete oriParseInfo.AAMVADLInfo;
} else {
oriParseInfo = {
...parsedDLInfo.basicPersonalInfo,
...parsedDLInfo.resultInfo,
};
}
for (let o in oriParseInfo) {
if (oriParseInfo[o]) {
resultShowNode.appendChild(createNode("span", o));
resultShowNode.appendChild(createNode("br"));
resultShowNode.appendChild(createNode("strong", oriParseInfo[o]));
resultShowNode.appendChild(createNode("br"));
resultShowNode.appendChild(
createNode("span", "------------------------------")
);
resultShowNode.appendChild(createNode("br"));
}
}
document.querySelector(".div-result-body").innerHTML = "";
document
.querySelector(".div-result-body")
.appendChild(resultShowNode);
divResultContainer.style.display = "block";
document.addEventListener("mousedown", clickToHide);
} catch (ex) {
alert(ex.message);
}
}
function clickToHide(e) {
if (e.target === document.body || e.target.id === "span-tip") {
closeResultAlert();
document.removeEventListener("click", arguments.callee);
}
}
async function closeResultAlert() {
divResultContainer.style.display = "none";
document.querySelector(".div-result-body").innerText = "";
const { cameraEnhancer } = await pInit;
if (cameraEnhancer.singleFrameMode === "disabled") {
timer = setTimeout(() => {
if (cameraEnhancer.singleFrameMode === "disabled") {
document.querySelector("#div-message-container").style.display =
"flex";
}
}, 3000);
}
}
</script>
</body>
</html>