• jquery.qrcode二维码插件生成彩色二维码


    jquery.qrcode.js 是居于jquery类库的绘制二维码的插件,用它来实现二维码图形渲染支持canvas和table两种绘图方式。
    (jquery.qrcode.js 设置显示方式为table时在webkit核心浏览器如chrome下会变形)

    以下是测试代码(增加了颜色控制,可以设置4个区块的颜色值,需要指定render为table。),效果:
    <a href=http://www.jbxue.com/jb/jquery/ target=_blank class=jquery.qrcode生成彩色二维码" src="http://www.jbxue.com/d/file/2014/08/20140808213149602.jpg" height="149" width="217">

    完整代码:
     

    复制代码 代码示例:
    <html>
    <head>
    <title>JS生成二维码-www.jbxue.com</title>
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="jquery.qrcode.min.js"></script>
    <style>
     #output{
      margin-left:300px;
      margin-top:100px;
     }
    </style>
    </head>
    <body>
    <div id="output"></div>
    <script>
     window.onload = function () {
      var trs = $('#output').qrcode({
       100,
       height: 100,
       render: "canvas", //设置渲染方式 table canvas
       text: utf16to8("javascript生成二维码"),
       background: "#ffffff", //背景颜色
       foreground: "red" //前景颜色
      }).find('tr'), trLen = Math.floor(trs.size() / 2), tdLen = Math.floor(trs.eq(0).find('td').size() / 2), tds, bgColor;
      var colors = [['#ff0000', '#0100e2'], ['#00ed01', '#9f4d95']];
      trs.each(function (j) {
       tds = $(this).find('td');
       tds.each(function (i) {
        bgColor = this.style.backgroundColor;
        if (bgColor == 'red') {
         this.style.backgroundColor = colors[j < trLen ? 0 : 1][i < tdLen ? 0 : 1];
        }
       });
      });
     }
     function utf16to8(str) {
      var out, i, len, c;
      out = "";
      len = str.length;
      for (i = 0; i < len; i++) {
       c = str.charCodeAt(i);
       if ((c >= 0x0001) && (c <= 0x007F)) {
        out += str.charAt(i);
       } else if (c > 0x07FF) {
        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
        out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
       } else {
        out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
       } // www.jbxue.com
      }
      return out;
     }
    </script>
     
    </body>
    </html>
     

    代码说明:
    jquery-qrcode这个库是采用 charCodeAt这个方式进行编码转换的,而这个方法默认会获取它的 Unicode 编码,一般的解码器都是采用UTF-8, ISO-8859-1等方式,英文是没有问题,如果是中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位,这样二维码的编解码就不匹配了。
     
    解决方法:
    在二维码编码前把字符串转换成UTF-8,具体代码如上utf16to8函数所示。

    转载 http://www.jbxue.com/article/24133.html  收藏一下

    不平凡的意义在于坚持
  • 相关阅读:
    ubuntu 安装docker
    docker 版本与查看某个容器详情信息
    linux namespace 分为有名和无名两种,默认情况下都是有名的
    查看centos 版本信息
    centos7 一键安装openstack 方法
    centos8 安装openstack (失败告终),参见centos7安装
    Windows7 安装docker工具的方法
    网络架构--防火墙双机热备(主备、负载均衡)
    ubuntu server 无线网口配置
    ubuntu server 18.04 单机安装openstack
  • 原文地址:https://www.cnblogs.com/wluomo/p/3930128.html
Copyright © 2020-2023  润新知