-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathfill-a-form-with-barcode-reading.html
More file actions
168 lines (144 loc) · 6.7 KB
/
fill-a-form-with-barcode-reading.html
File metadata and controls
168 lines (144 loc) · 6.7 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
<!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="Read barcodes and fill form fields using barcode with Dynamsoft Barcode Reader. Get quick and accurate results in no time." />
<meta name="keywords" content="fill form fields using barcodes" />
<link rel="canonical" href="https://demo.dynamsoft.com/samples/DBR/JS/use-case/fill-a-form-with-barcode-reading.html" />
<title>Dynamsoft Barcode Reader Sample - Fill Forms with Dynamsoft Barcode Reader</title>
<style>
body {
width: 100vw;
height: 80vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: hidden;
color: #455a64;
margin: 0;
}
#camera-view-container {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
text-align: center;
font-size: medium;
}
.latest-result {
width: 85%;
display: block;
margin: 2vh auto;
padding: 0.4rem 0.8rem;
color: inherit;
border: 1px solid #fe8e14;
font-size: 1.5rem;
border-radius: 0.2rem;
text-align: center;
}
.latest-result::placeholder {
color: #b0bec5;
}
.latest-result:focus {
outline: none;
box-shadow: 0.1rem 0.4rem 0.8rem #fe8e14;
}
</style>
</head>
<body>
<h1 style="font-size: 1.5em">Fill Forms with Dynamsoft Barcode Reader</h1>
<div id="inputs-container" style="text-align: center">
<h3>Click each input box to fill in!</h3>
<input type="text" class="latest-result" readonly="true" placeholder="Barcode Result 1" />
<input type="text" class="latest-result" readonly="true" placeholder="Barcode Result 2" />
<input type="text" class="latest-result" readonly="true" placeholder="Barcode Result 3" />
</div>
<span id="lib-load" style="font-size: x-large; display: none">Loading Library...</span>
<div id="camera-view-container" style="width: 100%; height: 100%; background: #eee; display: none"></div>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@10.5.3000/dist/dbr.bundle.min.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="../../../distributables/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=10.5.3000&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(["DBR"]);
// // If the network is unstable or you prefer to self-host the SDK, uncomment the line below to define the root path of the engine files
// Dynamsoft.Core.CoreModule.engineResourcePaths.rootDirectory = "../../../distributables/";
const inputsContainer = document.getElementById("inputs-container");
const loadingIndicator = document.getElementById("lib-load");
const cameraViewContainer = document.getElementById("camera-view-container");
const init = async () => {
inputsContainer.style.display = "none";
loadingIndicator.style.display = "";
// 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);
document.querySelector("#camera-view-container").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();
cvRouter.setInput(cameraEnhancer);
// 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);
await cvRouter.addResultFilter(filter);
// Define a callback for results.
cvRouter.addResultReceiver({
onDecodedBarcodesReceived: async (result) => {
const resultItems = result.barcodeResultItems;
if (!resultItems.length) return;
Dynamsoft.DCE.Feedback.beep(); // Trigger a beep.
},
});
inputsContainer.style.display = "";
loadingIndicator.style.display = "none";
return { cameraEnhancer, cvRouter, cameraView };
};
let pInit; // promise of init
const allInputs = document.getElementsByClassName("latest-result");
for (let input of allInputs) {
input.addEventListener("click", async function () {
try {
const { cameraEnhancer, cvRouter, cameraView } = await (pInit = pInit || init());
// Define a callback for results.
const resultReceiver = {
onDecodedBarcodesReceived: async (result) => {
const resultItems = result.barcodeResultItems;
if (!resultItems.length) return;
this.value = resultItems[0].text;
cvRouter.stopCapturing();
cameraEnhancer.close();
cameraViewContainer.style.display = "none";
cvRouter.removeResultReceiver(resultReceiver);
},
};
cvRouter.addResultReceiver(resultReceiver);
cameraViewContainer.style.display = "";
// Open camera and start scanning single barcode.
await cameraEnhancer.open();
cameraView.setScanLaserVisible(true);
await cvRouter.startCapturing("ReadSingleBarcode");
} catch (ex) {
let errMsg = ex.message || ex;
console.error(errMsg);
alert(errMsg);
}
});
}
</script>
</body>
</html>