安裝cordova掃碼插件
cordova plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git
然后在要頁面上添加調(diào)用的按鈕
<div ng-controller="erweimaCtrl">
<button ng-click="erweima()">掃描二維碼</button>
</div>
然后在控制器里面寫調(diào)用的方法
angular.module('app').controller('erweimaCtrl', erweimaCtrl);
erweimaCtrl.$inject = ['$scope'];
function erweimaCtrl($scope) {
//二維碼
$scope.erweima = ()=> {
cordova.plugins.barcodeScanner.scan(
function(result) {
//掃碼成功后執(zhí)行的回調(diào)函數(shù)
alert("收到一個二維碼\n" +
"掃碼文字結(jié)果: " + result.text + "\n" +
"格式: " + result.format + "\n" +
"是否在掃碼頁面取消掃碼: " + result.cancelled);
},
function(error) {
//掃碼失敗執(zhí)行的回調(diào)函數(shù)
alert("Scanning failed: " + error);
}, {
preferFrontCamera: true, // iOS and Android 設(shè)置前置攝像頭
showFlipCameraButton: true, // iOS and Android 顯示旋轉(zhuǎn)攝像頭按鈕
showTorchButton: true, // iOS and Android 顯示打開閃光燈按鈕
torchOn: false, // Android, launch with the torch switched on (if available)打開手電筒
prompt: "在掃描區(qū)域內(nèi)放置二維碼", // Android提示語
resultDisplayDuration: 500, // Android, display scanned text for X ms.
//0 suppresses it entirely, default 1500 設(shè)置掃碼時間的參數(shù)
formats: "QR_CODE", // 二維碼格式可設(shè)置多種類型
orientation: "portrait", // Android only (portrait|landscape),
//default unset so it rotates with the device在安卓上 landscape 是橫屏狀態(tài)
disableAnimations: true, // iOS 是否禁止動畫
disableSuccessBeep: false // iOS 禁止成功后提示聲音 “滴”
}
);
}
}