使用jquery.qrcode.min.js实现中文转化二维码

前端之家收集整理的这篇文章主要介绍了使用jquery.qrcode.min.js实现中文转化二维码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天还是要讲一下关于二维码的知识,前几篇讲解中有讲到我使用的可以生成二维码的js是qrcode.js,然后结合Cordovad的插件$cordovaBarcodeScanner插件可以扫描二维码,这样就基本完成了简单的扫一扫功能。后来在项目进行,开始要调用后台数据和传参数到接口的时候发现qrcode.js它只能解析英文或者数字,并且只能解析一个字符串!

而我所需要的是将一个对象转化成二维码,然后在扫描的时候取出对象中的数据,显然qrcode.js不能实现,经过查找,找到另一个jquery.qrcode.min.js(支持中文转化二维码).

下面让我来介绍一下具体的步骤和代码实现吧!

一、这是我找到的jquery.qrcode.min.js(支持中文转化二维码)代码,需要引入到你的项目里去:

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 + 1),a.lineTo(e,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) { for (var d = j.getBCHTypeInfo(this.errorCorrectLevel << 3 | c),b = 0; 15 > b; b++) { var e = !a && 1 == (d >> b & 1); 6 > b ? this.modules[b][8] = e : 8 > b ? this.modules[b + 1][8] = e : this.modules[this.moduleCount - 15 + b][8] = e } for (b = 0; 15 > b; b++) e = !a && 1 == (d >> b & 1),8 > b ? this.modules[8][this.moduleCount - b - 1] = e : 9 > b ? this.modules[8][15 - b - 1 + 1] = e : this.modules[8][15 - b - 1] = e; this.modules[this.moduleCount - 8][8] = !a },mapData: function (a,c) { for (var d = -1,b = this.moduleCount - 1,e = 7,f = 0,i = this.moduleCount - 1; 0 < i; i -= 2) for (6 == i && i--; ; ) { for (var g = 0; 2 > g; g++) if (null == this.modules[b][i - g]) { var n = !1; f < a.length && (n = 1 == (a[f] >>> e & 1)); j.getMask(c,b,i - g) && (n = !n); this.modules[b][i - g] = n; e--; -1 == e && (f++,e = 7) } b += d; if (0 > b || this.moduleCount <= b) { b -= d; d = -d; break } } } }; o.PAD0 = 236; o.PAD1 = 17; o.createData = function (a,d) { for (var c = p.getRSBlocks(a,c),b = new t,e = 0; e < d.length; e++) { var f = d[e]; b.put(f.mode,4); b.put(f.getLength(),j.getLengthInBits(f.mode,a)); f.write(b) } for (e = a = 0; e < c.length; e++) a += c[e].dataCount; if (b.getLengthInBits() > 8 * a) throw Error("code length overflow. (" + b.getLengthInBits() + ">" + 8 * a + ")"); for (b.getLengthInBits() + 4 <= 8 * a && b.put(0,4); 0 != b.getLengthInBits() % 8; ) b.putBit(!1); for (; !(b.getLengthInBits() >= 8 * a); ) { b.put(o.PAD0,8); if (b.getLengthInBits() >= 8 * a) break; b.put(o.PAD1,8) } return o.createBytes(b,c) }; o.createBytes = function (a,c) { for (var d = 0,e = 0,f = Array(c.length),i = Array(c.length),g = 0; g < c.length; g++) { var n = c[g].dataCount,h = c[g].totalCount - n,b = Math.max(b,n),e = Math.max(e,h); f[g] = Array(n); for (var k = 0; k < f[g].length; k++) f[g][k] = 255 & a.buffer[k + d]; d += n; k = j.getErrorCorrectPolynomial(h); n = (new q(f[g],k.getLength() - 1)).mod(k); i[g] = Array(k.getLength() - 1); for (k = 0; k < i[g].length; k++) h = k + n.getLength() - i[g].length,i[g][k] = 0 <= h ? n.get(h) : 0 } for (k = g = 0; k < c.length; k++) g += c[k].totalCount; d = Array(g); for (k = n = 0; k < b; k++) for (g = 0; g < c.length; g++) k < f[g].length && (d[n++] = f[g][k]); for (k = 0; k < e; k++) for (g = 0; g < c.length; g++) k < i[g].length && (d[n++] = i[g][k]); return d }; s = 4; for (var j = { PATTERN_POSITION_TABLE: [[],[6,18],22],26],30],34],22,38],24,42],26,46],28,50],30,54],32,58],34,62],46,66],48,70],50,74],54,78],56,82],58,86],62,90],72,94],74,98],78,102],80,106],84,110],86,114],90,118],98,122],102,126],52,104,130],82,108,134],60,112,138],114,142],118,146],126,150],76,128,154],106,132,158],110,136,162],138,166],142,170]],G15: 1335,G18: 7973,G15_MASK: 21522,getBCHTypeInfo: function (a) { for (var c = a << 10; 0 <= j.getBCHDigit(c) - j.getBCHDigit(j.G15); ) c ^= j.G15 << j.getBCHDigit(c) - j.getBCHDigit(j.G15); return (a << 10 | c) ^ j.G15_MASK },getBCHTypeNumber: function (a) { for (var c = a << 12; 0 <= j.getBCHDigit(c) - j.getBCHDigit(j.G18); ) c ^= j.G18 << j.getBCHDigit(c) - j.getBCHDigit(j.G18); return a << 12 | c },getBCHDigit: function (a) { for (var c = 0; 0 != a; ) c++,a >>>= 1; return c },getPatternPosition: function (a) { return j.PATTERN_POSITION_TABLE[a - 1] },getMask: function (a,d) { switch (a) { case 0: return 0 == (c + d) % 2; case 1: return 0 == c % 2; case 2: return 0 == d % 3; case 3: return 0 == (c + d) % 3; case 4: return 0 == (Math.floor(c / 2) + Math.floor(d / 3)) % 2; case 5: return 0 == c * d % 2 + c * d % 3; case 6: return 0 == (c * d % 2 + c * d % 3) % 2; case 7: return 0 == (c * d % 3 + (c + d) % 2) % 2; default: throw Error("bad maskPattern:" + a); } },getErrorCorrectPolynomial: function (a) { for (var c = new q([1],0),d = 0; d < a; d++) c = c.multiply(new q([1,l.gexp(d)],0)); return c },getLengthInBits: function (a,c) { if (1 <= c && 10 > c) switch (a) { case 1: return 10; case 2: return 9; case s: return 8; case 8: return 8; default: throw Error("mode:" + a); } else if (27 > c) switch (a) { case 1: return 12; case 2: return 11; case s: return 16; case 8: return 10; default: throw Error("mode:" + a); } else if (41 > c) switch (a) { case 1: return 14; case 2: return 13; case s: return 16; case 8: return 12; default: throw Error("mode:" + a); } else throw Error("type:" + c); },getLostPoint: function (a) { for (var c = a.getModuleCount(),d = 0,b = 0; b < c; b++) for (var e = 0; e < c; e++) { for (var f = 0,i = a.isDark(b,e),g = -1; 1 >= g; g++) if (!(0 > b + g || c <= b + g)) for (var h = -1; 1 >= h; h++) 0 > e + h || c <= e + h || 0 == g && 0 == h || i == a.isDark(b + g,e + h) && f++; 5 < f && (d += 3 + f - 5) } for (b = 0; b < c - 1; b++) for (e = 0; e < c - 1; e++) if (f = 0,a.isDark(b,e) && f++,a.isDark(b + 1,e + 1) && f++,0 == f || 4 == f) d += 3; for (b = 0; b < c; b++) for (e = 0; e < c - 6; e++) a.isDark(b,e) && !a.isDark(b,e + 1) && a.isDark(b,e + 2) && a.isDark(b,e + 3) && a.isDark(b,e + 4) && !a.isDark(b,e + 5) && a.isDark(b,e + 6) && (d += 40); for (e = 0; e < c; e++) for (b = 0; b < c - 6; b++) a.isDark(b,e) && !a.isDark(b + 1,e) && a.isDark(b + 2,e) && a.isDark(b + 3,e) && a.isDark(b + 4,e) && !a.isDark(b + 5,e) && a.isDark(b + 6,e) && (d += 40); for (e = f = 0; e < c; e++) for (b = 0; b < c; b++) a.isDark(b,e) && f++; a = Math.abs(100 * f / c / c - 50) / 5; return d + 10 * a } },l = { glog: function (a) { if (1 > a) throw Error("glog(" + a + ")"); return l.LOG_TABLE[a] },gexp: function (a) { for (; 0 > a; ) a += 255; for (; 256 <= a; ) a -= 255; return l.EXP_TABLE[a] },EXP_TABLE: Array(256),LOG_TABLE: Array(256) },m = 0; 8 > m; m++) l.EXP_TABLE[m] = 1 << m; for (m = 8; 256 > m; m++) l.EXP_TABLE[m] = l.EXP_TABLE[m - 4] ^ l.EXP_TABLE[m - 5] ^ l.EXP_TABLE[m - 6] ^ l.EXP_TABLE[m - 8]; for (m = 0; 255 > m; m++) l.LOG_TABLE[l.EXP_TABLE[m]] = m; q.prototype = { get: function (a) { return this.num[a] },getLength: function () { return this.num.length },multiply: function (a) { for (var c = Array(this.getLength() + a.getLength() - 1),d = 0; d < this.getLength(); d++) for (var b = 0; b < a.getLength(); b++) c[d + b] ^= l.gexp(l.glog(this.get(d)) + l.glog(a.get(b))); return new q(c,0) },mod: function (a) { if (0 > this.getLength() - a.getLength()) return this; for (var c = l.glog(this.get(0)) - l.glog(a.get(0)),d = Array(this.getLength()),b = 0; b < this.getLength(); b++) d[b] = this.get(b); for (b = 0; b < a.getLength(); b++) d[b] ^= l.gexp(l.glog(a.get(b)) + c); return (new q(d,0)).mod(a) } }; p.RS_BLOCK_TABLE = [[1,19],[1,16],13],9],44,28],70,55],44],[2,35,17],100,80],32],24],[4,25,134,108],67,43],33,15,2,11,12],68],43,27],15],49,31],14,4,39,13,1,40,14],121,97],38,61,39],18,41,146,116],[3,36,59,37],16,37,12,68,87,69],69,19,20],101,81],81,51],51,23],8,116,92,117,93],20,6,47,21],[7,42,133,107],[8,45,[12,145,115,64,5,65,41],[11,[5,109,88],66,7,55,25],122,123,99],73,3,[15,135,107,[10,75,47],17,150,120,151,121],[9,[17,141,113,71,45],21,10,144,117],[19,139,111,140,112],[34,152,48],[16,147,148,[30,[22,143,115],[28,[33,153,123],53,23,31,[21,[23,[13,29,[42,[14,[29,[44,[59,[39,[46,[49,[24,[48,[20,[40,[43,149,119],[18,16]]; p.getRSBlocks = function (a,c) { var d = p.getRsBlockTable(a,c); if (void 0 == d) throw Error("bad rs block @ typeNumber:" + a + "/errorCorrectLevel:" + c); for (var b = d.length / 3,e = [],f = 0; f < b; f++) for (var h = d[3 * f + 0],g = d[3 * f + 1],j = d[3 * f + 2],l = 0; l < h; l++) e.push(new p(g,j)); return e }; p.getRsBlockTable = function (a,c) { switch (c) { case 1: return p.RS_BLOCK_TABLE[4 * (a - 1) + 0]; case 0: return p.RS_BLOCK_TABLE[4 * (a - 1) + 1]; case 3: return p.RS_BLOCK_TABLE[4 * (a - 1) + 2]; case 2: return p.RS_BLOCK_TABLE[4 * (a - 1) + 3] } }; t.prototype = { get: function (a) { return 1 == (this.buffer[Math.floor(a / 8)] >>> 7 - a % 8 & 1) },put: function (a,c) { for (var d = 0; d < c; d++) this.putBit(1 == (a >>> c - d - 1 & 1)) },getLengthInBits: function () { return this.length },putBit: function (a) { var c = Math.floor(this.length / 8); this.buffer.length <= c && this.buffer.push(0); a && (this.buffer[c] |= 128 >>> this.length % 8); this.length++ } }; "string" === typeof h && (h = { text: h }); h = r.extend({},{ render: "canvas",width: 256,height: 256,typeNumber: -1,correctLevel: 2,background: "#ffffff",foreground: "#000000" },h); return this.each(function () { var a; if ("canvas" == h.render) { a = new o(h.typeNumber,h.correctLevel); a.addData(h.text); a.make(); var c = document.createElement("canvas"); c.width = h.width; c.height = h.height; for (var d = c.getContext("2d"),b = h.width / a.getModuleCount(),e = h.height / a.getModuleCount(),f = 0; f < a.getModuleCount(); f++) for (var i = 0; i < a.getModuleCount(); i++) { d.fillStyle = a.isDark(f,i) ? h.foreground : h.background; var g = Math.ceil((i + 1) * b) - Math.floor(i * b),j = Math.ceil((f + 1) * b) - Math.floor(f * b); d.fillRect(Math.round(i * b),Math.round(f * e),g,j) } } else { a = new o(h.typeNumber,h.correctLevel); a.addData(h.text); a.make(); c = r("

二、HTML代码,与之前的不同,修改了一些代码

(二维码)

<div id="Qrcode">


<button class="button button-positive"
ng-click="saveImageQrcode()">保存到手机

三、JS代码,这里需要将信息转化一下utf16转化为utf8格式,才能实现转化为二维码,我是将一个对象转化的:

= 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; } //{name: currentUser.UserName,id: currentUser.Id}这个是我需要转化的对象,也是我在扫描二维码的时候需要解析出来的内容 $(function () { jQuery('#Qrcode').qrcode(utf16to8(angular.toJson({name: currentUser.UserName,id: currentUser.Id}))) });

至于怎么将二维码保存到手机,我之前的文章中有讲到的,如果想实现的话可以参考一下~

好了,具体实现的步骤就是这样了,大家可以提意见或者参考一下!

猜你在找的jQuery相关文章