qrcode.js 是实现二维码计算的核心类
jQuery.qrcode.js 把它用jQuery封装起来,用来实现图像渲染 也就是画图。支持table和canvas
所需环境:jQuery.min.js压缩文件(或jQuery.js)
jQuery.qrcode.min.js压缩文件(或 jQuery.qrcode.js qrcode.js源文件)
utf16to8.js。因为qrcode不支持中文,所以需要将Unicode编码转出utf-8编码,不涉及中文就不用该方法。
生成二维码代码:
$("#qrcodeCanvas).qrcode({render:"canvas",width:256,height:256,text:"https://www.baidu.com"}); 若是中文:text:utf16to8("王小小")
utf16to8.js代码
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; }