這篇文章主要介紹了利用jquery.qrcode在頁面上生成二維碼且支持中文。需要的朋友可以過來參考下,希望對大家有所幫助
實例如下: 代碼如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用jquery-qrcode生成二維碼,支持中文</title> <script src="../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> <script src="jquery.qrcode.min.js" type="text/javascript"></script> <script type="text/javascript"> 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; } $(function () { jQuery('#output').qrcode(utf16to8("今天天氣不錯")); }) </script> </head> <body> <div id="output"></div> </body> </html> 復制代碼 代碼如下: //jquery.qrcode.min.js (function (r) { r.fn.qrcode = function (h) { var s; function u(a) { this.mode = s; this.data = a } function o(a, c) { this.typeNumber = a; this.errorCorrectLevel = c; this.modules = null; this.moduleCount = 0; this.dataCache = null; this.dataList = [] } function q(a, c) { if (void 0 == a.length) throw Error(a.length + "/" + c); for (var d = 0; d < a.length && 0 == a[d]; ) d++; this.num = Array(a.length - d + c); for (var b = 0; b < a.length - d; b++) this.num[b] = a[b + d] } function p(a, c) { this.totalCount = a; this.dataCount = c } function t() { this.buffer = []; this.length = 0 } u.prototype = { getLength: function () { return this.data.length }, write: function (a) { for (var c = 0; c < this.data.length; c++) a.put(this.data.charCodeAt(c), 8) } }; o.prototype = { addData: function (a) { this.dataList.push(new u(a)); this.dataCache = null }, isDark: function (a, c) { if (0 > a || this.moduleCount <= a || 0 > c || this.moduleCount <= c) throw Error(a + "," + c); return this.modules[a][c] }, getModuleCount: function () { return this.moduleCount }, make: function () { if (1 > this.typeNumber) { for (var a = 1, a = 1; 40 > a; a++) { for (var c = p.getRSBlocks(a, this.errorCorrectLevel), d = new t, b = 0, e = 0; e < c.length; e++) b += c[e].dataCount; for (e = 0; e < this.dataList.length; e++) c = this.dataList[e], d.put(c.mode, 4), d.put(c.getLength(), j.getLengthInBits(c.mode, a)), c.write(d); if (d.getLengthInBits() <= 8 * b) break } this.typeNumber = a } this.makeImpl(!1, this.getBestMaskPattern()) }, makeImpl: function (a, c) { this.moduleCount = 4 * this.typeNumber + 17; this.modules = Array(this.moduleCount); for (var d = 0; d < this.moduleCount; d++) { this.modules[d] = Array(this.moduleCount); for (var b = 0; b < this.moduleCount; b++) this.modules[d][b] = null } this.setupPositionProbePattern(0, 0); this.setupPositionProbePattern(this.moduleCount - 7, 0); this.setupPositionProbePattern(0, this.moduleCount - 7); this.setupPositionAdjustPattern(); this.setupTimingPattern(); this.setupTypeInfo(a, c); 7 <= this.typeNumber && this.setupTypeNumber(a); null == this.dataCache && (this.dataCache = o.createData(this.typeNumber, this.errorCorrectLevel, this.dataList)); this.mapData(this.dataCache, c) }, setupPositionProbePattern: function (a, c) { for (var d = -1; 7 >= d; d++) if (!(-1 >= a + d || this.moduleCount <= a + d)) for (var b = -1; 7 >= b; b++)-1 >= c + b || this.moduleCount <= c + b || (this.modules[a + d][c + b] = 0 <= d && 6 >= d && (0 == b || 6 == b) || 0 <= b && 6 >= b && (0 == d || 6 == d) || 2 <= d && 4 >= d && 2 <= b && 4 >= b ? !0 : !1) }, getBestMaskPattern: function () { for (var a = 0, c = 0, d = 0; 8 > d; d++) { this.makeImpl(!0, d); var b = j.getLostPoint(this); if (0 == d || a > b) a = b, c = d } return c }, createMovieClip: function (a, c, d) { a = a.createEmptyMovieClip(c, d); this.make(); for (c = 0; c < this.modules.length; c++) for (var d = 1 * c, b = 0; b < this.modules[c].length; b++) { var e = 1 * b; this.modules[c][b] && (a.beginFill(0, 100), a.moveTo(e, d), a.lineTo(e + 1, d), a.lineTo(e + 1, d + 1), a.lineTo(e, d + 1), a.endFill()) } return a }, setupTimingPattern: function () { for (var a = 8; a < this.moduleCount - 8; a++) null == this.modules[a][6] && (this.modules[a][6] = 0 == a % 2); for (a = 8; a < this.moduleCount - 8; a++) null == this.modules[6][a] && (this.modules[6][a] = 0 == a % 2) }, setupPositionAdjustPattern: function () { for (var a = j.getPatternPosition(this.typeNumber), c = 0; c < a.length; c++) for (var d = 0; d < a.length; d++) { var b = a[c], e = a[d]; if (null == this.modules[b][e]) for (var f = -2; 2 >= f; f++) for (var i = -2; 2 >= i; i++) this.modules[b + f][e + i] = -2 == f || 2 == f || -2 == i || 2 == i || 0 == f && 0 == i ? !0 : !1 } }, setupTypeNumber: function (a) { for (var c = j.getBCHTypeNumber(this.typeNumber), d = 0; 18 > d; d++) { var b = !a && 1 == (c >> d & 1); this.modules[Math.floor(d / 3)][d % 3 + this.moduleCount - 8 - 3] = b } for (d = 0; 18 > d; d++) b = !a && 1 == (c >> d & 1), this.modules[d % 3 + this.moduleCount - 8 - 3][Math.floor(d / 3)] = b }, setupTypeInfo: function (a, c) { &