jquery.qrcode.js生成二维码插件&转成图片格式

1.qrcode其实是通过使用jQuery实现图形渲染,画图,支持canvas(HTML5)和table两种方式,

github源码地址:

https://github.com/jeromeetienne/jquery-qrcode

参数说明:

 

[javascript]  view plain  copy
 
  1. render   : "canvas",//设置渲染方式    
  2. width       : 256,     //设置宽度    
  3. height      : 256,     //设置高度    
  4. typeNumber  : -1,      //计算模式    
  5. correctLevel    : QRErrorCorrectLevel.H,//纠错等级    
  6. background      : "#ffffff",//背景颜色    
  7. foreground      : "#000000" //前景颜色    

 

2.使用实例:

插件引用

[html]  view plain  copy
 
  1. <script src="../Js/jquery-1.11.3.min.js"></script>  
  2.  <script src="../Js/jquery-qrcode-master/jquery.qrcode.min.js"></script>  

简单实例1:

[html]  view plain  copy
 
  1. <div id="code"></div>  
  2. <script>  
  3.     //任意字符串 生成二维码  
  4.     //默认使用Canvas画图  
  5.     $('#code').qrcode('http://blog.csdn.net/u011127019');  
  6. </script>  

简单实例2:

[html]  view plain  copy
 
  1. <div id="code"></div>  
  2. <script>  
  3.     //table 模式兼容 IE低版本  
  4.     $('#code').qrcode({  
  5.         render: 'table',  
  6.         width: 100,  
  7.         height: 100,  
  8.         text: 'http://blog.csdn.net/u011127019'  
  9.     });  
  10. </script>  

简单实例3(中文支持):

我们试验的时候发现不能识别中文内容的二维码,通过查找多方资料了解到,jquery-qrcode是采用charCodeAt()方式进行编码转换的。而这个方法默认会获取它的Unicode编码,如果有中文内容,在生成二维码前就要把字符串转换成UTF-8,然后再生成二维码。

[html]  view plain  copy
 
  1. <div id="code"></div>  
  2. <script>  
  3.     //如果内容中有中文,在生成二维码钱就要把字符串转换成utf-8  
  4.     function toUtf8(str) {  
  5.         var out, i, len, c;  
  6.         out = "";  
  7.         len = str.length;  
  8.         for (i = 0; i < len; i++) {  
  9.             c = str.charCodeAt(i);  
  10.             if ((c >= 0x0001) && (c <= 0x007F)) {  
  11.                 out += str.charAt(i);  
  12.             } else if (c > 0x07FF) {  
  13.                 out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));  
  14.                 out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));  
  15.                 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));  
  16.             } else {  
  17.                 out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));  
  18.                 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));  
  19.             }  
  20.         }  
  21.         return out;  
  22.     }  
  23.   
  24.     $('#code').qrcode({  
  25.         text: toUtf8('我是tianma'),  
  26.         width: 150,  
  27.         height: 150  
  28.     });  
  29.   
  30.     //就目前 微信/支付宝等 不识别其他颜色的二维码  
  31.     $('#code').qrcode({  
  32.         text: toUtf8('我是tianma'),  
  33.         width: 150,  
  34.         height: 150,  
  35.         background: '#f00',  
  36.         foreground: '#0f0'  
  37.     });  
  38. </script>  

实例4:

[html]  view plain  copy
 
  1. //text 属性的值长度不能太长,最大字节数 10208  
  2. //text 字符串太长 微信/支付宝等扫一扫无法识别,微博识别内容更多  
  3. //微博扫一扫:大约200 字以内,微信扫一扫:大约 160字以内,支付宝扫一扫:大约130字符以内  
  4. $('#code').qrcode({  
  5.     text: toUtf8('SignalR 是 ASP.NET 团队正在开发的一个 Microsoft .NET Framework 库和 jQuery 插件,可能包括在以后版本的 ASP.NET 平台中。 它提供了一些前景极为光明的功能,而这些功能正是,并且是越来越多的,当前不曾具有的,'),  
  6.     width: 150,  
  7.     height: 150  
  8. });  

实例5,将生成的二维码转换成图片格式:

 

[html]  view plain  copy
 
  1.   <div id="divOne"></div>  
  2.   <img id='imgOne'  style='border:1px solid red;'/>  
  3. <script>  
  4. //默认使用Canvas生成,并显示到图片   
  5.  var qrcode= $('#divOne').qrcode('http://www.gongjuji.net/').hide();   
  6.  var canvas=qrcode.find('canvas').get(0);  
  7.  $('#imgOne').attr('src',canvas.toDataURL('image/jpg'))  
  8. </script>  

显示结果:

 

实例6,在当前的图片上添加文字或logo处理:

 

 

[javascript]  view plain  copy
 
  1. //默认使用Canvas画图  
  2. var qrcode = $('#code').qrcode({  
  3.     text: '@url',  
  4.     width: 400,  
  5.     height: 400  
  6. }).hide();  
  7. //添加文字  
  8. var text = "测试文字内容";//设置文字内容  
  9. var canvas = qrcode.find('canvas').get(0);  
  10. var oldCtx = canvas.getContext('2d');  
  11. var imgCanvas = document.getElementById('imgCanvas');  
  12. var ctx = imgCanvas.getContext('2d');  
  13. ctx.fillStyle = 'white';  
  14. ctx.fillRect(0,0,imgCanvas.width,imgCanvas.height);  
  15. ctx.putImageData(oldCtx.getImageData(0, 0, canvas.width, canvas.height), 0, 0);  
  16. ctx.fillStyle = 'red';  
  17. ctx.strokStyle = 'rgb(1,1,0)';  
  18. //ctx.stroke = 3;  
  19. ctx.textBaseline = 'middle';  
  20. ctx.textAlign = 'center';  
  21. ctx.font = 'bolder 30px Helvetica';  
  22. ctx.fillText(text, imgCanvas.width / 2, imgCanvas.height - 20);  
  23. ctx.strokeText(text, imgCanvas.width / 2, imgCanvas.height - 20);  
  24. imgCanvas.style.display = 'none';  
  25. $('#imgCode').attr('src', imgCanvas.toDataURL('image/png')).css({  
  26.     maxWidth:300  
  27. });  

 

 

 

 

转自:http://www.helloweba.com/view-blog-226.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值