使用jquery.qrcode.js生成二维码

通常生成二维码的方式有两种:第一种是java代码的形式,第二种是通过Js方式。

在这里我做个记录,用js生成二维码,可以在官网下载源码:http://jeromeetienne.github.io/jquery-qrcode/

1.如何使用jquery.qrcode.js

其实就是引入压缩后的文件,一个是jquery.min.js和jquery.qrcode.min.js

1 <script src="js/jquery.min.js"></script>
2 <script src="js/jquery.qrcode.min.js"></script>

2.调用方式,并生成logo图标:

1 <script>
2         $(function(){
3             $("#qrCode").qrcode({render:'canvas', 200,height: 200,text: "http://www.baidu.com"});
4             var margin = ($("#qrCode").height() - $("#qrCodeIco").height()) / 2; //控制Logo图标的位置
5             $("#qrCodeIco").css("margin", margin);
6         })
7 </script>

3.html代码

1 <body>
2 <div id="qrCode">
3     <img id="qrCodeIco" src="img/timg.jpeg" style="position: absolute;30px;height: 30px;"/>
4 </div>
5 
6 </body>

4.对中文的支持

 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));
                }
            }
            return out;
        }
原文地址:https://www.cnblogs.com/niechen/p/6864051.html