/
2.read-a-drivers-license.html
493 lines (447 loc) · 20.3 KB
/
2.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
<!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">
<title>Dynamsoft Barcode Reader Simple Sample - Read Driver License</title>
</head>
<body>
<script>
if (location.protocol === "file:") {
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/".`;
console.warn(message);
alert(message);
}
</script>
<span id="hintMSG" class="hintMSG">Aim at the barcode on the driver's license.</span>
<h1 id='header'>Read A Driver's License <a href="javascript:void(0)" id='readAgain' hidden onclick="backToHome()">Retry</a></h1>
<div id="message_box" class="message_box">
<em class="message_box_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="message_box_container">
<span class="message_box_title">Can't get scanning result for a long time?</span>
<p class="message_box_content">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="confirm_btn">Have a try</a></p>
<em id="close_btn" class="message_box_closebtn"><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="UIElement" class="UIElement">
<svg id="btn-show-scanner" class="btn-show-scanner" 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" class="div-ui-container" hidden>
<svg class="dce-bg-camera" 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 class="dce-video-container"></div>
</div>
</div>
<div id="resultAlert" class="resultAlert" hidden>
<div class="resultAlert_title">Results: <em id="resultAlert_btn" 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 id="resultAlert_content" class="resultAlert_content"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.42/dist/dbr.js"></script>
<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-code-parser@2.0.20/dist/dcp.js"></script>
<script>
/** LICENSE ALERT - README
* To use the library, you need to first specify a license key using the API "license" as shown below.
*/
Dynamsoft.DBR.BarcodeReader.license = 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9';
Dynamsoft.DCP.CodeParser.license = '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=9.6.42&utm_source=github#specify-the-license or contact support@dynamsoft.com.
* LICENSE ALERT - THE END
*/
const showScannerBtn = document.getElementById('btn-show-scanner');
const openImageBtn = document.getElementById('btn-open-image');
const UIElement = document.getElementById('UIElement');
// timer for displaying MessageBox
let timer = null;
// scanner for decoding video
let pScanner = null;
// parser for parsing code
let pParser = null;
let oriParseInfo = {};
(async () => {
//Load the library on page load to speed things up.
try {
await Dynamsoft.DBR.BarcodeScanner.loadWasm();
Dynamsoft.Core.CoreModule.loadWasm(["DCP"]);
Dynamsoft.DCP.CodeParserModule.loadSpec("AAMVA_DL_ID");
Dynamsoft.DCP.CodeParserModule.loadSpec("AAMVA_DL_ID_WITH_MAG_STRIPE");
Dynamsoft.DCP.CodeParserModule.loadSpec("SOUTH_AFRICA_DL");
} catch (ex) {
let errMsg;
if (ex.message?.includes("network connection error")) {
errMsg = "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license.";
} else {
errMsg = ex.message || ex;
}
console.error(errMsg);
alert(errMsg);
}
})();
// decode video from camera
showScannerBtn.addEventListener('click', () => {
showScannerBtn.style.display = "none";
startScanning(false);
});
document.getElementById("resultAlert_btn").addEventListener("click", closeResultAlert);
document.getElementById("confirm_btn").addEventListener("click", () => {
document.getElementById('hintMSG').innerHTML = "Click below to select a picture to read. <a href='javascript:void(0)' onclick='backToCameraMode()'>Back to camera mode</a>"
startScanning(true);
document.getElementById("message_box").style.display = "none";
});
document.getElementById("close_btn").addEventListener("click", () => {
document.getElementById("message_box").style.display = "none";
});
function backToHome() {
showScannerBtn.style.display = "block";
document.getElementById('hintMSG').hidden = true;
document.getElementById('header').hidden = false;
document.getElementById('readAgain').hidden = true;
document.getElementById('resultAlert').hidden = true;
document.getElementById('div-ui-container').hidden = true;
UIElement.classList.remove("show");
UIElement.style.display = "flex";
document.getElementById('resultAlert').innerHTML = "";
}
function backToCameraMode() {
closeResultAlert();
document.getElementById('hintMSG').innerText = "Aim at the barcode on the driver's license.";
startScanning(false);
}
async function startScanning(bSingleFrameMode) {
try {
let scanner = await (pScanner = pScanner || Dynamsoft.DBR.BarcodeScanner.createInstance());
if (scanner.singleFrameMode !== bSingleFrameMode) {
scanner.hide();
scanner.singleFrameMode = bSingleFrameMode;
}
await scanner.setUIElement(document.getElementById('div-ui-container'));
if (!bSingleFrameMode) {
await scanner.updateRuntimeSettings("dense");
let settings = await scanner.getRuntimeSettings();
settings.barcodeFormatIds = Dynamsoft.DBR.EnumBarcodeFormat.BF_PDF417;
await scanner.updateRuntimeSettings(settings);
document.getElementById('header').hidden = true;
UIElement.classList.add("show");
scanner.onFrameRead = null;
} else {
scanner.onFrameRead = (result) => {
if (result.length === 0) {
document.getElementById('resultAlert_content').innerText = "No PDF417 Barcode Found!";
document.getElementById('resultAlert').hidden = false;
document.addEventListener("mousedown", clickToHide);
}
}
}
scanner.onUniqueRead = async(txt, result) => {
document.getElementById("message_box").style.display = "none";
clearTimeout(timer);
const success = await extractResultAlert(result.barcodeBytes);
if (success) {
scanner.pauseScan();
}
};
await scanner.show();
if (!bSingleFrameMode) {
document.getElementById('hintMSG').innerText = "Aim at the barcode on the driver's license."
document.getElementById('hintMSG').hidden = false;
document.getElementById('div-ui-container').hidden = false;
timer = setTimeout(() => {
document.getElementById("message_box").style.display = "flex";
}, 3000);
}
} catch (ex) {
let errMsg;
if (ex.message?.includes("network connection error")) {
errMsg = "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license.";
} else {
errMsg = ex.message || ex;
}
console.error(errMsg);
alert(errMsg);
}
}
function createNode(type, txt) {
let node = document.createElement(type);
if (txt)
node.innerText = txt;
return node;
}
async function extractResultAlert(bytesToParse) {
try {
let parser = await (pParser = pParser || Dynamsoft.DCP.CodeParser.createInstance());
let parsedDLInfo = await parser.parse(bytesToParse);
if (parsedDLInfo.exception) return false;
parsedDLInfo = JSON.parse(parsedDLInfo.jsonString);
console.log(parsedDLInfo);
let resultShowNode = document.createElement("p");
oriParseInfo = {};
if (parsedDLInfo.CodeType === "AAMVA_DL_ID") {
for (let info of parsedDLInfo.ResultInfo) {
if (info.FieldName !== "commonSubfile") continue;
if (info.ChildFields) {
wrapResultObject(info.ChildFields);
}
}
} else if (parsedDLInfo.CodeType === "AAMVA_DL_ID_WITH_MAG_STRIPE") {
for (let info of parsedDLInfo.ResultInfo) {
if (info.FieldName.includes("track")) {
if (info.ChildFields) {
wrapResultObject(info.ChildFields);
}
}
}
} else if (parsedDLInfo.CodeType === "SOUTH_AFRICA_DL") {
for (let info of parsedDLInfo.ResultInfo) {
oriParseInfo[info.FieldName] = info.Value;
if (info.ChildFields) {
wrapResultObject(info.ChildFields);
}
}
}
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.getElementById('resultAlert_content').innerHTML = "";
document.getElementById('resultAlert_content').appendChild(resultShowNode);
document.getElementById('resultAlert').hidden = false;
document.addEventListener("mousedown", clickToHide);
return true;
} catch (ex) {
alert(ex.message);
return false;
}
}
function wrapResultObject(childFields) {
for (let childField of childFields) {
for (let field of childField) {
if (!["dataElementSeparator", "segmentTerminator", "subfile", "subfileType"].includes(field.FieldName)) {
oriParseInfo[field.FieldName] = field.Value;
}
if (field.ChildFields) {
wrapResultObject(field.ChildFields);
}
}
}
}
function clickToHide(e) {
if (e.target === document.body || e.target.id === "hintMSG") {
closeResultAlert();
document.removeEventListener("click", arguments.callee);
}
}
async function closeResultAlert() {
document.getElementById('resultAlert').hidden = true;
document.getElementById('resultAlert_content').innerText = "";
let scanner = await pScanner;
if (!scanner.singleFrameMode) {
timer = setTimeout(() => {
document.getElementById("message_box").style.display = "flex";
}, 3000);
}
scanner.resumeScan();
}
</script>
</body>
<style>
* {
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: hidden;
width: 100vw;
height: 100vh;
margin: 0;
}
.hintMSG {
position: absolute;
font-size: 1rem;
color: #fe8e14;
top: 1vh;
}
h1 {
font-size: 1.5em;
}
a {
color: #fe8d14;
}
a:hover {
color: #fe8d14a1;
}
.btn-show-scanner {
width: 100px;
height: 100px;
fill: #aaa;
cursor: pointer;
}
.UIElement {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
width: 50vw;
height: 60vh;
margin: 0;
}
.UIElement button {
width: 50%;
margin: 2vmin;
padding: 2vmin;
font-size: xx-large;
}
.UIElement img {
height: 15vmin;
float: left;
margin: 15vmin;
cursor: pointer;
}
.UIElement img:hover {
background-color: #fe8d14a1;
}
.show {
height: 60vh;
width: 50vw;
top: 0;
}
.dce-bg-camera {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
width: 40%;
height: 40%;
fill: #aaa;
}
.div-ui-container,
.dce-video-container {
position: relative;
width: 100%;
height: 100%;
}
.resultAlert {
position: fixed;
height: 70vh;
width: 50vw;
z-index: 10;
color: white;
font-size: small;
background: #222222;
margin-top: 30px;
}
.resultAlert_title {
position: fixed;
width: 50vw;
height: 30px;
background-color: #000;
font-size: 16px;
line-height: 30px;
padding-left: 10px;
box-sizing: border-box;
}
.resultAlert_title i {
top: 7px;
}
.resultAlert_content {
height: calc(70vh - 30px);
overflow-y: auto;
margin-top: 30px;
padding: 5px;
box-sizing: border-box;
}
.resultAlert p {
padding: 5px;
margin: 0;
}
strong {
color: rgb(254, 142, 20);
}
/* MessageBox */
.message_box {
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 .2s;
}
/* Small devices */
@media only screen and (max-width: 1000px) {
.message_box {
width: 90vw;
}
.UIElement,
.show {
width: 80vw;
}
.resultAlert,
.resultAlert_title {
width: 80vw;
}
}
.message_box_icon {
font-size: 28px;
width: 28px;
line-height: 0;
}
.message_box_container {
display: table-cell;
padding: 0 8px;
}
.message_box_title {
font-weight: 700;
font-size: 14px;
line-height: 18px;
}
.message_box_content {
font-size: 13px;
margin: 5px 0 0;
line-height: 20px;
}
.api_highlight {
font-weight: 700;
background-color: #ddd;
border-radius: 4px;
padding: 2px;
}
.message_box_closebtn {
position: absolute;
top: 12px;
right: 15px;
cursor: pointer;
line-height: 0;
}
</style>
</html>