html5中可以使用二維碼掃描,也可以從相冊中選擇二維碼識别,代碼如下
var ws = null, wo = null;
var scan = null, domready = false;
// H5 plus事件處理
function plusReady() {
if (ws || !window.plus || !domready) {
return;
}
// 擷取視窗對象
ws = plus.webview.currentWebview();
wo = ws.opener();
// 開始掃描
ws.addEventListener('show', function () {
scan = new plus.barcode.Barcode('bcid');
scan.onmarked = onmarked;
scan.start();
});
// 顯示頁面并關閉等待框
ws.show("pop-in");
wo.evalJS("closeWaiting()");
}
if (window.plus) {
plusReady();
} else {
document.addEventListener("plusready", plusReady, false);
}
// 監聽DOMContentLoaded事件
document.addEventListener("DOMContentLoaded", function () {
domready = true;
plusReady();
}, false);
// 二維碼掃描成功
function onmarked(type, result, file) {
switch (type) {
case plus.barcode.QR:
type = "QR";
break;
case plus.barcode.EAN13:
type = "EAN13";
break;
case plus.barcode.EAN8:
type = "EAN8";
break;
default:
type = "其它";
break;
}
result = result.replace(/\n/g, '');
wo.evalJS("scaned('" + type + "','" + result + "','" + file + "');");
plus.webview.currentWebview().close();
back();
}
// 從相冊中選擇二維碼圖檔
function scanPicture() {
plus.gallery.pick(function (path) {
plus.barcode.scan(path, onmarked, function (error) {
plus.nativeUI.alert("無法識别此圖檔");
});
}, function (err) {
});
}
作者:
Tyler Ning出處:
http://www.cnblogs.com/tylerdonet/本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,如有問題,可以通過以下郵箱位址
[email protected]聯系我,非常感謝。