jquery.qrcode笔记

由于一个坑爹的项目需要生成二维码扫描,后台由于数据库比较麻烦,就只能前端做了,于是乎找到Js生成qrcode的一个库:https://github.com/jeromeetienne/jquery-qrcode,试用了下感觉挺方便的,做个记录。

这是Jquery的一个插件,需要用到Jquery。

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>QRCODE TEST</title>
 6 </head>
 7 <body>
 8     <div id="code"></div>
 9 </body>
10 <script src="./jquery/jquery.js"></script>
11 <script src="./jquery/jquery.qrcode.min.js"></script>
12 <script>
13     function changeToUtf8(str) {    
14     var output, i, len, c;    
15     output = "";    
16     len = str.length;    
17     for(i = 0; i < len; i++) {    
18         c = str.charCodeAt(i);    
19         if ((c >= 0x0001) && (c <= 0x007F)) {    
20             output += str.charAt(i);    
21         } else if (c > 0x07FF) {    
22             output += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));    
23             output += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));    
24             output += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));    
25         } else {    
26             output += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));    
27             output += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));    
28         }    
29     }    
30     return output;    
31 } 
32     var str=changeToUtf8("尼玛");
33     $("#code").qrcode({
34         render:"table",
35         width:200,
36         height:200,
37         text:str
38     });
39 </script>
40 </html>

之所以需要个changeToUft8函数是因为中文字符直接生成二维码再进行扫描读取的话会乱码,qrcode是采用charCodeAt()进行转码的,默认是unicode编码。

posted @ 2016-03-16 10:40  不断学习中的小菜鸟  阅读(1004)  评论(0编辑  收藏  举报