2020 < script src ="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-barcode-reader@10.0.20-dev-20231020140243/dist/dbr.js "> </ script >
2121 < script src ="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-capture-vision-router@2.0.20-dev-20231027145739/dist/cvr.js "> </ script >
2222 < script src ="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-camera-enhancer@4.0.1-dev-20231023131759/dist/dce.js "> </ script >
23- </ head >
23+ < style >
24+ body {
25+ display : flex;
26+ flex-direction : column;
27+ align-items : center;
28+ justify-content : center;
29+ overflow : hidden;
30+ width : 100vw ;
31+ height : 80vh ;
32+ color : # 455a64 ;
33+ margin : 0 ;
34+ }
35+
36+ # div-ui-container {
37+ position : absolute;
38+ left : 0 ;
39+ top : 0 ;
40+ text-align : center;
41+ font-size : medium;
42+ height : 100% ;
43+ width : 100% ;
44+ }
45+
46+ .latest-result {
47+ display : block;
48+ margin : 2vh auto;
49+ padding : 0.4rem 0.8rem ;
50+ color : inherit;
51+ width : 85% ;
52+ border : 1px solid # fe8e14 ;
53+ font-size : 1.5rem ;
54+ border-radius : 0.2rem ;
55+ text-align : center;
56+ }
57+
58+ .latest-result ::placeholder {
59+ color : # b0bec5 ;
60+ }
2461
62+ .latest-result : focus {
63+ outline : none;
64+ box-shadow : 0.1rem 0.4rem 0.8rem # fe8e14 ;
65+ }
66+ </ style >
67+ </ head >
2568 < body >
2669 < h1 style ="font-size: 1.5em "> Read Barcodes as Input</ h1 >
2770 < div id ="inputs " style ="text-align: center " hidden >
@@ -46,6 +89,11 @@ <h3>Click each input box to fill in!</h3>
4689 />
4790 </ div >
4891 < span id ="lib-load " style ="font-size: x-large "> Loading Library...</ span >
92+ < div
93+ id ="div-ui-container "
94+ style ="width: 100%; height: 100%; background: #eee "
95+ hidden
96+ > </ div >
4997 < script >
5098 if ( location . protocol === "file:" ) {
5199 const message = `The page is opened via file:// and our SDKs may not work properly. Please open the page via https:// or host it on "http://localhost/".` ;
@@ -69,63 +117,78 @@ <h3>Click each input box to fill in!</h3>
69117 * LICENSE ALERT - THE END
70118 */
71119
72- // scanner for decoding video
73- let pScanner = null ;
74- ( async function ( ) {
75- let allInputs = document . getElementsByClassName ( "latest-result" ) ;
76- for ( let input of allInputs )
77- input . addEventListener ( "click" , async function ( ) {
78- try {
79- let scanner = await ( pScanner =
80- pScanner || Dynamsoft . DBR . BarcodeScanner . createInstance ( ) ) ;
81- let scanSettings = await scanner . getScanSettings ( ) ;
82- scanSettings . whenToPlaySoundforSuccessfulRead = "frame" ;
83- await scanner . updateScanSettings ( scanSettings ) ;
84- scanner . onUniqueRead = ( txt , result ) => {
85- this . value = result . barcodeText ;
86- scanner . hide ( ) ;
87- document . getElementById ( "UIElement" ) . hidden = true ;
88- } ;
89- settings = await scanner . getRuntimeSettings ( ) ;
90- settings . region = {
91- regionLeft : 5 ,
92- regionTop : 5 ,
93- regionRight : 95 ,
94- regionBottom : 95 ,
95- regionMeasuredByPercentage : 1 ,
96- } ;
97- await scanner . updateRuntimeSettings ( settings ) ;
98- await scanner . updateVideoSettings ( {
99- video : {
100- width : {
101- ideal : 1280 ,
102- } ,
103- height : {
104- ideal : 720 ,
105- } ,
106- facingMode : {
107- ideal : "environment" ,
108- } ,
109- } ,
110- } ) ;
111- document . getElementById ( "UIElement" ) . hidden = false ;
112- await scanner . setUIElement ( document . getElementById ( "UIElement" ) ) ;
113- await scanner . show ( ) ;
114- } catch ( ex ) {
115- let errMsg ;
116- if ( ex . message . includes ( "network connection error" ) ) {
117- errMsg =
118- "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license." ;
119- } else {
120- errMsg = ex . message || ex ;
121- }
122- console . error ( errMsg ) ;
123- alert ( errMsg ) ;
124- }
125- } ) ;
126- //Load the library on page load to speed things up.
120+ // Preload "BarcodeReader" module for reading barcodes. It will save time on the initial decoding by skipping the module loading.
121+ Dynamsoft . CVR . CaptureVisionRouter . preloadModule ( [ "DBR" ] ) . catch ( ( ex ) => {
122+ let errMsg ;
123+ if ( ex . message . includes ( "network connection error" ) ) {
124+ errMsg =
125+ "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license." ;
126+ } else {
127+ errMsg = ex . message || ex ;
128+ }
129+ console . error ( errMsg ) ;
130+ alert ( errMsg ) ;
131+ } ) ;
132+
133+ const divUIContainer = document . getElementById ( "div-ui-container" ) ;
134+
135+ const init = async ( ) => {
127136 try {
128- await Dynamsoft . DBR . BarcodeScanner . loadWasm ( ) ;
137+ // Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control.
138+ const cameraView = await Dynamsoft . DCE . CameraView . createInstance ( ) ;
139+ const cameraEnhancer =
140+ await Dynamsoft . DCE . CameraEnhancer . createInstance ( cameraView ) ;
141+ document
142+ . querySelector ( "#div-ui-container" )
143+ . append ( cameraView . getUIElement ( ) ) ; // Get default UI and append it to DOM.
144+ cameraEnhancer . setScanRegion ( {
145+ x : 5 ,
146+ y : 5 ,
147+ width : 90 ,
148+ height : 90 ,
149+ isMeasuredInPercentage : true ,
150+ } ) ; // Set a scan region
151+
152+ // Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source.
153+ const router =
154+ await Dynamsoft . CVR . CaptureVisionRouter . createInstance ( ) ;
155+ router . setInput ( cameraEnhancer ) ;
156+
157+ // Filter out unchecked and duplicate results.
158+ const filter = new Dynamsoft . Utility . MultiFrameResultCrossFilter ( ) ;
159+ filter . enableResultCrossVerification (
160+ Dynamsoft . Core . EnumCapturedResultItemType . CRIT_BARCODE ,
161+ true
162+ ) ; // Filter out unchecked barcodes.
163+ // Filter out duplicate barcodes within 3 seconds.
164+ filter . enableResultDeduplication (
165+ Dynamsoft . Core . EnumCapturedResultItemType . CRIT_BARCODE ,
166+ true
167+ ) ;
168+ filter . setDuplicateForgetTime (
169+ Dynamsoft . Core . EnumCapturedResultItemType . CRIT_BARCODE ,
170+ 3000
171+ ) ;
172+ await router . addResultFilter ( filter ) ;
173+
174+ // Define a callback for results.
175+ const resultReceiver = new Dynamsoft . CVR . CapturedResultReceiver ( ) ;
176+ resultReceiver . onDecodedBarcodesReceived = async ( result ) => {
177+ const resultItems = result . barcodesResultItems ;
178+ if ( ! resultItems . length ) return ;
179+
180+ Dynamsoft . DCE . Feedback . beep ( ) ; // Trigger a beep.
181+ } ;
182+ router . addResultReceiver ( resultReceiver ) ;
183+
184+ document . getElementById ( "inputs" ) . hidden = false ;
185+ document . getElementById ( "lib-load" ) . hidden = true ;
186+
187+ return {
188+ cameraView,
189+ cameraEnhancer,
190+ router,
191+ } ;
129192 } catch ( ex ) {
130193 let errMsg ;
131194 if ( ex . message . includes ( "network connection error" ) ) {
@@ -137,129 +200,36 @@ <h3>Click each input box to fill in!</h3>
137200 console . error ( errMsg ) ;
138201 alert ( errMsg ) ;
139202 }
140- document . getElementById ( "inputs" ) . hidden = false ;
141- document . getElementById ( "lib-load" ) . hidden = true ;
142- } ) ( ) ;
143- </ script >
144- < div
145- id ="UIElement "
146- style ="
147- width: 100%;
148- height: 100%;
149- min-width: 100px;
150- min-height: 100px;
151- background: #eee;
152- "
153- hidden
154- >
155- < svg
156- class ="dce-bg-loading "
157- style ="
158- display: none;
159- animation: 1s linear infinite dce-rotate;
160- width: 40%;
161- height: 40%;
162- position: absolute;
163- margin: auto;
164- left: 0;
165- top: 0;
166- right: 0;
167- bottom: 0;
168- fill: #aaa;
169- "
170- viewBox ="0 0 1792 1792 "
171- >
172- < path
173- d ="M1760 896q0 176-68.5 336t-184 275.5-275.5 184-336 68.5-336-68.5-275.5-184-184-275.5-68.5-336q0-213 97-398.5t265-305.5 374-151v228q-221 45-366.5 221t-145.5 406q0 130 51 248.5t136.5 204 204 136.5 248.5 51 248.5-51 204-136.5 136.5-204 51-248.5q0-230-145.5-406t-366.5-221v-228q206 31 374 151t265 305.5 97 398.5z "
174- />
175- </ svg >
176- < svg
177- class ="dce-bg-camera "
178- style ="
179- display: none;
180- width: 40%;
181- height: 40%;
182- position: absolute;
183- margin: auto;
184- left: 0;
185- top: 0;
186- right: 0;
187- bottom: 0;
188- fill: #aaa;
189- "
190- viewBox ="0 0 2048 1792 "
191- >
192- < path
193- d ="M1024 672q119 0 203.5 84.5t84.5 203.5-84.5 203.5-203.5 84.5-203.5-84.5-84.5-203.5 84.5-203.5 203.5-84.5zm704-416q106 0 181 75t75 181v896q0 106-75 181t-181 75h-1408q-106 0-181-75t-75-181v-896q0-106 75-181t181-75h224l51-136q19-49 69.5-84.5t103.5-35.5h512q53 0 103.5 35.5t69.5 84.5l51 136h224zm-704 1152q185 0 316.5-131.5t131.5-316.5-131.5-316.5-316.5-131.5-316.5 131.5-131.5 316.5 131.5 316.5 316.5 131.5z "
194- />
195- </ svg >
196- < div
197- class ="dce-video-container "
198- style ="position: absolute; left: 0; top: 0; width: 100%; height: 100% "
199- > </ div >
200- < div
201- class ="dce-scanarea "
202- style ="position: absolute; left: 0; top: 0; width: 100%; height: 100% "
203- >
204- < div
205- class ="dce-scanlight "
206- style ="
207- display: none;
208- position: absolute;
209- width: 100%;
210- height: 3%;
211- border-radius: 50%;
212- box-shadow: 0px 0px 2vw 1px #00e5ff;
213- background: #fff;
214- animation: 3s infinite dce-scanlight;
215- user-select: none;
216- "
217- > </ div >
218- </ div >
219- </ div >
220- </ body >
221- < style >
222- body {
223- display : flex;
224- flex-direction : column;
225- align-items : center;
226- justify-content : center;
227- overflow : hidden;
228- width : 100vw ;
229- height : 80vh ;
230- color : # 455a64 ;
231- margin : 0 ;
232- }
203+ } ;
204+ const pInit = init ( ) ;
205+
206+ const allInputs = document . getElementsByClassName ( "latest-result" ) ;
207+ for ( let input of allInputs ) {
208+ input . addEventListener ( "click" , async function ( ) {
209+ const { cameraView, cameraEnhancer, router } = await pInit ;
233210
234- # UIElement {
235- position : absolute;
236- left : 0 ;
237- top : 0 ;
238- text-align : center;
239- font-size : medium;
240- height : 100% ;
241- width : 100% ;
242- }
211+ // Define a callback for results.
212+ const resultReceiver = new Dynamsoft . CVR . CapturedResultReceiver ( ) ;
213+ resultReceiver . onDecodedBarcodesReceived = async ( result ) => {
214+ const resultItems = result . barcodesResultItems ;
215+ if ( ! resultItems . length ) return ;
243216
244- .latest-result {
245- display : block;
246- margin : 2vh auto;
247- padding : 0.4rem 0.8rem ;
248- color : inherit;
249- width : 85% ;
250- border : 1px solid # fe8e14 ;
251- font-size : 1.5rem ;
252- border-radius : 0.2rem ;
253- text-align : center;
254- }
217+ this . value = resultItems [ 0 ] . text ;
218+ router . stopCapturing ( ) ;
219+ cameraEnhancer . close ( ) ;
220+ divUIContainer . hidden = true ;
255221
256- . latest-result :: placeholder {
257- color : # b0bec5 ;
258- }
222+ router . removeResultReceiver ( resultReceiver ) ;
223+ } ;
224+ router . addResultReceiver ( resultReceiver ) ;
259225
260- .latest-result : focus {
261- outline : none;
262- box-shadow : 0.1rem 0.4rem 0.8rem # fe8e14 ;
263- }
264- </ style >
226+ divUIContainer . hidden = false ;
227+
228+ // Open camera and start scanning single barcode.
229+ await cameraEnhancer . open ( ) ;
230+ await router . startCapturing ( "ReadSingleBarcode" ) ;
231+ } ) ;
232+ }
233+ </ script >
234+ </ body >
265235</ html >
0 commit comments