目前流行的三大国际标准
1 PDF417:不支持中文
2 DM:专利未公开,需支付专利费用
3 QR Code : 专利公开,支持中文
QR code 比其他二维码相比,具有识读速度快,数据密度大,占用空间小的优势.
纠错能力:
L级 : 约可纠错7%的数据码字
M级:约可纠错15%的数据码字
Q级:约可纠错25%的数据码字
H级:约可纠错30%的数据码字
php生成QRcode:
http://phpqrcode.sourceforge.net/ 下载源码
引入qrlib.php 使用QRcode下面的静态方法 QRcode ::png();
//public static function
//png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
// { }
$text :二维码内容,$outfile:二维码保存文件名称,$level:纠错级别,$size:二维码大小,$margin:边框.$saveandprint是否在浏览器输出并保存为文件,(bug:需要将qrencode.php $saveandprint =false改掉);
include "phpqrcode/qrlib.php";
QRcode::png(‘this is test‘);
jquery 生成QR Code:
https://github.com/jeromeetienne/jquery-qrcode 下载
使用其qrcode.min.js文件需要先使用jquery文件
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="jquery-qrcode-master/jquery.qrcode.min.js"></script>
<script type="text/javascript">
$(‘#qrcode‘).qrcode("this is test);
$(‘#qrcode‘).qrcode({width: 64,height: 64,text:"this is test" });
</script>
生成中文产生乱码需要
<script type="text/javascript">
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>
即可解决
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="qrcode"></div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="jquery-qrcode-master/jquery.qrcode.min.js"></script>
<script type="text/javascript">
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>
<script type="text/javascript">
$(‘#qrcode‘).qrcode("this plugin is great");
$(‘#qrcode‘).qrcode({width: 64,height: 64,text:utf16to8("size doesn‘t matter 爱你") });
</script>
</body>
</html>