-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.html
More file actions
153 lines (138 loc) · 5.45 KB
/
index.html
File metadata and controls
153 lines (138 loc) · 5.45 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
<!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="" />
<meta name="keywords" content="read vin" />
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/scenarios/read-vin/index.html" />
<link rel="stylesheet" href="./style.css" />
<title>Dynamsoft Barcode Reader Sample - Read a VIN</title>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.4.2000/dist/dbr.bundle.js"></script>
</head>
<body>
<div class="home-page">
<h1 class="home-page-title">Read VIN</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>
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 barcodeScannerView = document.querySelector(".barcode-scanner-view");
const backToScan = document.querySelector(".back-to-scan");
Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
Dynamsoft.DCP.CodeParserModule.loadSpec("VIN");
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 = handleVINResult(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_vin.json");
barcodeScannerView.append(cameraView.getUIElement());
await cameraEnhancer.open();
await cvRouter.startCapturing("ReadVIN");
homePage.style.display = "none";
} catch (ex) {
console.log(ex.message || ex);
alert(ex.message || ex);
}
})();
async function start() {
await pInit;
await cameraEnhancer.open();
await cvRouter.startCapturing("ReadVIN");
homePage.style.display = "none";
}
function handleVINResult(parsedResult) {
const oriParseInfo = [];
const resultInfo = JSON.parse(parsedResult.jsonString);
for (let info of resultInfo.ResultInfo) {
if (info.ChildFields) {
wrapResultObject(info.ChildFields);
}
}
function wrapResultObject(childFields) {
for (let childField of childFields) {
for (let field of childField) {
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>