js生成二維碼,支持打印顯示


Jquery中的二維碼生成插件jquery.qrcode.js,

導入2個Jq文件:qrcode.js (二維碼生成算法)    jquery.qrcode.js(使用配置)

 

下載鏈接:

鏈接:https://pan.baidu.com/s/1fDpoNPhDtAocho-uTl9xQg
提取碼:b9go

 

基本使用方法:

$("#id").qrcode("http://......");  //在指定元素中生成二維碼

注意:原本qrcode生成的是canvas元素,而canvas是無法打印出來的,會顯示為空白,應將canvas轉換為image返回

 

在jquery.qrcode.js中creatCanvas方法中修改:

var createCanvas    = function(){
            // create the qrcode itself
            var qrcode    = new QRCode(options.typeNumber, options.correctLevel);
            qrcode.addData(options.text);
            qrcode.make();

            // create canvas element
            var canvas    = document.createElement('canvas');
            canvas.width    = options.width;
            canvas.height    = options.height;
            var ctx        = canvas.getContext('2d');

            // compute tileW/tileH based on options.width/options.height
            var tileW    = options.width  / qrcode.getModuleCount();
            var tileH    = options.height / qrcode.getModuleCount();

            // draw in the canvas
            for( var row = 0; row < qrcode.getModuleCount(); row++ ){
                for( var col = 0; col < qrcode.getModuleCount(); col++ ){
                    ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
                    var w = (Math.ceil((col+1)*tileW) - Math.floor(col*tileW));
                    var h = (Math.ceil((row+1)*tileW) - Math.floor(row*tileW));
                    ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);  
                }    
            }
var image=new Image(); image.src=canvas.toDataURL("image/png"); image.id="qrcode";
       return image;
// return canvas }

 

 

轉換方法:

function(canvas){    
var image=new Image(); image.src=canvas.toDataURL("image/png"); image.id="qrcode";
   return image;
}

 

  

 


免責聲明!

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



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