Generate QR code using jquery
Go to the topic:
Introduce jquery js and jquery-qrcode.js. I will upload the code demo to the resource library later. The code is simple:
It is easier to generate a QR code for non-Chinese characters.
<script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/jquery.qrcode.min.js"></script><script>$(function() {$("#qrcode").qrcode("welcome to China");});</script>
If it is a Chinese character or contain Chinese characters, then you need to do some encoding conversion, the UTF-16 to the UTF-8
<Script type = "text/javascript" src = "js/jquery. min. js "> </script> <script type =" text/javascript "src =" js/jquery. qrcode. min. js "> </script> <script type =" text/javascript "src =" js/convertCN. js "> </script> <script >$ (function () {$ (" # qrcode "). qrcode (utf16to8 ("Huang Bin") ;}); </script>
The content of convertCN. js is as follows:
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;}
The introduction of the third js is customized to solve the problem that Chinese characters cannot be identified. Compared with Java servlet, jquery is faster, more convenient, and relies on fewer plug-ins.