Skip to content

Instantly share code, notes, and snippets.

@mdshadman
Created August 2, 2019 07:17
Show Gist options
  • Save mdshadman/cbeb0287cd3e0f60050ccd8018318183 to your computer and use it in GitHub Desktop.
Save mdshadman/cbeb0287cd3e0f60050ccd8018318183 to your computer and use it in GitHub Desktop.
import { Component } from '@angular/core';
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner/ngx';
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
encodedData = '';
QRSCANNED_DATA: string;
isOn = false;
scannedData: {};
constructor(public qrScanCtrl: QRScanner) { }
goToQrScan() {
this.qrScanCtrl.prepare()
.then((status: QRScannerStatus) => {
if (status.authorized) {
// camera permission was granted
this.isOn = true;
// start scanning
const scanSub = this.qrScanCtrl.scan().subscribe((text: string) => {
console.log('Scanned something', text);
this.isOn = false;
this.QRSCANNED_DATA = text;
if (this.QRSCANNED_DATA !== '') {
this.closeScanner();
scanSub.unsubscribe();
}
});
this.qrScanCtrl.show();
} else if (status.denied) {
console.log('camera permission denied');
this.qrScanCtrl.openSettings();
} else {
}
})
.catch((e: any) => console.log('Error is', e));
}
closeScanner() {
this.isOn = false;
this.qrScanCtrl.hide();
this.qrScanCtrl.destroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment