-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.html
More file actions
284 lines (251 loc) · 13.1 KB
/
index.html
File metadata and controls
284 lines (251 loc) · 13.1 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
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
<!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 GS1-AI barcodes and Parse It with Dynamsoft Barcode Reader." />
<meta name="keywords" content="read GS1 AI barcode" />
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/scenarios/read-and-parse-GS1-AI/scan-using-foundational-api/index.html" />
<link rel="stylesheet" href="./index.css">
<title>Dynamsoft Barcode Reader Sample - Read a GS1-AI</title>
</head>
<body>
<span id="span-tip">Click the play button to activate the camera.</span>
<h1>Read a GS1-AI</h1>
<div id="message-container">
<em class="message-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-content">
<span>Can't get scanning result for a long time?</span>
<p>
Enable
<span class="api_highlight">singleFrameMode</span> to capture high-definition images with your camera (mobile
device) or upload a local image for better recognition.
<a href="javascript:void(0)" id="toggle-scan-mode">Give it a try!</a>
</p>
<em class="close-button"><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="main-container">
<svg id="svg-start" t="1742347975823" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2701" width="50" height="50">
<path d="M169.258667 904.533333V119.466667A102.4 102.4 0 0 1 333.098667 37.546667l523.349333 392.533333a102.4 102.4 0 0 1 0 163.84l-523.349333 392.533333A102.4 102.4 0 0 1 169.258667 904.533333z" p-id="2702"></path>
</svg>
<span id="loading">Loading...</span>
<div id="camera-view-container"></div>
</div>
<div id="result-container">
<div class="result-header">
Results:
<em class="close-button" 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="result-body"></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>
/** 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=samples&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/docs/web/programming/javascript/user-guide/index.html?ver=11.4.2000&cVer=true#specify-the-license&utm_source=samples or contact support@dynamsoft.com.
* 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();
Dynamsoft.DCP.CodeParserModule.loadSpec("GS1_AI");
const svgStart = document.querySelector("#svg-start");
const loading = document.querySelector("#loading");
const openImageBtn = document.querySelector("#btn-open-image");
const cameraViewContainer = document.querySelector("#camera-view-container");
// Result HTML Elements
const resultContainer = document.querySelector("#result-container");
const resultBody = document.querySelector(".result-body");
// Message Box HTML Elements
const messageContainer = document.querySelector("#message-container");
// timer for displaying MessageBox
let messageDisplayTimer;
let oriParseInfo = {};
const init = async () => {
const parser = await Dynamsoft.DCP.CodeParser.createInstance();
// 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);
cameraEnhancer.setResolution({ width: 1920, height: 1080 });
cameraViewContainer.append(cameraView.getUIElement()); // Get default UI and append it to DOM.
// Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source.
const cvRouter = await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
await cvRouter.initSettings("./read_and_parse_gs1.json");
cvRouter.setInput(cameraEnhancer);
// Filter out unchecked and duplicate results.
const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter();
// Filter out duplicate barcodes within 3 seconds.
filter.enableResultDeduplication("barcode", true);
await cvRouter.addResultFilter(filter);
// Define a callback for results.
await cvRouter.addResultReceiver({
// callback in every frame
onCapturedResultReceived: (result) => {
if (cameraEnhancer.singleFrameMode === "disabled") {
return;
}
// Report no barcode in singleFrameMode
if (!result.items.some((item) => item.type === Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE)) {
resultBody.innerText = "No GS1 Barcode Found!";
resultContainer.style.display = "block";
document.addEventListener("mousedown", clickToHide);
}
},
onParsedResultsReceived: async (result) => {
if (!result.parsedResultItems.length) return;
Dynamsoft.DCE.Feedback.beep();
messageContainer.style.display = "none";
clearTimeout(messageDisplayTimer);
const success = await extractResultAlert(result.parsedResultItems[0]);
if (success) {
cvRouter.stopCapturing(result);
}
}
});
return {
parser,
cameraView,
cameraEnhancer,
cvRouter,
};
};
const pInit = init();
// decode video from camera
svgStart.addEventListener("click", async () => {
document.querySelector("h1").style.display = "none";
svgStart.style.display = "none";
loading.style.display = "inline";
await switchToVideo();
});
document.querySelector("#result-container .close-button").addEventListener("click", closeResultAlert);
document.querySelector("#toggle-scan-mode").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();
messageContainer.style.display = "none";
});
document.querySelector(".message-content .close-button").addEventListener("click", () => {
messageContainer.style.display = "none";
});
async function backToCameraMode() {
closeResultAlert();
document.querySelector("#span-tip").innerText = "Aim at the GS1 barcode.";
await switchToVideo();
}
async function switchToSingleFrameMode() {
const { cameraEnhancer, cvRouter, cameraView } = await pInit;
cvRouter.stopCapturing();
cameraEnhancer.close();
cameraEnhancer.singleFrameMode = "image";
await cameraEnhancer.open();
cameraView.setScanLaserVisible(true);
await cvRouter.startCapturing("ReadGS1AI");
}
async function switchToVideo() {
document.querySelector("#main-container").style.display = "block";
document.querySelector("#span-tip").innerText = "Aim at the GS1 barcode.";
document.querySelector("#span-tip").hidden = false;
cameraViewContainer.style.display = "block";
const { cameraEnhancer, cvRouter, cameraView } = await pInit;
loading.style.display = "none";
cvRouter.stopCapturing();
cameraEnhancer.close();
cameraEnhancer.singleFrameMode = "disabled";
await cameraEnhancer.open();
cameraView.setScanLaserVisible(true);
await cvRouter.startCapturing("ReadGS1AI");
messageDisplayTimer = setTimeout(() => {
if (cameraEnhancer.singleFrameMode === "disabled") {
messageContainer.style.display = "flex";
}
}, 3000);
}
async function extractResultAlert(result) {
function createNode(type, txt) {
let node = document.createElement(type);
if (txt) node.innerText = txt;
return node;
}
try {
console.log(result);
parsedDLInfo = JSON.parse(result.jsonString);
let resultShowNode = document.createElement("p");
oriParseInfo = {};
if (parsedDLInfo.CodeType === "GS1_AI") {
for (let info of parsedDLInfo.ResultInfo) {
if(info.FieldName && info.Value) {
oriParseInfo[info.FieldName] = info.Value;
}
if (info.ChildFields) {
wrapChildFields(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"));
}
}
resultBody.innerHTML = "";
resultBody.appendChild(resultShowNode);
resultContainer.style.display = "block";
document.addEventListener("mousedown", clickToHide);
return true;
} catch (ex) {
alert(ex.message);
return false;
}
}
function wrapChildFields(childFields) {
for (let childField of childFields) {
for (let field of childField) {
oriParseInfo[field.FieldName] = field.Value;
if (field.ChildFields) {
wrapChildFields(field.ChildFields);
}
}
}
}
function clickToHide(e) {
if (e.target === document.body || e.target.id === "span-tip") {
closeResultAlert();
document.removeEventListener("click", arguments.callee);
}
}
async function closeResultAlert() {
resultContainer.style.display = "none";
resultBody.innerText = "";
const { cameraEnhancer, cvRouter } = await pInit;
if (cameraEnhancer.singleFrameMode === "disabled") {
messageDisplayTimer = setTimeout(() => {
if (cameraEnhancer.singleFrameMode === "disabled") {
messageContainer.style.display = "flex";
}
}, 3000);
}
await cvRouter.startCapturing("ReadGS1AI");
}
</script>
</body>
</html>