用二維碼掃描
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.rzrgm.cn/tylerdonet/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,請微信聯系冬天里的一把火
浙公網安備 33010602011771號