界面添加一个按钮:

<button ion-button block color="secondary" class="Scan-button" (click)="scanQR()" [disabled]="loading">点我扫描</button>

 ts具体代码:

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

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
public scannedText: string;
  constructor(public navCtrl: NavController) {

  }
  public scanQR() {

    BarcodeScanner.scan().then((barcodeData) => {
      if (barcodeData.cancelled) {
        console.log("User cancelled the action!");
        return false;
      }
      console.log("Scanned successfully!");
      console.log(barcodeData);
      this.scannedText=JSON.stringify(barcodeData);
    }, (err) => {
      console.log(err);
    });
  }
}

 运行时会告诉你缺这个缺那个,只管npm install即可

然后编译成apk的时候会提示没有接受条款,其实是找不到依赖包,根据提示使用SDK Manager装上就行,扫描效果还是很好的。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2021-04-10
  • 2021-11-26
  • 2021-12-04
  • 2022-02-20
猜你喜欢
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-04-14
  • 2021-11-20
相关资源
相似解决方案