Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { NavController } from 'ionic-angular';
  3. import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner/ngx';
  4. import { NativeAudio } from '@ionic-native/native-audio/ngx';
  5.  
  6.  
  7. @Component({
  8. selector: 'page-home',
  9. templateUrl: 'home.html'
  10. })
  11. export class HomePage {
  12.  
  13. scanning: boolean;
  14. scanSub: any;
  15.  
  16. constructor(public navCtrl: NavController, private qrScanner: QRScanner, private nativeAudio: NativeAudio) {
  17. this.nativeAudio.preloadSimple('beep', 'assets/audio/beep.mp3');
  18. this.scanQRCode();
  19. }
  20.  
  21. scanQRCode() {
  22. this.qrScanner.prepare().then((status: QRScannerStatus) => {
  23. if (status.authorized) {
  24. this.qrScanner.show();
  25. this.scanning = true;
  26. window.document.querySelector('.app-root').classList.add('transparentBody');
  27. this.scanSub = this.qrScanner.scan().subscribe((text: string) => { this.resultado(text) });
  28. }
  29. }).catch((e: any) => {
  30. // console.log('Error is', e);
  31. });
  32. }
  33.  
  34. resultado (text) {
  35. this.nativeAudio.play('beep');
  36. this.scanSub.unsubscribe();
  37. console.log(text);
  38. let timer = 1;
  39. let intervalId = setInterval(() => {
  40. if(timer == 0) {
  41. clearInterval(intervalId);
  42. this.scanSub = this.qrScanner.scan().subscribe((text: string) => { this.resultado(text) });
  43. }
  44. timer = timer - 1;
  45. }, 1000);
  46. }
  47.  
  48. closeScanner() {
  49. this.scanning = false;
  50. this.scanSub.unsubscribe();
  51. let app_root = window.document.querySelector('.app-root');
  52. if (app_root) {
  53. app_root.classList.remove('transparentBody');
  54. }
  55. this.qrScanner.hide();
  56. this.qrScanner.destroy();
  57. }
  58.  
  59. }
  60.  
  61. <ion-content [ngClass]="{'transparentBody': scanning}">
  62. <ng-container *ngIf="scanning === true">
  63. <div class="qrcode-container" [style.height]="'100vh'">
  64. </div>
  65. </ng-container>
  66. </ion-content>
  67.  
  68. .transparentBody {
  69. background: transparent !important;
  70. background-color: transparent !important;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement