jQuery QRcode


jquery.qrcode.js 是一个能够在客户端生成矩阵二维码QRCode 的jquery插件 ,使用它可以很方便的在页面上生成二维条码。

Quick Start

引用jquery,jquery-qrcode

<script type="text/javascript" src="../../scripts/jquery-1.10.1.min.js"></script>
<script src="../../scripts/jquery.qrcode.min.js" type="text/javascript"></script>

定义div

<div id="code"></div>

调用

function showQRCodeDiv() &#123;
    var row = mastergrid.getRowByUID(row_uid);
    $("#code").text("").qrcode(&#123;
        render: "canvas", //table/canvas方式
        width: 228, //宽度
        height:228, //高度
        text: utf16to8(‘测试’) //二维码
    &#125;);
&#125;

function utf16to8(str) &#123;
    var out, i, len, c;
    out = "";
    len = str.length;
    for(i = 0; i < len; i++) &#123;
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) &#123;
            out += str.charAt(i);
        &#125; else if (c > 0x07FF) &#123;
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        &#125; else &#123;
            out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        &#125;
    &#125;
    return out;
&#125;

function saveQRCodeDiv() &#123;
    var myCanvas = $("canvas")[0];
    var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");

    var link = document.createElement('a');
    link.href = image;
    var filename = mini.get("bqcode_2").getValue() + '.png';
    link.download = filename;
    var event = document.createEvent('MouseEvents');
    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    link.dispatchEvent(event);
&#125;

文章作者: Jacboo
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Jacboo !
评论
  目录