qrcode.js生成二维

使用到qrcode.js生成二维码

pako.js压缩字符串:https://github.com/nodeca/pako

参照代码如下:

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3     <head>
 4         <title>aa.html</title>
 5         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 6         <meta http-equiv="description" content="this is my page">
 7         <meta http-equiv="content-type" content="text/html; charset=GB18030">
 8         <script type="text/javascript" src="./js/pako.js"></script>
 9         <script type="text/javascript" src="./js/qrcode.js"></script>
10 
11     </head>
12 
13     <body>
14         原始:
15         <div id="qrcode"></div>
16         压缩后:
17         <div id="qrcode2"></div>
18 
19     <script>
20     
21     //var ticketData = 'http://www.baidu.com/efying/com/aa/d/sfa/sf.do?safafasdfasfdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhgggggggggggggggggggggggggggggggsafafasdfasfdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg';
22     var ticketData = 'www.baidu.com';
23     var data = zip(ticketData);
24     console.log("压缩后:");
25     console.log(data);
26     
27     var s = unzip(data);
28     console.log("解压后:");
29     console.log(s);
30     
31     var qrcode = new QRCode(document.getElementById("qrcode"), {
32         width : 166,//设置宽高
33         height : 166
34     
35     });
36     qrcode.makeCode(s);
37     
38     var qrcode2 = new QRCode(document.getElementById("qrcode2"), {
39         width : 166,//设置宽高
40         height : 166
41     
42     });
43     qrcode2.makeCode(data);
44     
45     function unzip(b64Data) {
46         var strData = atob(b64Data);
47     
48         // Convert binary string to character-number array  
49         var charData = strData.split('').map(function(x) {
50             return x.charCodeAt(0);
51         });
52     
53         // Turn number array into byte-array  
54         var binData = new Uint8Array(charData);
55     
56         // // unzip  
57         var data = pako.inflate(binData);
58     
59         // Convert gunzipped byteArray back to ascii string:  
60         strData = String.fromCharCode.apply(null, new Uint16Array(data));
61         return strData;
62     }
63     
64     function zip(str) {
65         var binaryString = pako.gzip(str, {
66             to : 'string'
67         });
68     
69         return btoa(binaryString);
70     }
71     </script>
72     </body>
73 </html>
View Code

原文地址:https://www.cnblogs.com/gexiaoshan/p/6375537.html