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