qrcode actually realized by using jQuery graphics rendering, drawing support canvas (HTML5) and the table are two ways that you can get the latest code to https://github.com/jeromeetienne/jquery-qrcode.
How to use
1, first joined jquery library files and qrcode plugin page.
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.qrcode.min.js"></script>
2,adding the following code in the page where you want to display two-dimensional code:
<div id="code"></div>
3, call qrcode plugin.
qrcode supports two ways canvas and table picture rendering, canvas default mode, the highest efficiency, of course, the browser supports html5. Direct call is as follows:
$('#code').qrcode("http://www.helloweba.com"); //any strings
You can also call as follows:
$("#code").qrcode({
render: "table", //table type
width: 200, //width
height:200, //height
text: "www.goocode.net" //any content
});
This can generate a two-dimensional code in the page, you can use the phone, "sweep the" function to read the two-dimensional code information.
Chinese Recognition
When we tested does not recognize Chinese content found two-dimensional code, multi-information learned by looking, jquery-qrcode is the use charCodeAt () methods for encoding conversion. And this method will get its default Unicode encoding, if there is Chinese content before it is imperative to generate two-dimensional code string into UTF-8, and then generate a two-dimensional code. You can use the following function to convert Chinese string:
function toUtf8(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;
}
Example lists as follows:
var str = toUtf8("Chinese Charactors");
$('#code').qrcode(str);