-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.html
More file actions
178 lines (163 loc) · 6.63 KB
/
index.html
File metadata and controls
178 lines (163 loc) · 6.63 KB
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
<!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="Instantly Read PDF417 barcodes on Driver Licenses with Dynamsoft Barcode Reader." />
<meta name="keywords" content="read driver license barcode" />
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/scenarios/read-a-drivers-license/index.html" />
<title>Dynamsoft Barcode Reader Sample - Read a Driver's License</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<div class="home-page">
<h1 class="home-page-title">Read a Driver's License</h1>
<div class="start-btn">
<div class="start-icon"></div>
<div class="loading"></div>
</div>
<img class="logo-dynamsoft" src="./logo-dynamsoft.png" alt="logo-dynamsoft">
</div>
<div class="barcode-scanner-view"></div>
<div class="parsed-result-view">
<div class="barcode-text"></div>
<ul class="parsed-result-list"></ul>
<div class="back-to-scan" style="cursor: pointer;">Back to Scan</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.4.2000/dist/dbr.bundle.js"></script>
<!-- If the network is unstable or you prefer to self-host the SDK, uncomment the line below to load it locally -->
<!-- <script src="../../../dist/dbr.bundle.js"></script> -->
<script>
const parsedResultView = document.querySelector(".parsed-result-view");
const homePage = document.querySelector(".home-page");
const startIcon = document.querySelector(".start-icon");
const startBtn = document.querySelector(".start-btn");
const loading = document.querySelector(".loading");
const barcodeText = document.querySelector(".barcode-text");
const parsedResultList = document.querySelector(".parsed-result-list");
const backToScan = document.querySelector(".back-to-scan");
const barcodeScannerView = document.querySelector(".barcode-scanner-view");
Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
Dynamsoft.DCP.CodeParserModule.loadSpec(["AAMVA_DL_ID", "SOUTH_AFRICA_DL"]);
let cameraView, cameraEnhancer, cvRouter, parser;
let pInit = (async function () {
try {
cameraView = await Dynamsoft.DCE.CameraView.createInstance();
cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView);
cvRouter = await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
cvRouter.setInput(cameraEnhancer);
const resultReceiver = new Dynamsoft.CVR.CapturedResultReceiver();
resultReceiver.onDecodedBarcodesReceived = async (result) => {
if (result.barcodeResultItems.length > 0) {
let processedResult;
try {
parser = await Dynamsoft.DCP.CodeParser.createInstance();
const parsedResult = await parser.parse(result.barcodeResultItems[0].bytes);
processedResult = handleDLResult(parsedResult);
} catch (ex) {
processedResult = [
{ description: "Error", value: ex.message || "parse failed." },
{
description: "Original text",
value: result.barcodeResultItems[0].text,
},
]
} finally {
generateResultList(processedResult);
barcodeText.innerText = result.barcodeResultItems[0].text.replace(/\n/g, "");
}
parsedResultView.style.display = "block";
barcodeScannerView.style.display = "none";
cameraView.clearAllInnerDrawingItems();
cvRouter.stopCapturing();
}
}
await cvRouter.addResultReceiver(resultReceiver);
await cvRouter.initSettings("./read_dl.json");
barcodeScannerView.append(cameraView.getUIElement());
} catch (ex) {
console.log(ex.message || ex);
alert(ex.message || ex);
}
})();
async function start() {
await pInit;
await cameraEnhancer.open();
await cvRouter.startCapturing("ReadDriversLicense");
homePage.style.display = "none";
}
function handleDLResult(parsedResult) {
const oriParseInfo = [];
const resultInfo = JSON.parse(parsedResult.jsonString);
if (parsedResult.codeType === "AAMVA_DL_ID") {
for (let info of resultInfo.ResultInfo) {
if (info.FieldName !== "commonSubfile") continue;
if (info.ChildFields) {
wrapResultObject(info.ChildFields);
}
}
} else if (parsedResult.codeType === "AAMVA_DL_ID_WITH_MAG_STRIPE") {
for (let info of resultInfo.ResultInfo) {
if (info.FieldName.includes("track")) {
if (info.ChildFields) {
wrapResultObject(info.ChildFields);
}
}
}
} else if (parsedResult.codeType === "SOUTH_AFRICA_DL") {
for (let info of resultInfo.ResultInfo) {
if (info.ChildFields) {
wrapResultObject(info.ChildFields);
} else {
if (info.Value) {
oriParseInfo.push({
description: info.FieldName,
value: info.Value,
});
}
}
}
}
function wrapResultObject(childFields) {
for (let childField of childFields) {
for (let field of childField) {
if (!["dataElementSeparator", "segmentTerminator", "subfile", "subfileType"].includes(field.FieldName)) {
if (field.Value) {
oriParseInfo.push({
description: field.FieldName,
value: field.Value,
});
}
}
if (field.ChildFields) {
wrapResultObject(field.ChildFields);
}
}
}
}
return oriParseInfo;
}
function generateResultList(processedResult) {
parsedResultList.innerHTML = "";
const resultList = [];
for (let item of processedResult) {
const resultItem = document.createElement("li");
resultItem.className = "parsed-result-item";
resultItem.innerHTML = `<strong>${item.description}: </strong><span>${item.value}</span>`;
resultList.push(resultItem);
}
parsedResultList.append(...resultList);
}
startBtn.addEventListener("click", () => {
loading.style.display = "block";
startIcon.style.display = "none";
start();
})
backToScan.addEventListener("click", () => {
parsedResultView.style.display = "none";
barcodeScannerView.style.display = "block";
start();
})
</script>
</body>
</html>