import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { AndroidPermissions } from '@ionic-native/android-permissions';
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner';
@Component({
selector: 'page-scanner',
templateUrl: 'scanner.html'
})
export class ScannerPage {
constructor(public navCtrl: NavController,
public platform:Platform,
public androidPermissions: AndroidPermissions,
public qrScanner: QRScanner) {
// solve the problem - "plugin not installed".
platform.ready().then(()=>{
this.qrscanner();
})
}
qrscanner() {
// Optionally request the permission early
this.qrScanner.prepare()
.then((status: QRScannerStatus) => {
if (status.authorized) {
// camera permission was granted
alert('authorized');
// start scanning
let scanSub = this.qrScanner.scan().subscribe((text: string) => {
console.log('Scanned something', text);
alert(text);
this.qrScanner.hide(); // hide camera preview
scanSub.unsubscribe(); // stop scanning
this.navCtrl.pop();
});
this.qrScanner.resumePreview();
// show camera preview
this.qrScanner.show()
.then((data : QRScannerStatus)=> {
alert(data.showing);
},err => {
alert(err);
});
// wait for user to scan something, then the observable callback will be called
} else if (status.denied) {
alert('denied');
// camera permission was permanently denied
// you must use QRScanner.openSettings() method to guide the user to the settings page
// then they can grant the permission from there
} else {
// permission was denied, but not permanently. You can ask for permission again at a later time.
alert('else');
}
})
.catch((e: any) => {
alert('Error is' + e);
});
}
}
I'm submitting a ... (check one with "x") [x] bug report [ ] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or https://ionicworldwide.herokuapp.com/
Current behavior: The white background is not removed. The camera should be switched on. Unable to init QR Scanner properly.
Expected behavior: The white background is removed. The camera should be switched on. Able to init QR Scanner properly.
Steps to reproduce:
Click launch to launch QR Scanner Page that manifest the component.
Related code: scanner.html
scanner.ts
scanner.scss
app.module.ts
Other information:
Ionic info: (run
ionic info
from a terminal/cmd prompt and paste output below):