1,下载jquery.js和jquery.qrcode.js 下载链接:jquery.qrcode.min.js
2,创建HTML文件,将2个js引入,代码如下
<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.qrcode.min.js"></script>
<script type="text/javascript">
$(document).ready(function($){
$('#qrcode').qrcode({
width: 100, //设置宽
height: 100, //设置高
text: "https://www.baidu.com" //设置内容
});
})
</script>
</head>
<body>
<div id="qrcode"></div>
</body>
</html>
3,就会在div里面生成二维码,图下
4,参数介绍
render : "canvas", //设置渲染方式 也可以设置成 table
width : 256, //设置宽度
height : 256, //设置高度
typeNumber : -1, //计算模式
correctLevel : QRErrorCorrectLevel.H,//纠错等级
background : "#ffffff",//背景颜色
foreground : "#000000" //前景颜色
text : "xxx" //设置内容
5,中文乱码解决
将字符串解析成UTF-8编码的字符串
function transforUTF8(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;
}