qrcode.js的識別解析二維碼圖片和生成二維碼圖片


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');

生成二維碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM