-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello-world-js10.html
124 lines (109 loc) · 5.75 KB
/
hello-world-js10.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
<!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="Quickly read barcodes with Dynamsoft Barcode Reader from a live camera stream." />
<meta name="keywords" content="barcode, camera" />
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/hello-world/hello-world.html" />
<title>Dynamsoft Barcode Reader Sample - Hello World (Decode via Camera)</title>
</head>
<body>
<h1>Hello World JS 10 (Decode via Camera)</h1>
<div id="camera-view-container" style="width: 100%; height: 80vh"></div>
Results:<br />
<div id="results" style="width: 100%; height: 10vh; overflow: auto; white-space: pre-wrap"></div>
<button id="getScanCountBtn">Get Scan Count</button>
<div id="scanCount"></div>
<!-- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/dbr.bundle.js"></script> -->
<script type="module" src="./test.js"> </script>
<script type="module">
/** LICENSE ALERT - README
* To use the library, you need to first specify a license key using the API "initLicense()" as shown below.
*/
// const license = 'DLS2eyJoYW5kc2hha2VDb2RlIjoiMTAyOTc1MDEyLVRYbFhaV0pRY205cSIsIm1haW5TZXJ2ZXJVUkwiOiJodHRwczovL21kbHMuZHluYW1zb2Z0b25saW5lLmNvbS8iLCJvcmdhbml6YXRpb25JRCI6IjEwMjk3NTAxMiIsInN0YW5kYnlTZXJ2ZXJVUkwiOiJodHRwczovL3NkbHMuZHluYW1zb2Z0b25saW5lLmNvbS8iLCJjaGVja0NvZGUiOi0xMzA1NTA5MjJ9';
// Dynamsoft.License.LicenseManager.initLicense(license);
/**
* 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/programming/javascript/user-guide/?ver=10.2.10&utm_source=samples#specify-the-license or contact [email protected].
* LICENSE ALERT - THE END
*/
import { loadDynamsoftSDK } from './test.js';
// Call the loadDynamsoftSDK function
loadDynamsoftSDK()
.then(() => {
// Optional. Used to load wasm resources in advance, reducing latency between video playing and barcode decoding.
// Dynamsoft.Core.CoreModule.loadWasm(["DBR"]);
// Defined globally for easy debugging.
let cameraEnhancer, cvRouter;
(async () => {
try {
// Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control.
const cameraView = await Dynamsoft.DCE.CameraView.createInstance();
cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView);
// Get default UI and append it to DOM.
document.querySelector("#camera-view-container").append(cameraView.getUIElement());
// Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source.
cvRouter = await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
cvRouter.setInput(cameraEnhancer);
// Define a callback for results.
cvRouter.addResultReceiver({
onDecodedBarcodesReceived: (result) => {
if (!result.barcodeResultItems.length) return;
const resultsContainer = document.querySelector("#results");
resultsContainer.textContent = "";
console.log(result);
for (let item of result.barcodeResultItems) {
resultsContainer.textContent += `${item.formatString}: ${item.text}\n\n`;
console.log(item.text);
}
},
});
// Filter out unchecked and duplicate results.
const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter();
// Filter out unchecked barcodes.
filter.enableResultCrossVerification("barcode", true);
// Filter out duplicate barcodes within 3 seconds.
filter.enableResultDeduplication("barcode", true);
filter.setDuplicateForgetTime("barcode", 10000);
await cvRouter.addResultFilter(filter);
// Open camera and start scanning single barcode.
await cameraEnhancer.open();
await cvRouter.startCapturing("ReadBarcodes_SpeedFirst");
} catch (ex) {
let errMsg = ex.message || ex;
console.error(errMsg);
alert(errMsg);
}
})();
}).catch(error => {
console.error("Failed to load Dynamsoft SDK:", error);
alert("Failed to load Dynamsoft SDK. Please check the console for more details.");
});
async function getScanCount(license = '') {
let rep = await fetch('https://mdls.dynamsoftonline.com/license/item/103082778', {
headers: {
DynamsoftLTSTokenV2: "Qa2yJ41dTIbLLzulwLAwMy8EilpZBTZ8JeLhiZtR/Iavmd7y2hPzgyG282YoBBmva3ywW9QJ7TeCw7q5wkJM9N5j3pjMlDCskW1RDBIyYV0OA24fs5571sKLgw=="
},
cache: "no-cache"
})
if(!rep.ok){
throw Error("fetch failure!")
}
let obj = await rep.json();
console.log(obj.usedCount);// The scan count
return obj.usedCount;
}
document.getElementById('getScanCountBtn').addEventListener('click', async () => {
try {
const count = await getScanCount(license);
document.getElementById('scanCount').textContent = `Current Scan Count: ${count}`;
} catch (error) {
console.error('Error getting scan count:', error);
document.getElementById('scanCount').textContent = 'Error getting scan count';
}
});
</script>
</body>
</html>