對於MUI的框架在使用攝像頭直接掃碼和選擇相冊圖片進行掃碼得出的參數不一致。
根據路相冊徑掃描圖片掃出參數type得出的二維碼類型是QR_CODE,而官方給出的二維碼類型是plus.barcode.QR;
這裏所以每次導致掃碼類型的匹配不成功,需要手動對QR_CODE進行判斷重新傳入type = 0.
同時對於掃描相冊圖片的另一個參數result得出的一個有包含雙引號的數據,而直接掃碼得出的是不包含雙引號.可能導致數據出入後端的錯誤;需要手動的對雙引號進行刪除拿到原始數據.
參考代碼:
mui.plusReady(function(){
//避免卡頓對其添加500ms的延遲
setTimeout("startScan()", "500");
//綁定從相冊選擇二維碼,link_select_qrcode是對應的跳轉的鏈接元素,對其進行事件綁定
var link_select_qrcode = document.getElementById("link_select_qrcode");
link_select_qrcode.addEventListener("tap", function() {
gallery();
});
});
var scan;
function startScan() {
var styles = {
frameColor: "#128E12",
scanbarColor: "#0062CC",
background: ""
}
scan = new plus.barcode.Barcode('scanComponent', null, styles);
scan.onmarked = onmarked;
scan.start();
}
function onmarked( type, result ) {
if (type == plus.barcode.QR) {
//這裏有一個bug,根據相冊掃描的二維碼參數包含了引號,需要做去除引號的處理
result = result.replace("\"","").replace("\"","")
var content = result.split("miniwechat_qrcode:");
console.log(result);
//......做一些請求的處理....
}
scan.start();
}
function gallery(){
if(!window.plus) return;
plus.gallery.pick( function(path){
alert(path);
plus.barcode.scan(path, function(type,result) {
if(type == "QR_CODE"){
onmarked(0, result);
} else{
onmarked(type, result)
}
}, function (error) {
plus.nativeUI.alert('無法識別此圖片');
});
}, function ( e ) {
console.log( "取消選擇圖片" );
}, {filter:"image"});
}