11<script lang =" ts" >
2- import { onMount } from " svelte" ;
3- import " ../dynamsoft.config" ;
4- import { EnumCapturedResultItemType } from " dynamsoft-core" ;
5- import { type BarcodeResultItem } from " dynamsoft-barcode-reader" ;
6- import { CaptureVisionRouter } from " dynamsoft-capture-vision-router" ;
2+ import { onMount } from " svelte" ;
3+ import " ../dynamsoft.config" ;
4+ import { EnumCapturedResultItemType } from " dynamsoft-core" ;
5+ import { type BarcodeResultItem } from " dynamsoft-barcode-reader" ;
6+ import { CaptureVisionRouter } from " dynamsoft-capture-vision-router" ;
77
8- let resDiv : HTMLDivElement ;
8+ let resultsContainer : HTMLDivElement ;
99
10- let pCvRouter: Promise <CaptureVisionRouter >;
11- let bDestoried = false ;
10+ let pCvRouter: Promise <CaptureVisionRouter >;
11+ let isDestroyed = false ;
1212
13+ const captureImage = async (e : Event ) => {
14+ let files = [... (e .target ! as HTMLInputElement ).files ! ];
15+ (e .target ! as HTMLInputElement ).value = " " ; // reset input
16+ resultsContainer .innerText = " " ;
1317
14- const captureImage = async (e : Event ) => {
15- let files = [... (e .target ! as HTMLInputElement ).files ! ];
16- (e .target ! as HTMLInputElement ).value = ' ' ;
17- resDiv .innerText = " " ;
18- try {
19- const cvRouter = await (pCvRouter = pCvRouter || CaptureVisionRouter .createInstance ());
20- if (bDestoried ) return ;
21-
22- for (let file of files ){
23- // Decode selected image with 'ReadBarcodes_SpeedFirst' template.
24- const result = await cvRouter .capture (file , " ReadBarcodes_SpeedFirst" );
25- if (bDestoried ) return ;
18+ try {
19+ const cvRouter = await (pCvRouter = pCvRouter || CaptureVisionRouter .createInstance ());
20+ if (isDestroyed ) return ;
2621
27- if (files .length > 1 ){
28- resDiv .innerText += ` \n ${file .name }:\n ` ;
29- }
30- for (let _item of result .items ) {
31- if (_item .type !== EnumCapturedResultItemType .CRIT_BARCODE ) { continue ; }
32- let item = _item as BarcodeResultItem ;
33- resDiv .innerText += item .text + " \n " ;
34- console .log (item .text );
35- }
36- if (! result .items .length ) resDiv .innerText += ' No barcode found\n ' ;
37- }
38- } catch (ex : any ) {
39- let errMsg = ex .message || ex ;
40- console .error (errMsg );
41- alert (errMsg );
42- }
43- };
22+ for (let file of files ) {
23+ // Decode selected image with 'ReadBarcodes_SpeedFirst' template.
24+ const result = await cvRouter .capture (file , " ReadBarcodes_SpeedFirst" );
25+ if (isDestroyed ) return ;
4426
45- onMount (() => {
46- // onBeforeUnmount
47- return async ()=> {
48- bDestoried = true ;
49- if (pCvRouter ){
50- try {
51- (await pCvRouter ).dispose ();
52- }catch (_ ){}
27+ // Print file name if there's multiple files
28+ if (files .length > 1 ) {
29+ resultsContainer .innerText += ` \n ${file .name }:\n ` ;
30+ }
31+ for (let _item of result .items ) {
32+ if (_item .type !== EnumCapturedResultItemType .CRIT_BARCODE ) {
33+ continue ; // check if captured result item is a barcode
34+ }
35+ let item = _item as BarcodeResultItem ;
36+ resultsContainer .innerText += item .text + " \n " ; // output the decoded barcode text
37+ console .log (item .text );
38+ }
39+ // If no items are found, display that no barcode was detected
40+ if (! result .items .length ) resultsContainer .innerText += " No barcode found\n " ;
41+ }
42+ } catch (ex : any ) {
43+ let errMsg = ex .message || ex ;
44+ console .error (errMsg );
45+ alert (errMsg );
5346 }
5447 };
55- });
48+
49+ onMount (() => {
50+ // onBeforeUnmount. dispose cvRouter when it's no longer needed
51+ return async () => {
52+ isDestroyed = true ;
53+ if (pCvRouter ) {
54+ try {
55+ (await pCvRouter ).dispose ();
56+ } catch (_ ) {}
57+ }
58+ };
59+ });
5660 </script >
5761
58- <div class =" capture-img " >
59- <div class =" img-ipt " >
60- <input type ="file" multiple on:change ={captureImage } accept =" .jpg,.jpeg,.icon,.gif,.svg,.webp,.png,.bmp" />
62+ <div class =" image- capture-container " >
63+ <div class =" input-container " >
64+ <input type ="file" multiple on:change ={captureImage } accept =" .jpg,.jpeg,.icon,.gif,.svg,.webp,.png,.bmp" />
6165 </div >
62- <div class ="result-area " bind:this ={resDiv }></div >
66+ <div class ="result" bind:this ={resultsContainer }></div >
6367</div >
6468
6569<style >
66- .capture-img {
70+ .image- capture-container {
6771 width : 100% ;
6872 height : 100% ;
6973 font-family :
@@ -77,7 +81,7 @@ onMount(() => {
7781 monospace ;
7882 }
7983
80- .capture-img .img-ipt {
84+ .image- capture-container .input-container {
8185 width : 80% ;
8286 height : 100% ;
8387 display : flex ;
@@ -86,7 +90,7 @@ onMount(() => {
8690 margin : 0 auto ;
8791 }
8892
89- .capture-img .result-area {
93+ .image- capture-container .result {
9094 margin-top : 20px ;
9195 }
9296 </style >
0 commit comments