import { Observable , alert } from "@nativescript/core" ; import { BarcodeScanner } from "nativescript-barcodescanner" ; export class HelloWorldModel extends Observable { public message : string ; private barcodeScanner : BarcodeScanner ; constructor ( ) { super ( ) ; this . barcodeScanner = new BarcodeScanner ( ) ; } public onScanResult ( scanResult ) { console . log ( `onScanResult: ${ scanResult . text } (${ scanResult . format })` ) ; } public doCheckAvailable ( ) { this . barcodeScanner . available ( ) . then ( avail => { alert ( { title : "Scanning available?" , message : avail ? "YES" : "NO" , okButtonText : "OK" } ) ; } , ( err ) => { alert ( err ) ; } ) ; } public doCheckHasCameraPermission ( ) { this . barcodeScanner . hasCameraPermission ( ) . then ( permitted => { alert ( { title : "Has Camera permission?" , message : permitted ? "YES" : "NO" , okButtonText : "OK" } ) ; } , ( err ) => { alert ( err ) ; } ) ; } public doRequestCameraPermission ( ) { this . barcodeScanner . requestCameraPermission ( ) . then ( ( ) => console . log ( "Camera permission granted" ) ) . catch ( ( ) => console . log ( "Camera permission not granted" ) ) ; } public doScanWithBackCamera ( ) { this . scan ( false , true ) ; } public doScanWithFrontCamera ( ) { this . scan ( true , false ) ; } public doScanWithTorch ( ) { this . scan ( false , true , true , "portrait" ) ; } public doScanPortrait ( ) { this . scan ( false , true , false , "portrait" ) ; } public doScanLandscape ( ) { this . scan ( false , true , false , "landscape" ) ; } public doContinuousScan ( ) { this . barcodeScanner . scan ( { reportDuplicates : true , continuousScanCallback : function ( result ) { console . log ( `${ result . format }: ${ result . text } @ ${ new Date ( ) . getTime ( ) }` ) ; } , closeCallback : ( ) => { console . log ( "Scanner closed @ " + new Date ( ) . getTime ( ) ) ; } } ) ; } public doContinuousScanMax3 ( ) { let count = 0 ; let self = this ; this . barcodeScanner . scan ( { reportDuplicates : false , closeCallback : ( ) => { console . log ( "Scanner closed @ " + new Date ( ) . getTime ( ) ) ; } , continuousScanCallback : function ( result ) { count ++ ; console . log ( result . format + ": " + result . text + " (count: " + count + ")" ) ; if ( count === 3 ) { self . barcodeScanner . stop ( ) ; setTimeout ( function ( ) { alert ( { title : "Scanned 3 codes" , message : "Check the log for the results" , okButtonText : "Sweet!" } ) ; } , 1000 ) ; } } } ) ; } private scan ( front , flip , torch ? , orientation ? ) { this . barcodeScanner . scan ( { presentInRootViewController : true , cancelLabel : "EXIT. Also, try the volume buttons!" , cancelLabelBackgroundColor : "#333333" , message : "Use the volume buttons for extra light" , preferFrontCamera : front , showFlipCameraButton : flip , showTorchButton : torch , torchOn : false , resultDisplayDuration : 500 , orientation : orientation , beepOnScan : true , fullScreen : true , openSettingsIfPermissionWasPreviouslyDenied : true , closeCallback : ( ) => { console . log ( "Scanner closed @ " + new Date ( ) . getTime ( ) ) ; } } ) . then ( function ( result ) { console . log ( "--- scanned: " + result . text ) ; setTimeout ( function ( ) { alert ( { title : "Scan result" , message : "Format: " + result . format + ",\nValue: " + result . text , okButtonText : "OK" } ) ; } , 500 ) ; } , function ( errorMessage ) { console . log ( "No scan. " + errorMessage ) ; } ) ; } }