/*扫描二维码*/
var ReadQRCode= {
scan: null, //扫描对象
/*开始识别*/
StartRecognize: function() {
try {
var filter;
//自定义的扫描控件样式
var styles = {
frameColor: "#29E52C",
scanbarColor: "#29E52C",
background: ""
}
//扫描控件构造
ReadQRCode.scan = new plus.barcode.Barcode('bcid', filter, styles);
ReadQRCode.scan.onmarked = ReadQRCode.Onmarked;
ReadQRCode.scan.onerror = ReadQRCode.Onerror;
ReadQRCode.scan.start();
//打开关闭闪光灯处理
var flag = false;
document.getElementById("turnTheLight").addEventListener('tap', function() {
if(flag == false) {
ReadQRCode.scan.setFlash(true);
flag = true;
} else {
ReadQRCode.scan.setFlash(false);
flag = false;
}
});
} catch(e) {
alert("出现错误啦:\n" + e);
}
},
/*错误回调防范*/
Onerror:function(e) {
mui.alert(e);
},
/*标记是类型*/
Onmarked:function(type, result) {
var returnValue = {};
var type="";
switch(type) {
case plus.barcode.QR:
type = 'QR';
break;
case plus.barcode.EAN13:
type = 'EAN13';
break;
case plus.barcode.EAN8:
type = 'EAN8';
break;
}
returnValue.Type=type;
returnValue.Code=result
mui.back(returnValue);
},
//返回
BackPrePage:function(){
var returnValue = {};
returnValue.Type="QR";
returnValue.Code="1234567"
mui.back(returnValue);
}
}
mui.init({
swipeBack: true
});
/*初始化方法*/
mui.plusReady(function() {
var old_back = mui.back;
mui.back = function(data) {
//执行mui封装好的窗口关闭逻辑;
var obj = plus.webview.getWebviewById(plus.webview.currentWebview().PageID);
if(data != null && data != undefined) {
//执行自定义事件
mui.fire(obj, 'ReadQRClose', {
data
});
old_back();
} else {
mui.toast('未识别到数据!');
old_back();
}
};
setTimeout(function(){ ReadQRCode.StartRecognize(); }, 1000);
});