使用到qrcode.js生成二维码
pako.js压缩字符串:https://github.com/nodeca/pako
参照代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>aa.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB18030">
<script type="text/javascript" src="./js/pako.js"></script>
<script type="text/javascript" src="./js/qrcode.js"></script> </head> <body>
原始:
<div id="qrcode"></div>
压缩后:
<div id="qrcode2"></div> <script> //var ticketData = 'http://www.baidu.com/efying/com/aa/d/sfa/sf.do?safafasdfasfdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhgggggggggggggggggggggggggggggggsafafasdfasfdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg';
var ticketData = 'www.baidu.com';
var data = zip(ticketData);
console.log("压缩后:");
console.log(data); var s = unzip(data);
console.log("解压后:");
console.log(s); var qrcode = new QRCode(document.getElementById("qrcode"), {
width : 166,//设置宽高
height : 166 });
qrcode.makeCode(s); var qrcode2 = new QRCode(document.getElementById("qrcode2"), {
width : 166,//设置宽高
height : 166 });
qrcode2.makeCode(data); function unzip(b64Data) {
var strData = atob(b64Data); // Convert binary string to character-number array
var charData = strData.split('').map(function(x) {
return x.charCodeAt(0);
}); // Turn number array into byte-array
var binData = new Uint8Array(charData); // // unzip
var data = pako.inflate(binData); // Convert gunzipped byteArray back to ascii string:
strData = String.fromCharCode.apply(null, new Uint16Array(data));
return strData;
} function zip(str) {
var binaryString = pako.gzip(str, {
to : 'string'
}); return btoa(binaryString);
}
</script>
</body>
</html>