77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import {NavController} from "@ionic/angular";
|
|
import {Router} from "@angular/router";
|
|
import { BarcodeScanner } from '@capacitor-community/barcode-scanner';
|
|
@Component({
|
|
selector: 'app-startscan',
|
|
templateUrl: './startscan.page.html',
|
|
styleUrls: ['./startscan.page.scss'],
|
|
})
|
|
export class StartscanPage implements OnInit {
|
|
|
|
content_visibility = '';
|
|
tabs = 'about';
|
|
|
|
constructor(
|
|
private navctr: NavController,
|
|
private router: Router
|
|
) { }
|
|
|
|
ngOnInit() {
|
|
this.startScan();
|
|
}
|
|
|
|
onBack() {
|
|
this.stopScan();
|
|
this.navctr.back();
|
|
}
|
|
|
|
|
|
async checkPermission() {
|
|
try {
|
|
// check or request permission
|
|
const status = await BarcodeScanner.checkPermission({ force: true });
|
|
if (status.granted) {
|
|
// the user granted permission
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch(e) {
|
|
console.log(e);
|
|
}
|
|
}
|
|
scannedResult:any;
|
|
async startScan() {
|
|
try {
|
|
const permission = await this.checkPermission();
|
|
if(!permission) {
|
|
this. stopScan();
|
|
return;
|
|
}
|
|
await BarcodeScanner.hideBackground();
|
|
document.querySelector('body').classList.add('scanner-active');
|
|
this.content_visibility = 'hidden';
|
|
const result = await BarcodeScanner.startScan();
|
|
console.log(result);
|
|
BarcodeScanner.showBackground();
|
|
document.querySelector('body').classList.remove('scanner-active');
|
|
this.content_visibility = '';
|
|
if(result?.hasContent) {
|
|
this.scannedResult = result.content;
|
|
console.log(this.scannedResult);
|
|
}
|
|
} catch(e) {
|
|
console.log(e);
|
|
this.stopScan();
|
|
}
|
|
}
|
|
|
|
stopScan() {
|
|
BarcodeScanner.showBackground();
|
|
BarcodeScanner.stopScan();
|
|
document.querySelector('body').classList.remove('scanner-active');
|
|
this.content_visibility = '';
|
|
}
|
|
|
|
}
|