使用ionic2开发一个二维码扫描功能


界面添加一个按钮:

<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装上就行,扫描效果还是很好的。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM