Jquery.qrcode.js is a plug-in that draws two-dimensional code for the jquery class library, and uses it to implement two-dimensional code graphics rendering support for canvas and table two drawing methods.
(Jquery.qrcode.js settings are displayed as table when the WebKit core browser is distorted under chrome)
Here is the test code (added color control, you can set a color value of 4 blocks, you need to specify render as table.) ), Effect:
Jquery.qrcode Generate color QR code "src=" http://www.jbxue.com/d/file/2014/08/20140808213149602.jpg "height=" 149 "width=" 217 " >
Full code:
Copy Code code example:<title>js generate two-dimensional code-www.jbxue.com</title>
<script type= "Text/javascript" src= "Jquery-1.4.2.min.js" ></script>
<script type= "Text/javascript" src= "Jquery.qrcode.min.js" ></script>
<style>
#output {
margin-left:300px;
margin-top:100px;
}
</style>
<body>
<div id= "Output" ></div>
<script>
Window.onload = function () {
var TRS = $ (' #output '). QRCode ({
WIDTH:100,
HEIGHT:100,
Render: "Canvas",//Set render mode table canvas
Text:utf16to8 ("JavaScript generates QR Code"),
Background: "#ffffff",//Background color
Foreground: "Red"//foreground color
}). Find (' tr '), Trlen = Math.floor (Trs.size ()/2), Tdlen = Math.floor (Trs.eq (0). Find (' TD '). Size ()/2), TDs, BgColor;
var colors = [[' #ff0000 ', ' #0100e2 '], [' #00ed01 ', ' #9f4d95 '];
Trs.each (function (j) {
TDS = $ (this). Find (' TD ');
Tds.each (function (i) {
BgColor = This.style.backgroundColor;
if (BgColor = = ' Red ') {
This.style.backgroundColor = Colors[j < Trlen 0:1][i < Tdlen? 0:1];
}
});
});
}
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 >> b) & 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));
}//Www.jbxue.com
}
return out;
}
</script>
</body>
Code Description:
Jquery-qrcode This library is used charcodeat this way to encode the conversion, and this method by default will get its Unicode encoding, the general decoder is used UTF-8, iso-8859-1, etc., English is no problem, if it is Chinese, In general, Unicode is UTF-16 implementation, length 2 bits, and UTF-8 encoding is 3 bits, so that the codec of the two-dimensional code does not match.
Workaround:
Convert the string to UTF-8 before the QR code is encoded, as shown in the Utf16to8 function above.
Reprint Http://www.jbxue.com/article/24133.html Collection
Jquery.qrcode Two-dimensional code plugin to generate color QR code