Ionic cordova-plugin-qrscanner没有相机预览

时间:2017-07-26 03:20:05

标签: cordova-plugins qr-code ionic3 ionic-native

我运行一个简单的演示来使用cordova-plugin-qrscanner,它可以扫描qrcode但没有摄像头预览。

qrscannerDemo on Github

相关密码打击:



import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';


import { AndroidPermissions } from '@ionic-native/android-permissions';
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController,
              public androidPermissions: AndroidPermissions,
              public qrScanner: 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.qrScanner.resumePreview();

          // show camera preview
          this.qrScanner.show();

          // 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);
      });

  }

}

<ion-header>
  <ion-navbar transparent>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header> 

 <ion-content padding style="background: none transparent;">
  <button ion-button (click)="qrscanner()">qrscanner</button>
</ion-content>  
&#13;
&#13;
&#13;

<击> 我在android上运行离子项目,然后单击按钮但没有任何反应,没有相机预览显示。

我再次测试项目并发现它可以扫描qrcode并获得结果测试,但没有相机预览。

我搜索问题,有人说应该设置身体和任何元素透明。我尝试但不起作用。

Android. Nothing appears on screen. #35

AnyOne帮助?

5 个答案:

答案 0 :(得分:12)

在顶级index.html:

<ion-app style="background: none transparent;"></ion-app>

在相机显示页面html文件中:

<ion-content style="background: none transparent;">

答案 1 :(得分:4)

经过一番研究,我什至找到了答案,而且肯定对所有人都有效,但是@nokeieng的答案也对我有帮助。

1)首先,为qrscanner创建一个新组件。在ionic中存在离子的生命周期,因此请按照进入组件后的事件进行操作,以触发ionViewDidEnter()。在这种情况下,相机会打开并允许您扫描。

 ionViewDidEnter(){
     this.qrScanner.prepare()
    .then((status: QRScannerStatus) => {
      if (status.authorized) {
        // camera permission was granted

        var camtoast = this.toastCtrl.create({
          message: 'camera permission granted',
          duration: 1000
        });
        camtoast.present();
        // start scanning

        this.qrScanner.show()
        window.document.querySelector('ion-app').classList.add('cameraView');

        let scanSub = this.qrScanner.scan().subscribe((text: string) => {

          console.log('Scanned something', text);

          window.document.querySelector('ion-app').classList.remove('cameraView');
          this.qrScanner.hide(); // hide camera preview

          const toast = this.toastCtrl.create({
            message: 'You scanned text is this :'+text,
            duration: 6000
          });
          toast.present();
          scanSub.unsubscribe(); // stop scanning
        });


      } else if (status.denied) {
        const toast = this.toastCtrl.create({
          message: 'camera permission was denied',
          duration: 3000
        });
        toast.present();
        // 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 {
        const toast = this.toastCtrl.create({
          message: 'You can ask for permission again at a later time.',
          duration: 3000
        });
        toast.present();
        // permission was denied, but not permanently. You can ask for permission again at a later time.
      }
    })
    .catch((e: any) => console.log('Error is', e));

}

2)此后,在按下返回按钮的情况下删除camera类,以添加此代码。  ionViewWillLeave()将在组件被销毁或遗留时触发。

ionViewWillLeave(){

  window.document.querySelector('ion-app').classList.remove('cameraView');

}

3)我们完成了.ts文件。现在,我们必须使组件和主要元素(即ion-app透明),以便我们可以看到在theme/variables.scss

中添加此CSS的摄像机。
ion-app.cameraView ion-nav{opacity:0}

ion-app.cameraView,ion-app.cameraView ion-content,ion-app.cameraView .nav-decor,{
background: transparent url("../../assets/imgs/camera_overlay.png") !important;
background-size: 100% 100% !important;}

4)如您所见,我给了背景图片,以便我们获得相机覆盖预览

代码完成后,只需在终端中运行此命令即可查看离子中的实时变化

ionic cordova run android --livereload

答案 2 :(得分:1)

有一个div,其中class =“nav-decor”,背景为黑色,需要将其更改为透明。

我将3件事改为透明,以便相机显示:ion-app,ion-content和.nav-decor

我的解决方案是拥有一个“cameraView”类,它将离子应用程序,离子内容和.nav-decor设置为具有透明背景。

我使用了这个CSS

ion-app.cameraView, ion-app.cameraView ion-content, ion-app.cameraView .nav-decor {
  background: transparent none !important; 
}

然后这些函数在qrScanner.show()之后显示相机并在我完成扫描后隐藏它

showCamera() {    
  (window.document.querySelector('ion-app') as HTMLElement).classList.add('cameraView');
}
hideCamera() {    
  (window.document.querySelector('ion-app') as HTMLElement).classList.remove('cameraView');
}

答案 3 :(得分:1)

我已经解决了许多问题,

这是我的解决方案,结合了我读过的所有答案。

在我的名为page-scan.scss

的scss文件中
page-scan {}

ion-app.cameraView,
ion-app.cameraView ion-content,
ion-app.cameraView .nav-decor,
ion-header,
ion-navbar,
ion-title {
    background: transparent none !important;
}

ion-app.cameraView {
    background-size: 100% 100% !important;
    /* To show image border */
    background-image: url([YOU CAN USE BASE64 image here!!]) !important;
}

注意:图片边框如this one 以下是示例图片:Border for QR code area 档案scan.html

<ion-header>

  <ion-navbar color="primary_dark">
    <ion-title>scan</ion-title>
  </ion-navbar>

</ion-header>

<ion-content>
</ion-content>

档案scan.ts。添加这些功能以显示和隐藏相机预览

 private showCamera() {
        ((<any>window).document.querySelector('ion-app') as HTMLElement).classList.add('cameraView');
    }
 private hideCamera() {
        ((<any>window).document.querySelector('ion-app') as HTMLElement).classList.remove('cameraView');
    }

最后,调用显示,扫描和预览相机,如下面的代码

this.showCamera();
this.qrScanner.show()
this.subScan = this.qrScanner.scan()

请参阅github上的问题here

答案 4 :(得分:1)

如果状态被授权,您只需要在“无”和“阻止”之间切换离子应用程序显示。

const ionApp = <HTMLElement>document.getElementsByTagName("ion-app")[0];
                // start scanning
                const scanSub = this.qrScanner.scan().subscribe((link: string) => {
                    ionApp.style.display = "block";

                    this.qrScanner.hide(); // hide camera preview
                    scanSub.unsubscribe(); // stop scanning
                });
                ionApp.style.display = "none";
                this.qrScanner.show();