zoukankan      html  css  js  c++  java
  • 转: jquery.qrcode.js生成二维码插件&转成图片格式

    原文地址: https://blog.csdn.net/u011127019/article/details/51226104

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

    github源码地址:

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

    参数说明:

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

    2.使用实例:

    插件引用

    1.  <script src="../Js/jquery-1.11.3.min.js"></script>  
    2.  <script src="../Js/jquery-qrcode-master/jquery.qrcode.min.js"></script>  

    简单实例1:

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

    简单实例2:

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

    10. </script>  

     

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

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

    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.           150,  
    27.          height: 150  
    28.      });  
    29.    
    30.     //就目前 微信/支付宝等 不识别其他颜色的二维码  
    31.      $('#code').qrcode({  
    32.          text: toUtf8('我是tianma'),  
    33.           150,  
    34.          height: 150,  
    35.          background: '#f00',  
    36.          foreground: '#0f0'  
    37.      });  

    38. </script>  

     

    实例4:

    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.      150,  
    7.     height: 150  
    8. });  

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

    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处理:

    1. //默认使用Canvas画图  
    2. var qrcode = $('#code').qrcode({  
    3.     text: '@url',  
    4.      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. });  

     

  • 相关阅读:
    Linux Ctrl+Z的使用方法
    cx_Oracle库导入失败引起crontab中python程序运行失败,并且无错误提示
    cx_Oracle库导入失败引起crontab中python程序运行失败,并且无错误提示
    python __file__ 与相对路径
    ORACLE之手动注册监听listener。alter system set local_listener="XXX"
    pl/sql developer 连接本地ORACLE 11g 64位数据库
    在linux设置环境变量
    通过Instant Client包来使用SQL*PLUS
    linux 安装oracle 11g
    plsql developer 使用技巧
  • 原文地址:https://www.cnblogs.com/wind-wang/p/9152463.html
Copyright © 2011-2022 走看看