qrcode只通過前端就能生成二維碼和解析二維碼圖片,
首先要引入文件qrcode.js,下載地址為:http://static.runoob.com/download/qrcodejs-04f46c6.zip;
1、識別本地圖片:
通過文件的形式獲取到本地的圖片,生成一個圖片的臨時路徑,然后解析這個二維碼圖片;
qrcode.decode(img)方法能將二維碼圖片解析,通過qrcode.callback=function(img){}將解析的圖片返回文本值;
//獲取預覽圖片路徑
var getObjectURL = function(file){
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
console.log(getObjectURL(newfile[0]));// newfile[0]是通過input file上傳的二維碼圖片文件
qrcode.decode(getObjectURL(newfile[0]));
qrcode.callback = function(imgMsg){
console.log("imgMsg",imgMsg);
}
2、生成二維碼:
var qrcode = new QRCode(document.getElementById("qrcode"), {
width : 100,
height : 100
});
qrcode.makeCode('123123');
生成二維碼
