Identifying strings
1, first download Jquery.qrcode plug-in, the page to introduce the jquery library files and QRCode plug-ins.
<scripttype="Text/javascript"src="Jquery.js"></script>
<scripttype="Text/javascript"src="Jquery.qrcode.min.js"></script>
2, in the page need to display two-dimensional code place to add tags:
<div id="qrcode" style="text-align: center;"></div>
3, call QRCode plug-in.
QRCode supports canvas and table two ways of rendering pictures, the default use of canvas, the most efficient, of course, the browser supports HTML5. The call is as follows:
$(‘#qrcode‘).qrcode("http://www.baidu.com"); //任意字符串
It can also be called in the following ways:
$("#qrcode"). QRCode ({
Render"Table",//table Way
Width $,//Width
Height $,//Height
Text"Www.baidu.com"//arbitrary content
});
This allows you to generate a QR code in the page. Next, you can use the mobile phone "sweep" function to read the QR code information.
Identify Chinese
When we tried to find the two-dimensional code that did not recognize the Chinese content, we found that the Jquery-qrcode was encoded by the charCodeAt () method. This method will get its Unicode encoding by default, and if it has Chinese content, it will convert the string to UTF-8 before generating the QR code, and then generate the QR code. You can use the following functions to convert Chinese strings:
functionToUtf8 (str){
varOut, 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);
} Elseif(C > 0x07ff){
Out + =String. fromCharCode (0xE0 | (c >> A) (& 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));
}
}
returnOut
}
The following example:
var str = toUtf8("中文二维码!");
$(‘#qrcode‘).qrcode(str);
Ps:
Jquery.qrcode.min.js
Https://github.com/jack0888/jquery-qrcode
Https://github.com/jeromeetienne/jquery-qrcode
Jquery.qrcode generate two-dimensional code