11import { Component , ViewChild , ElementRef } from '@angular/core' ;
2- import " ../dynamsoft.config" ;
3- import { EnumCapturedResultItemType } from " dynamsoft-core" ;
4- import type { BarcodeResultItem } from " dynamsoft-barcode-reader" ;
5- import { CaptureVisionRouter } from " dynamsoft-capture-vision-router" ;
2+ import ' ../dynamsoft.config' ;
3+ import { EnumCapturedResultItemType } from ' dynamsoft-core' ;
4+ import type { BarcodeResultItem } from ' dynamsoft-barcode-reader' ;
5+ import { CaptureVisionRouter } from ' dynamsoft-capture-vision-router' ;
66
77@Component ( {
88 selector : 'app-image-capture' ,
@@ -11,49 +11,57 @@ import { CaptureVisionRouter } from "dynamsoft-capture-vision-router";
1111 standalone : true ,
1212} )
1313export class ImageCaptureComponent {
14-
15- @ViewChild ( 'resDiv' ) resDiv ?: ElementRef < HTMLDivElement > ;
14+ @ViewChild ( 'results' ) resultsContainer ?: ElementRef < HTMLDivElement > ;
1615
1716 pCvRouter ?: Promise < CaptureVisionRouter > ;
18- bDestoried = false ;
17+ isDestroyed = false ;
1918
2019 captureImage = async ( e : Event ) => {
21- let files = [ ...( e . target ! as HTMLInputElement ) . files as any as File [ ] ] ;
22- ( e . target ! as HTMLInputElement ) . value = '' ;
23- this . resDiv ! . nativeElement . innerText = "" ;
20+ let files = [ ...( ( e . target ! as HTMLInputElement ) . files as any as File [ ] ) ] ;
21+ ( e . target ! as HTMLInputElement ) . value = '' ; // reset input
22+ this . resultsContainer ! . nativeElement . innerText = '' ;
2423 try {
25- const cvRouter = await ( this . pCvRouter = this . pCvRouter || CaptureVisionRouter . createInstance ( ) ) ;
26- if ( this . bDestoried ) return ;
27-
28- for ( let file of files ) {
24+ // ensure cvRouter is created only once
25+ const cvRouter = await ( this . pCvRouter =
26+ this . pCvRouter || CaptureVisionRouter . createInstance ( ) ) ;
27+ if ( this . isDestroyed ) return ;
28+
29+ for ( let file of files ) {
2930 // Decode selected image with 'ReadBarcodes_SpeedFirst' template.
30- const result = await cvRouter . capture ( file , "ReadBarcodes_SpeedFirst" ) ;
31- if ( this . bDestoried ) return ;
32-
33- if ( files . length > 1 ) {
34- this . resDiv ! . nativeElement . innerText += `\n${ file . name } :\n` ;
31+ const result = await cvRouter . capture ( file , 'ReadBarcodes_SpeedFirst' ) ;
32+ if ( this . isDestroyed ) return ;
33+
34+ // Print file name if there's multiple files
35+ if ( files . length > 1 ) {
36+ this . resultsContainer ! . nativeElement . innerText += `\n${ file . name } :\n` ;
3537 }
3638 for ( let _item of result . items ) {
37- if ( _item . type !== EnumCapturedResultItemType . CRIT_BARCODE ) { continue ; }
39+ if ( _item . type !== EnumCapturedResultItemType . CRIT_BARCODE ) {
40+ continue ; // check if captured result item is a barcode
41+ }
3842 let item = _item as BarcodeResultItem ;
39- this . resDiv ! . nativeElement . innerText += item . text + "\n" ;
43+ this . resultsContainer ! . nativeElement . innerText += item . text + '\n' ; // output the decoded barcode text
4044 console . log ( item . text ) ;
4145 }
42- if ( ! result . items . length ) this . resDiv ! . nativeElement . innerText += 'No barcode found\n' ;
46+ // If no items are found, display that no barcode was detected
47+ if ( ! result . items . length )
48+ this . resultsContainer ! . nativeElement . innerText +=
49+ 'No barcode found\n' ;
4350 }
4451 } catch ( ex : any ) {
4552 let errMsg = ex . message || ex ;
4653 console . error ( errMsg ) ;
4754 alert ( errMsg ) ;
4855 }
49- }
56+ } ;
5057
58+ // dispose cvRouter when it's no longer needed
5159 async ngOnDestroy ( ) {
52- this . bDestoried = true ;
53- if ( this . pCvRouter ) {
54- try {
60+ this . isDestroyed = true ;
61+ if ( this . pCvRouter ) {
62+ try {
5563 ( await this . pCvRouter ) . dispose ( ) ;
56- } catch ( _ ) { }
64+ } catch ( _ ) { }
5765 }
5866 }
5967}
0 commit comments