-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.html
More file actions
441 lines (381 loc) · 18.9 KB
/
Copy pathindex.html
File metadata and controls
441 lines (381 loc) · 18.9 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Scan & Search</title>
<link rel="stylesheet" href="./index.css">
<!-- Include Dynamsoft Barcode Reader Bundle via CDN -->
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.4.3000/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> -->
</head>
<body>
<h1>📦 Instant Product Information Lookup</h1>
<h2>Scan or enter a barcode to search for product data</h2>
<!-- Always-visible camera view -->
<div id="barcode-scanner-view">
<!-- Transparent overlay covering the full view; center click toggles play/pause -->
<div id="camera-toggle-area" class="loading" aria-hidden="true">
<div id="loading-spinner"></div>
<div id="play-pause-icon" role="button" tabindex="0" aria-label="Toggle scanning"></div>
</div>
</div>
<!-- Filter toggle -->
<div id="filter-row">
<button id="filter-toggle-btn" aria-expanded="false" aria-controls="filters-panel">
<svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
<path d="M1 2h14l-5 6v5l-4-2V8L1 2z"/>
</svg>
Filters
<span id="filter-badge" hidden aria-label="Filters active">●</span>
</button>
</div>
<!-- Collapsible filter panel -->
<div id="filters-panel" hidden aria-label="Scan filters">
<!-- 1. Barcode format -->
<section class="filter-section">
<h3 class="filter-section-title">Barcode Format</h3>
<div class="preset-group" role="group" aria-label="Format preset">
<button class="preset-btn active" data-preset="all">All</button>
<button class="preset-btn" data-preset="retail">1D Retail</button>
<button class="preset-btn" data-preset="industrial">1D Industrial</button>
<button class="preset-btn" data-preset="2d">2D Codes</button>
<button class="preset-btn" data-preset="custom">Custom</button>
</div>
<div id="custom-formats-wrapper" hidden>
<div id="custom-formats-grid"></div>
</div>
</section>
<!-- 2. String length -->
<section class="filter-section">
<h3 class="filter-section-title">String Length</h3>
<div class="length-inputs">
<label class="length-label">
Min
<input type="number" id="min-length" min="0" placeholder="Any" />
</label>
<span class="length-dash">–</span>
<label class="length-label">
Max
<input type="number" id="max-length" min="0" placeholder="Any" />
</label>
<span class="length-unit">chars</span>
</div>
</section>
<!-- 3. Keyword rules -->
<section class="filter-section">
<h3 class="filter-section-title">Keyword Rules <span class="filter-hint">(all must match)</span></h3>
<div id="keyword-rules-list" aria-label="Keyword rules"></div>
<button id="add-rule-btn" class="add-rule-btn">+ Add Rule</button>
</section>
</div>
<!-- Input bar + search button -->
<div id="container">
<input type="text" id="text-input" placeholder="e.g. 049000042511" />
<button id="search-btn" aria-label="Search">
<div class="search-icon"></div>
</button>
</div>
<textarea id="search-result" placeholder="Search Result..."></textarea>
<script>
// ── BF ID constants ────────────────────────────────────────────────────────
const ALL_2D_BF = ['BF_PDF417', 'BF_QR_CODE', 'BF_DATAMATRIX', 'BF_AZTEC', 'BF_MICRO_QR', 'BF_MICRO_PDF417', 'BF_DOTCODE'];
const ALL_1D_BF = ['BF_EAN_13', 'BF_EAN_8', 'BF_UPC_A', 'BF_UPC_E', 'BF_CODE_128', 'BF_CODE_39', 'BF_CODE_93', 'BF_ITF', 'BF_CODABAR', 'BF_GS1_DATABAR'];
// Display name → BF ID (1D only — 2D formats are not exposed in the filter UI)
const FORMAT_TO_BF = {
"EAN-13": "BF_EAN_13",
"EAN-8": "BF_EAN_8",
"UPC-A": "BF_UPC_A",
"UPC-E": "BF_UPC_E",
"Code 128": "BF_CODE_128",
"Code 39": "BF_CODE_39",
"Code 93": "BF_CODE_93",
"ITF": "BF_ITF",
"Codabar": "BF_CODABAR",
"QR Code": "BF_QR_CODE",
"PDF417": "BF_PDF417",
"DataMatrix":"BF_DATAMATRIX",
"Aztec": "BF_AZTEC",
};
// Preset → 1D BF ID array (written to BarcodeReaderTaskSettingOptions[0].BarcodeFormatIds)
const PRESET_FORMAT_IDS = {
all: [...ALL_1D_BF, ...ALL_2D_BF],
retail: ['BF_EAN_13', 'BF_EAN_8', 'BF_UPC_A', 'BF_UPC_E'],
industrial: ['BF_CODE_128', 'BF_CODE_39', 'BF_CODE_93', 'BF_ITF', 'BF_CODABAR'],
"2d": ALL_2D_BF,
};
const ALL_FORMATS = Object.keys(FORMAT_TO_BF);
// ── Filter state ───────────────────────────────────────────────────────────
let activePreset = "all";
let customSelectedBF = new Set(Object.values(FORMAT_TO_BF)); // all checked by default
let keywordRules = [];
let ruleIdCounter = 0;
let scanner = null;
let isCapturing = false;
let isApplyingFilters = false;
let filterApplyTimer = null;
// ── DOM refs ───────────────────────────────────────────────────────────────
const searchBtn = document.getElementById("search-btn");
const inputBox = document.getElementById("text-input");
const barcodeScannerView = document.getElementById("barcode-scanner-view");
const cameraToggleArea = document.getElementById("camera-toggle-area");
const playPauseIcon = document.getElementById("play-pause-icon");
const searchResult = document.getElementById("search-result");
const filterToggleBtn = document.getElementById("filter-toggle-btn");
const filtersPanel = document.getElementById("filters-panel");
const filterBadge = document.getElementById("filter-badge");
const customFormatsWrapper = document.getElementById("custom-formats-wrapper");
const customFormatsGrid = document.getElementById("custom-formats-grid");
const minLengthInput = document.getElementById("min-length");
const maxLengthInput = document.getElementById("max-length");
const keywordRulesList = document.getElementById("keyword-rules-list");
const addRuleBtn = document.getElementById("add-rule-btn");
// ── SDK filter application ─────────────────────────────────────────────────
function getActiveFormatIds() {
if (activePreset !== "custom") {
return PRESET_FORMAT_IDS[activePreset] ?? PRESET_FORMAT_IDS.all;
}
return [...ALL_1D_BF, ...ALL_2D_BF].filter(id => customSelectedBF.has(id));
}
function buildRegexPattern(rules) {
const parts = [];
for (const rule of rules) {
const val = rule.value.trim();
if (!val) continue;
const esc = val.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
switch (rule.type) {
case "contains": parts.push(`(?=.*${esc})`); break;
case "not-contains": parts.push(`(?!.*${esc})`); break;
case "starts-with": parts.push(`(?=${esc})`); break;
case "ends-with": parts.push(`(?=.*${esc}$)`); break;
case "regex":
try { new RegExp(val); parts.push(`(?=${val})`); } catch {}
break;
}
}
return parts.length ? `^${parts.join("")}[\\s\\S]*$` : "";
}
async function applySDKFilters() {
if (!scanner || isApplyingFilters) return;
isApplyingFilters = true;
try {
const { cvRouter } = scanner;
// 1. Stop capturing if active
const wasCapturing = isCapturing;
if (wasCapturing) {
cvRouter.stopCapturing();
isCapturing = false;
}
// 2. Get current settings for the template
const tempSetting = await cvRouter.outputSettings("ReadSingleBarcode", true);
// 3a. Format filter — BarcodeReaderTaskSettingOptions[0].BarcodeFormatIds
tempSetting.BarcodeReaderTaskSettingOptions[0].BarcodeFormatIds = getActiveFormatIds();
// 3b. String length filter — BarcodeFormatSpecificationOptions[0].BarcodeTextLengthRangeArray
const minLen = minLengthInput.value !== "" ? parseInt(minLengthInput.value, 10) : null;
const maxLen = maxLengthInput.value !== "" ? parseInt(maxLengthInput.value, 10) : null;
const lengthRange = (minLen !== null || maxLen !== null)
? [{ MinValue: minLen ?? 0, MaxValue: maxLen ?? 999999 }]
: [];
tempSetting.BarcodeFormatSpecificationOptions[1].BarcodeTextLengthRangeArray = lengthRange;
// 3c. Keyword / regex filter — BarcodeFormatSpecificationOptions[0].BarcodeTextRegExPattern
const pattern = buildRegexPattern(keywordRules);
tempSetting.BarcodeFormatSpecificationOptions[1].BarcodeTextRegExPattern = pattern;
// 4. Apply updated settings
await cvRouter.initSettings(tempSetting);
// 5. Resume capturing if it was active before
if (wasCapturing) {
await cvRouter.startCapturing("ReadSingleBarcode");
isCapturing = true;
}
} finally {
isApplyingFilters = false;
}
}
// Debounced wrapper — used by text inputs to avoid applying on every keystroke
function scheduleFilterApply() {
updateBadge();
clearTimeout(filterApplyTimer);
filterApplyTimer = setTimeout(() => {
if (isCapturing) applySDKFilters();
}, 400);
}
// Immediate wrapper — used by discrete controls (preset buttons, checkboxes)
function onFilterChanged() {
updateBadge();
if (isCapturing) applySDKFilters();
}
// ── Build custom format checkboxes ─────────────────────────────────────────
ALL_FORMATS.forEach(fmt => {
const bfId = FORMAT_TO_BF[fmt];
const label = document.createElement("label");
label.className = "format-checkbox-label";
const cb = document.createElement("input");
cb.type = "checkbox";
cb.checked = true;
cb.addEventListener("change", () => {
if (cb.checked) customSelectedBF.add(bfId);
else customSelectedBF.delete(bfId);
onFilterChanged();
});
label.append(cb, fmt);
customFormatsGrid.append(label);
});
// ── Preset buttons ─────────────────────────────────────────────────────────
document.querySelectorAll(".preset-btn").forEach(btn => {
btn.addEventListener("click", () => {
document.querySelectorAll(".preset-btn").forEach(b => b.classList.remove("active"));
btn.classList.add("active");
activePreset = btn.dataset.preset;
customFormatsWrapper.hidden = activePreset !== "custom";
onFilterChanged();
});
});
// ── Filter panel toggle ────────────────────────────────────────────────────
filterToggleBtn.addEventListener("click", () => {
const opening = filtersPanel.hidden;
filtersPanel.hidden = !opening;
filterToggleBtn.setAttribute("aria-expanded", String(opening));
filterToggleBtn.classList.toggle("open", opening);
});
// ── Length inputs ──────────────────────────────────────────────────────────
minLengthInput.addEventListener("input", scheduleFilterApply);
maxLengthInput.addEventListener("input", scheduleFilterApply);
// ── Keyword rules ──────────────────────────────────────────────────────────
function addKeywordRule(type = "contains", value = "") {
const rule = { id: ruleIdCounter++, type, value };
keywordRules.push(rule);
renderKeywordRule(rule);
updateBadge();
}
function removeKeywordRule(ruleId) {
keywordRules = keywordRules.filter(r => r.id !== ruleId);
document.getElementById(`rule-${ruleId}`)?.remove();
onFilterChanged();
}
function renderKeywordRule(rule) {
const row = document.createElement("div");
row.className = "rule-row";
row.id = `rule-${rule.id}`;
const typeSelect = document.createElement("select");
typeSelect.className = "rule-type-select";
typeSelect.setAttribute("aria-label", "Rule type");
[
["contains", "contains"],
["not-contains", "does not contain"],
["starts-with", "starts with"],
["ends-with", "ends with"],
["regex", "matches regex"],
].forEach(([val, label]) => {
const opt = new Option(label, val);
opt.selected = val === rule.type;
typeSelect.add(opt);
});
typeSelect.addEventListener("change", () => {
rule.type = typeSelect.value;
onFilterChanged();
});
const valueInput = document.createElement("input");
valueInput.type = "text";
valueInput.className = "rule-value-input";
valueInput.placeholder = "e.g. SN-";
valueInput.value = rule.value;
valueInput.setAttribute("aria-label", "Rule value");
valueInput.addEventListener("input", () => {
rule.value = valueInput.value;
scheduleFilterApply();
});
const removeBtn = document.createElement("button");
removeBtn.className = "rule-remove-btn";
removeBtn.textContent = "✕";
removeBtn.setAttribute("aria-label", "Remove rule");
removeBtn.addEventListener("click", () => removeKeywordRule(rule.id));
row.append(typeSelect, valueInput, removeBtn);
keywordRulesList.append(row);
}
addRuleBtn.addEventListener("click", () => addKeywordRule());
// ── Active filter badge ────────────────────────────────────────────────────
function hasActiveFilters() {
if (activePreset !== "all") return true;
if (minLengthInput.value || maxLengthInput.value) return true;
if (keywordRules.some(r => r.value.trim())) return true;
return false;
}
function updateBadge() {
filterBadge.hidden = !hasActiveFilters();
}
// ── Play / pause toggle UI ─────────────────────────────────────────────────
function updateToggleUI() {
cameraToggleArea.classList.remove("loading", "paused");
if (!isCapturing) cameraToggleArea.classList.add("paused");
cameraToggleArea.setAttribute("aria-label", isCapturing ? "Pause scanning" : "Resume scanning");
}
async function toggleCapturing() {
if (!scanner || cameraToggleArea.classList.contains("loading")) return;
if (isCapturing) {
scanner.cvRouter.stopCapturing();
scanner.cameraEnhancer.pause();
isCapturing = false;
} else {
await applySDKFilters();
await scanner.cameraEnhancer.resume();
await scanner.cvRouter.startCapturing("ReadSingleBarcode");
isCapturing = true;
}
updateToggleUI();
}
// ── Placeholder lookup ─────────────────────────────────────────────────────
function lookupBarcode(text, format) {
const formatLine = format ? `Format: ${format}\n\n` : "";
return `Barcode Text to Query from Database:\n\n${text}\n\n${formatLine}-------------------\n\nProduct name: xxx\n\nBrand: xxx\n\nCategory: xxx\n\nPrice: xxx\n\nDescription: xxx`;
}
// ── SDK initialisation ─────────────────────────────────────────────────────
Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
const pInit = (async () => {
const cameraView = await Dynamsoft.DCE.CameraView.createInstance();
const cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView);
const cvRouter = await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
cvRouter.setInput(cameraEnhancer);
// Attach camera UI behind the toggle overlay
barcodeScannerView.insertBefore(cameraView.getUIElement(), cameraToggleArea);
// Deduplicate so the same barcode doesn't fire repeatedly between filter applies
const dedupeFilter = new Dynamsoft.Utility.MultiFrameResultCrossFilter();
dedupeFilter.enableResultCrossVerification("barcode", true);
dedupeFilter.enableResultDeduplication("barcode", true);
await cvRouter.addResultFilter(dedupeFilter);
const resultReceiver = new Dynamsoft.CVR.CapturedResultReceiver();
resultReceiver.onDecodedBarcodesReceived = (result) => {
if (!result.barcodeResultItems.length) return;
// SDK-level filters have already applied; accept the first result
const item = result.barcodeResultItems[0];
// Fill the input only; user clicks search to query
inputBox.value = item.text;
// Pause scanning after a successful read so the user can review the result
cvRouter.stopCapturing();
cameraEnhancer.pause();
isCapturing = false;
updateToggleUI();
};
await cvRouter.addResultReceiver(resultReceiver);
scanner = { cameraView, cameraEnhancer, cvRouter };
// Auto-open camera and start capturing on page load
await cameraEnhancer.open();
await applySDKFilters();
await cvRouter.startCapturing("ReadSingleBarcode");
isCapturing = true;
updateToggleUI();
return scanner;
})();
// ── Event listeners ────────────────────────────────────────────────────────
playPauseIcon.addEventListener("click", toggleCapturing);
playPauseIcon.addEventListener("keydown", e => {
if (e.key === "Enter" || e.key === " ") { e.preventDefault(); toggleCapturing(); }
});
searchBtn.addEventListener("click", () => {
const text = inputBox.value.trim();
if (!text) return;
searchResult.value = lookupBarcode(text);
});
</script>
</body>
</html>