代码如下:
<!DOCTYPE html>
<html>
<head>
<title>basic example</title>
</head>
<style type="text/css">
*{margin:0;padding:0;font-family:微软雅黑;}
#qrcode{border:1px solid #AFAFAF;width:200px;height:200px}
</style>
<body>
<script src="../jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../jQuery/jquery.qrcode.js"></script>
<script type="text/javascript" src="../jQuery/qrcode.js"></script>
<p>Render in canvas</p>
<div id="qrcode"></div>
<script>
//中文乱码,将UTF-16转换为UTF-8,UTF-16长度为2位,UTF-8为3位
String.prototype.toUTF8=function(){
var result = ""; //转码后的结果
for(var i=0,maxLen=this.length;i<maxLen;i++) {
var t = this.charCodeAt(i);
if( (t >= 0x0001) && (t <= 0x007F)) {
result += this.charAt(i);
} else if (t > 0x07FF) {
result += String.fromCharCode(0xE0 | ((t >> 12) & 0x0F));
result += String.fromCharCode(0x80 | ((t >> 6) & 0x3F));
result += String.fromCharCode(0x80 | ((t >> 0) & 0x3F));
} else {
result += String.fromCharCode(0xC0 | ((t >> 6) & 0x1F));
result += String.fromCharCode(0x80 | ((t >> 0) & 0x3F));
}
}
return result;
}
jQuery('#qrcode').qrcode({
render : "canvas" , //渲染方式:还可以为table方式
width : 200, //生成二维码的宽度
height : 200, //生成二维码的高度
typeNumber : -1, //计算方式
correctLevel : QRErrorCorrectLevel.H,//纠错等级
background : "#FFFFFF", //背景色
foreground : "#3399CC", //前景色
text : "许增强".toUTF8() //会乱码
});
</script>
</body>
</html>