安装cordova扫码插件
cordova plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git
然后在要页面上添加调用的按钮
<div ng-controller="erweimaCtrl">
<button ng-click="erweima()">扫描二维码</button>
</div>
然后在控制器里面写调用的方法
angular.module('app').controller('erweimaCtrl', erweimaCtrl);
erweimaCtrl.$inject = ['$scope'];
function erweimaCtrl($scope) {
//二维码
$scope.erweima = ()=> {
cordova.plugins.barcodeScanner.scan(
function(result) {
//扫码成功后执行的回调函数
alert("收到一个二维码\n" +
"扫码文字结果: " + result.text + "\n" +
"格式: " + result.format + "\n" +
"是否在扫码页面取消扫码: " + result.cancelled);
},
function(error) {
//扫码失败执行的回调函数
alert("Scanning failed: " + error);
}, {
preferFrontCamera: true, // iOS and Android 设置前置摄像头
showFlipCameraButton: true, // iOS and Android 显示旋转摄像头按钮
showTorchButton: true, // iOS and Android 显示打开闪光灯按钮
torchOn: false, // Android, launch with the torch switched on (if available)打开手电筒
prompt: "在扫描区域内放置二维码", // Android提示语
resultDisplayDuration: 500, // Android, display scanned text for X ms.
//0 suppresses it entirely, default 1500 设置扫码时间的参数
formats: "QR_CODE", // 二维码格式可设置多种类型
orientation: "portrait", // Android only (portrait|landscape),
//default unset so it rotates with the device在安卓上 landscape 是横屏状态
disableAnimations: true, // iOS 是否禁止动画
disableSuccessBeep: false // iOS 禁止成功后提示声音 “滴”
}
);
}
}