需要引入两个js文件:
1)jquery.min.js
2)jquery.qrcode.min.js
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>生成名片二维码</title>
</head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.qrcode.min.js"></script>
<style type="text/css" >
body {
position: absolute; top: 0%;
left: 0%; width:100%;
height:100%;
background-color: #a7aaad;
-moz-opacity: 0.7;
opacity:0.5;
z-index:1001;
filter: alpha(opacity=40);
}
.back{
background-image: url("image/img1.jpg");
z-index:100;
}
</style>
<script type="text/javascript">
$(function(){
//此处生成名片二维码
var the_text = utf16to8("https://www.zhihu.com/question/40117062");
//alert(the_text);
$('#qrcodeid').qrcode({
width:140,
height:140,
render:"canvas", //设置渲染方式 table canvas
typeNumber : -1, //计算模式
correctLevel : 0,//纠错等级
background : "#ffffff",//背景颜色
foreground : "#000000",//前景颜色
text:the_text
});
});
function utf16to8(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;
}
</script>
<body >
<div class="back"></div>
<div id = "qrcodeid"></div> <!-- 生成的二维码放在此div中 -->
</body>
</html>