jquery.qrcode.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. function QR8bitByte(a) {
  2. this.mode = QRMode.MODE_8BIT_BYTE,
  3. this.data = a
  4. }
  5. function QRCode(a, b) {
  6. this.typeNumber = a,
  7. this.errorCorrectLevel = b,
  8. this.modules = null,
  9. this.moduleCount = 0,
  10. this.dataCache = null,
  11. this.dataList = new Array()
  12. }
  13. function QRPolynomial(a, b) {
  14. var c, d;
  15. if (void 0 == a.length) {
  16. throw new Error(a.length + "/" + b)
  17. }
  18. for (c = 0; c < a.length && 0 == a[c]; ) {
  19. c++
  20. }
  21. for (this.num = new Array(a.length - c + b),
  22. d = 0; d < a.length - c; d++) {
  23. this.num[d] = a[d + c]
  24. }
  25. }
  26. function QRRSBlock(a, b) {
  27. this.totalCount = a,
  28. this.dataCount = b
  29. }
  30. function QRBitBuffer() {
  31. this.buffer = new Array(),
  32. this.length = 0
  33. }
  34. var QRMode, QRErrorCorrectLevel, QRMaskPattern, QRUtil, QRMath, i;
  35. for (function(a) {
  36. a.fn.qrcode = function(b) {
  37. var c, d;
  38. return "string" == typeof b && (b = {
  39. text: b
  40. }),
  41. b = a.extend({}, {
  42. render: "canvas",
  43. width: 256,
  44. height: 256,
  45. //这里是图片的高度和宽度
  46. /*imgWidth: b.width / 4.7,
  47. imgHeight: b.height / 4,*/
  48. imgWidth: b.width / 3.857,
  49. imgHeight: b.height / 3.857,
  50. typeNumber: -1,
  51. correctLevel: QRErrorCorrectLevel.H,
  52. background: "#ffffff",
  53. foreground: "#000000"
  54. }, b),
  55. c = function() {
  56. var c, d, e, f, g, h, i, j, k, a = new QRCode(b.typeNumber,b.correctLevel);
  57. for (a.addData(utf16to8(b.text)),
  58. a.make(),
  59. c = document.createElement("canvas"),
  60. c.width = b.width,
  61. c.height = b.height,
  62. d = c.getContext("2d"),
  63. b.src && (e = new Image(),
  64. e.src = b.src,
  65. e.onload = function() {
  66. d.drawImage(e, (b.width - b.imgWidth) / 2, (b.height - b.imgHeight) / 2, b.imgWidth, b.imgHeight)
  67. }
  68. ),
  69. f = b.width / a.getModuleCount(),
  70. g = b.height / a.getModuleCount(),
  71. h = 0; h < a.getModuleCount(); h++) {
  72. for (i = 0; i < a.getModuleCount(); i++) {
  73. d.fillStyle = a.isDark(h, i) ? b.foreground : b.background,
  74. j = Math.ceil((i + 1) * f) - Math.floor(i * f),
  75. k = Math.ceil((h + 1) * f) - Math.floor(h * f),
  76. d.fillRect(Math.round(i * f), Math.round(h * g), j, k)
  77. }
  78. }
  79. return c
  80. }
  81. ,
  82. d = function() {
  83. var d, e, f, g, h, i, c = new QRCode(b.typeNumber,b.correctLevel);
  84. //这里的utf16to8(b.text)是对Text中的字符串进行转码,让其支持中文
  85. for (c.addData(utf16to8(b.text)),
  86. c.make(),
  87. d = a("<table></table>").css("width", b.width + "px").css("height", b.height + "px").css("border", "0px").css("border-collapse", "collapse").css("background-color", b.background),
  88. e = b.width / c.getModuleCount(),
  89. f = b.height / c.getModuleCount(),
  90. g = 0; g < c.getModuleCount(); g++) {
  91. for (h = a("<tr></tr>").css("height", f + "px").appendTo(d),
  92. i = 0; i < c.getModuleCount(); i++) {
  93. a("<td></td>").css("width", e + "px").css("background-color", c.isDark(g, i) ? b.foreground : b.background).appendTo(h)
  94. }
  95. }
  96. return d
  97. }
  98. ,
  99. this.each(function() {
  100. var e = "canvas" == b.render ? c() : d();
  101. a(e).appendTo(this)
  102. })
  103. }
  104. }(jQuery),
  105. QR8bitByte.prototype = {
  106. getLength: function() {
  107. return this.data.length
  108. },
  109. write: function(a) {
  110. for (var b = 0; b < this.data.length; b++) {
  111. a.put(this.data.charCodeAt(b), 8)
  112. }
  113. }
  114. },
  115. QRCode.prototype = {
  116. addData: function(a) {
  117. var b = new QR8bitByte(a);
  118. this.dataList.push(b),
  119. this.dataCache = null
  120. },
  121. isDark: function(a, b) {
  122. if (0 > a || this.moduleCount <= a || 0 > b || this.moduleCount <= b) {
  123. throw new Error(a + "," + b)
  124. }
  125. return this.modules[a][b]
  126. },
  127. getModuleCount: function() {
  128. return this.moduleCount
  129. },
  130. make: function() {
  131. var a, b, c, d, e, f;
  132. if (this.typeNumber < 1) {
  133. for (a = 1,
  134. a = 1; 40 > a; a++) {
  135. for (b = QRRSBlock.getRSBlocks(a, this.errorCorrectLevel),
  136. c = new QRBitBuffer(),
  137. d = 0,
  138. e = 0; e < b.length; e++) {
  139. d += b[e].dataCount
  140. }
  141. for (e = 0; e < this.dataList.length; e++) {
  142. f = this.dataList[e],
  143. c.put(f.mode, 4),
  144. c.put(f.getLength(), QRUtil.getLengthInBits(f.mode, a)),
  145. f.write(c)
  146. }
  147. if (c.getLengthInBits() <= 8 * d) {
  148. break
  149. }
  150. }
  151. this.typeNumber = a
  152. }
  153. this.makeImpl(!1, this.getBestMaskPattern())
  154. },
  155. makeImpl: function(a, b) {
  156. var c, d;
  157. for (this.moduleCount = 4 * this.typeNumber + 17,
  158. this.modules = new Array(this.moduleCount),
  159. c = 0; c < this.moduleCount; c++) {
  160. for (this.modules[c] = new Array(this.moduleCount),
  161. d = 0; d < this.moduleCount; d++) {
  162. this.modules[c][d] = null
  163. }
  164. }
  165. this.setupPositionProbePattern(0, 0),
  166. this.setupPositionProbePattern(this.moduleCount - 7, 0),
  167. this.setupPositionProbePattern(0, this.moduleCount - 7),
  168. this.setupPositionAdjustPattern(),
  169. this.setupTimingPattern(),
  170. this.setupTypeInfo(a, b),
  171. this.typeNumber >= 7 && this.setupTypeNumber(a),
  172. null == this.dataCache && (this.dataCache = QRCode.createData(this.typeNumber, this.errorCorrectLevel, this.dataList)),
  173. this.mapData(this.dataCache, b)
  174. },
  175. setupPositionProbePattern: function(a, b) {
  176. var c, d;
  177. for (c = -1; 7 >= c; c++) {
  178. if (!(-1 >= a + c || this.moduleCount <= a + c)) {
  179. for (d = -1; 7 >= d; d++) {
  180. -1 >= b + d || this.moduleCount <= b + d || (this.modules[a + c][b + d] = c >= 0 && 6 >= c && (0 == d || 6 == d) || d >= 0 && 6 >= d && (0 == c || 6 == c) || c >= 2 && 4 >= c && d >= 2 && 4 >= d ? !0 : !1)
  181. }
  182. }
  183. }
  184. },
  185. getBestMaskPattern: function() {
  186. var c, d, a = 0, b = 0;
  187. for (c = 0; 8 > c; c++) {
  188. this.makeImpl(!0, c),
  189. d = QRUtil.getLostPoint(this),
  190. (0 == c || a > d) && (a = d,
  191. b = c)
  192. }
  193. return b
  194. },
  195. createMovieClip: function(a, b, c) {
  196. var f, g, h, i, j, d = a.createEmptyMovieClip(b, c), e = 1;
  197. for (this.make(),
  198. f = 0; f < this.modules.length; f++) {
  199. for (g = f * e,
  200. h = 0; h < this.modules[f].length; h++) {
  201. i = h * e,
  202. j = this.modules[f][h],
  203. j && (d.beginFill(0, 100),
  204. d.moveTo(i, g),
  205. d.lineTo(i + e, g),
  206. d.lineTo(i + e, g + e),
  207. d.lineTo(i, g + e),
  208. d.endFill())
  209. }
  210. }
  211. return d
  212. },
  213. setupTimingPattern: function() {
  214. var a, b;
  215. for (a = 8; a < this.moduleCount - 8; a++) {
  216. null == this.modules[a][6] && (this.modules[a][6] = 0 == a % 2)
  217. }
  218. for (b = 8; b < this.moduleCount - 8; b++) {
  219. null == this.modules[6][b] && (this.modules[6][b] = 0 == b % 2)
  220. }
  221. },
  222. setupPositionAdjustPattern: function() {
  223. var b, c, d, e, f, g, a = QRUtil.getPatternPosition(this.typeNumber);
  224. for (b = 0; b < a.length; b++) {
  225. for (c = 0; c < a.length; c++) {
  226. if (d = a[b],
  227. e = a[c],
  228. null == this.modules[d][e]) {
  229. for (f = -2; 2 >= f; f++) {
  230. for (g = -2; 2 >= g; g++) {
  231. this.modules[d + f][e + g] = -2 == f || 2 == f || -2 == g || 2 == g || 0 == f && 0 == g ? !0 : !1
  232. }
  233. }
  234. }
  235. }
  236. }
  237. },
  238. setupTypeNumber: function(a) {
  239. var c, d, b = QRUtil.getBCHTypeNumber(this.typeNumber);
  240. for (c = 0; 18 > c; c++) {
  241. d = !a && 1 == (1 & b >> c),
  242. this.modules[Math.floor(c / 3)][c % 3 + this.moduleCount - 8 - 3] = d
  243. }
  244. for (c = 0; 18 > c; c++) {
  245. d = !a && 1 == (1 & b >> c),
  246. this.modules[c % 3 + this.moduleCount - 8 - 3][Math.floor(c / 3)] = d
  247. }
  248. },
  249. setupTypeInfo: function(a, b) {
  250. var e, f, c = this.errorCorrectLevel << 3 | b, d = QRUtil.getBCHTypeInfo(c);
  251. for (e = 0; 15 > e; e++) {
  252. f = !a && 1 == (1 & d >> e),
  253. 6 > e ? this.modules[e][8] = f : 8 > e ? this.modules[e + 1][8] = f : this.modules[this.moduleCount - 15 + e][8] = f
  254. }
  255. for (e = 0; 15 > e; e++) {
  256. f = !a && 1 == (1 & d >> e),
  257. 8 > e ? this.modules[8][this.moduleCount - e - 1] = f : 9 > e ? this.modules[8][15 - e - 1 + 1] = f : this.modules[8][15 - e - 1] = f
  258. }
  259. this.modules[this.moduleCount - 8][8] = !a
  260. },
  261. mapData: function(a, b) {
  262. var g, h, i, j, c = -1, d = this.moduleCount - 1, e = 7, f = 0;
  263. for (g = this.moduleCount - 1; g > 0; g -= 2) {
  264. for (6 == g && g--; ; ) {
  265. for (h = 0; 2 > h; h++) {
  266. null == this.modules[d][g - h] && (i = !1,
  267. f < a.length && (i = 1 == (1 & a[f] >>> e)),
  268. j = QRUtil.getMask(b, d, g - h),
  269. j && (i = !i),
  270. this.modules[d][g - h] = i,
  271. e--,
  272. -1 == e && (f++,
  273. e = 7))
  274. }
  275. if (d += c,
  276. 0 > d || this.moduleCount <= d) {
  277. d -= c,
  278. c = -c;
  279. break
  280. }
  281. }
  282. }
  283. }
  284. },
  285. QRCode.PAD0 = 236,
  286. QRCode.PAD1 = 17,
  287. QRCode.createData = function(a, b, c) {
  288. var f, g, h, d = QRRSBlock.getRSBlocks(a, b), e = new QRBitBuffer();
  289. for (f = 0; f < c.length; f++) {
  290. g = c[f],
  291. e.put(g.mode, 4),
  292. e.put(g.getLength(), QRUtil.getLengthInBits(g.mode, a)),
  293. g.write(e)
  294. }
  295. for (h = 0,
  296. f = 0; f < d.length; f++) {
  297. h += d[f].dataCount
  298. }
  299. if (e.getLengthInBits() > 8 * h) {
  300. throw new Error("code length overflow. (" + e.getLengthInBits() + ">" + 8 * h + ")")
  301. }
  302. for (e.getLengthInBits() + 4 <= 8 * h && e.put(0, 4); 0 != e.getLengthInBits() % 8; ) {
  303. e.putBit(!1)
  304. }
  305. for (; ; ) {
  306. if (e.getLengthInBits() >= 8 * h) {
  307. break
  308. }
  309. if (e.put(QRCode.PAD0, 8),
  310. e.getLengthInBits() >= 8 * h) {
  311. break
  312. }
  313. e.put(QRCode.PAD1, 8)
  314. }
  315. return QRCode.createBytes(e, d)
  316. }
  317. ,
  318. QRCode.createBytes = function(a, b) {
  319. var h, i, j, k, l, m, n, o, p, q, r, c = 0, d = 0, e = 0, f = new Array(b.length), g = new Array(b.length);
  320. for (h = 0; h < b.length; h++) {
  321. for (i = b[h].dataCount,
  322. j = b[h].totalCount - i,
  323. d = Math.max(d, i),
  324. e = Math.max(e, j),
  325. f[h] = new Array(i),
  326. k = 0; k < f[h].length; k++) {
  327. f[h][k] = 255 & a.buffer[k + c]
  328. }
  329. for (c += i,
  330. l = QRUtil.getErrorCorrectPolynomial(j),
  331. m = new QRPolynomial(f[h],l.getLength() - 1),
  332. n = m.mod(l),
  333. g[h] = new Array(l.getLength() - 1),
  334. k = 0; k < g[h].length; k++) {
  335. o = k + n.getLength() - g[h].length,
  336. g[h][k] = o >= 0 ? n.get(o) : 0
  337. }
  338. }
  339. for (p = 0,
  340. k = 0; k < b.length; k++) {
  341. p += b[k].totalCount
  342. }
  343. for (q = new Array(p),
  344. r = 0,
  345. k = 0; d > k; k++) {
  346. for (h = 0; h < b.length; h++) {
  347. k < f[h].length && (q[r++] = f[h][k])
  348. }
  349. }
  350. for (k = 0; e > k; k++) {
  351. for (h = 0; h < b.length; h++) {
  352. k < g[h].length && (q[r++] = g[h][k])
  353. }
  354. }
  355. return q
  356. }
  357. ,
  358. QRMode = {
  359. MODE_NUMBER: 1,
  360. MODE_ALPHA_NUM: 2,
  361. MODE_8BIT_BYTE: 4,
  362. MODE_KANJI: 8
  363. },
  364. QRErrorCorrectLevel = {
  365. L: 1,
  366. M: 0,
  367. Q: 3,
  368. H: 2
  369. },
  370. QRMaskPattern = {
  371. PATTERN000: 0,
  372. PATTERN001: 1,
  373. PATTERN010: 2,
  374. PATTERN011: 3,
  375. PATTERN100: 4,
  376. PATTERN101: 5,
  377. PATTERN110: 6,
  378. PATTERN111: 7
  379. },
  380. QRUtil = {
  381. PATTERN_POSITION_TABLE: [[], [6, 18], [6, 22], [6, 26], [6, 30], [6, 34], [6, 22, 38], [6, 24, 42], [6, 26, 46], [6, 28, 50], [6, 30, 54], [6, 32, 58], [6, 34, 62], [6, 26, 46, 66], [6, 26, 48, 70], [6, 26, 50, 74], [6, 30, 54, 78], [6, 30, 56, 82], [6, 30, 58, 86], [6, 34, 62, 90], [6, 28, 50, 72, 94], [6, 26, 50, 74, 98], [6, 30, 54, 78, 102], [6, 28, 54, 80, 106], [6, 32, 58, 84, 110], [6, 30, 58, 86, 114], [6, 34, 62, 90, 118], [6, 26, 50, 74, 98, 122], [6, 30, 54, 78, 102, 126], [6, 26, 52, 78, 104, 130], [6, 30, 56, 82, 108, 134], [6, 34, 60, 86, 112, 138], [6, 30, 58, 86, 114, 142], [6, 34, 62, 90, 118, 146], [6, 30, 54, 78, 102, 126, 150], [6, 24, 50, 76, 102, 128, 154], [6, 28, 54, 80, 106, 132, 158], [6, 32, 58, 84, 110, 136, 162], [6, 26, 54, 82, 110, 138, 166], [6, 30, 58, 86, 114, 142, 170]],
  382. G15: 1335,
  383. G18: 7973,
  384. G15_MASK: 21522,
  385. getBCHTypeInfo: function(a) {
  386. for (var b = a << 10; QRUtil.getBCHDigit(b) - QRUtil.getBCHDigit(QRUtil.G15) >= 0; ) {
  387. b ^= QRUtil.G15 << QRUtil.getBCHDigit(b) - QRUtil.getBCHDigit(QRUtil.G15)
  388. }
  389. return (a << 10 | b) ^ QRUtil.G15_MASK
  390. },
  391. getBCHTypeNumber: function(a) {
  392. for (var b = a << 12; QRUtil.getBCHDigit(b) - QRUtil.getBCHDigit(QRUtil.G18) >= 0; ) {
  393. b ^= QRUtil.G18 << QRUtil.getBCHDigit(b) - QRUtil.getBCHDigit(QRUtil.G18)
  394. }
  395. return a << 12 | b
  396. },
  397. getBCHDigit: function(a) {
  398. for (var b = 0; 0 != a; ) {
  399. b++,
  400. a >>>= 1
  401. }
  402. return b
  403. },
  404. getPatternPosition: function(a) {
  405. return QRUtil.PATTERN_POSITION_TABLE[a - 1]
  406. },
  407. getMask: function(a, b, c) {
  408. switch (a) {
  409. case QRMaskPattern.PATTERN000:
  410. return 0 == (b + c) % 2;
  411. case QRMaskPattern.PATTERN001:
  412. return 0 == b % 2;
  413. case QRMaskPattern.PATTERN010:
  414. return 0 == c % 3;
  415. case QRMaskPattern.PATTERN011:
  416. return 0 == (b + c) % 3;
  417. case QRMaskPattern.PATTERN100:
  418. return 0 == (Math.floor(b / 2) + Math.floor(c / 3)) % 2;
  419. case QRMaskPattern.PATTERN101:
  420. return 0 == b * c % 2 + b * c % 3;
  421. case QRMaskPattern.PATTERN110:
  422. return 0 == (b * c % 2 + b * c % 3) % 2;
  423. case QRMaskPattern.PATTERN111:
  424. return 0 == (b * c % 3 + (b + c) % 2) % 2;
  425. default:
  426. throw new Error("bad maskPattern:" + a)
  427. }
  428. },
  429. getErrorCorrectPolynomial: function(a) {
  430. var c, b = new QRPolynomial([1],0);
  431. for (c = 0; a > c; c++) {
  432. b = b.multiply(new QRPolynomial([1, QRMath.gexp(c)],0))
  433. }
  434. return b
  435. },
  436. getLengthInBits: function(a, b) {
  437. if (b >= 1 && 10 > b) {
  438. switch (a) {
  439. case QRMode.MODE_NUMBER:
  440. return 10;
  441. case QRMode.MODE_ALPHA_NUM:
  442. return 9;
  443. case QRMode.MODE_8BIT_BYTE:
  444. return 8;
  445. case QRMode.MODE_KANJI:
  446. return 8;
  447. default:
  448. throw new Error("mode:" + a)
  449. }
  450. } else {
  451. if (27 > b) {
  452. switch (a) {
  453. case QRMode.MODE_NUMBER:
  454. return 12;
  455. case QRMode.MODE_ALPHA_NUM:
  456. return 11;
  457. case QRMode.MODE_8BIT_BYTE:
  458. return 16;
  459. case QRMode.MODE_KANJI:
  460. return 10;
  461. default:
  462. throw new Error("mode:" + a)
  463. }
  464. } else {
  465. if (!(41 > b)) {
  466. throw new Error("type:" + b)
  467. }
  468. switch (a) {
  469. case QRMode.MODE_NUMBER:
  470. return 14;
  471. case QRMode.MODE_ALPHA_NUM:
  472. return 13;
  473. case QRMode.MODE_8BIT_BYTE:
  474. return 16;
  475. case QRMode.MODE_KANJI:
  476. return 12;
  477. default:
  478. throw new Error("mode:" + a)
  479. }
  480. }
  481. }
  482. },
  483. getLostPoint: function(a) {
  484. var d, e, f, g, h, i, j, k, l, b = a.getModuleCount(), c = 0;
  485. for (d = 0; b > d; d++) {
  486. for (e = 0; b > e; e++) {
  487. for (f = 0,
  488. g = a.isDark(d, e),
  489. h = -1; 1 >= h; h++) {
  490. if (!(0 > d + h || d + h >= b)) {
  491. for (i = -1; 1 >= i; i++) {
  492. 0 > e + i || e + i >= b || (0 != h || 0 != i) && g == a.isDark(d + h, e + i) && f++
  493. }
  494. }
  495. }
  496. f > 5 && (c += 3 + f - 5)
  497. }
  498. }
  499. for (d = 0; b - 1 > d; d++) {
  500. for (e = 0; b - 1 > e; e++) {
  501. j = 0,
  502. a.isDark(d, e) && j++,
  503. a.isDark(d + 1, e) && j++,
  504. a.isDark(d, e + 1) && j++,
  505. a.isDark(d + 1, e + 1) && j++,
  506. (0 == j || 4 == j) && (c += 3)
  507. }
  508. }
  509. for (d = 0; b > d; d++) {
  510. for (e = 0; b - 6 > e; e++) {
  511. a.isDark(d, e) && !a.isDark(d, e + 1) && a.isDark(d, e + 2) && a.isDark(d, e + 3) && a.isDark(d, e + 4) && !a.isDark(d, e + 5) && a.isDark(d, e + 6) && (c += 40)
  512. }
  513. }
  514. for (e = 0; b > e; e++) {
  515. for (d = 0; b - 6 > d; d++) {
  516. a.isDark(d, e) && !a.isDark(d + 1, e) && a.isDark(d + 2, e) && a.isDark(d + 3, e) && a.isDark(d + 4, e) && !a.isDark(d + 5, e) && a.isDark(d + 6, e) && (c += 40)
  517. }
  518. }
  519. for (k = 0,
  520. e = 0; b > e; e++) {
  521. for (d = 0; b > d; d++) {
  522. a.isDark(d, e) && k++
  523. }
  524. }
  525. return l = Math.abs(100 * k / b / b - 50) / 5,
  526. c += 10 * l
  527. }
  528. },
  529. QRMath = {
  530. glog: function(a) {
  531. if (1 > a) {
  532. throw new Error("glog(" + a + ")")
  533. }
  534. return QRMath.LOG_TABLE[a]
  535. },
  536. gexp: function(a) {
  537. for (; 0 > a; ) {
  538. a += 255
  539. }
  540. for (; a >= 256; ) {
  541. a -= 255
  542. }
  543. return QRMath.EXP_TABLE[a]
  544. },
  545. EXP_TABLE: new Array(256),
  546. LOG_TABLE: new Array(256)
  547. },
  548. i = 0; 8 > i; i++) {
  549. QRMath.EXP_TABLE[i] = 1 << i
  550. }
  551. for (i = 8; 256 > i; i++) {
  552. QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath.EXP_TABLE[i - 8]
  553. }
  554. for (i = 0; 255 > i; i++) {
  555. QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i
  556. }
  557. QRPolynomial.prototype = {
  558. get: function(a) {
  559. return this.num[a]
  560. },
  561. getLength: function() {
  562. return this.num.length
  563. },
  564. multiply: function(a) {
  565. var c, d, b = new Array(this.getLength() + a.getLength() - 1);
  566. for (c = 0; c < this.getLength(); c++) {
  567. for (d = 0; d < a.getLength(); d++) {
  568. b[c + d] ^= QRMath.gexp(QRMath.glog(this.get(c)) + QRMath.glog(a.get(d)))
  569. }
  570. }
  571. return new QRPolynomial(b,0)
  572. },
  573. mod: function(a) {
  574. var b, c, d;
  575. if (this.getLength() - a.getLength() < 0) {
  576. return this
  577. }
  578. for (b = QRMath.glog(this.get(0)) - QRMath.glog(a.get(0)),
  579. c = new Array(this.getLength()),
  580. d = 0; d < this.getLength(); d++) {
  581. c[d] = this.get(d)
  582. }
  583. for (d = 0; d < a.getLength(); d++) {
  584. c[d] ^= QRMath.gexp(QRMath.glog(a.get(d)) + b)
  585. }
  586. return new QRPolynomial(c,0).mod(a)
  587. }
  588. },
  589. QRRSBlock.RS_BLOCK_TABLE = [[1, 26, 19], [1, 26, 16], [1, 26, 13], [1, 26, 9], [1, 44, 34], [1, 44, 28], [1, 44, 22], [1, 44, 16], [1, 70, 55], [1, 70, 44], [2, 35, 17], [2, 35, 13], [1, 100, 80], [2, 50, 32], [2, 50, 24], [4, 25, 9], [1, 134, 108], [2, 67, 43], [2, 33, 15, 2, 34, 16], [2, 33, 11, 2, 34, 12], [2, 86, 68], [4, 43, 27], [4, 43, 19], [4, 43, 15], [2, 98, 78], [4, 49, 31], [2, 32, 14, 4, 33, 15], [4, 39, 13, 1, 40, 14], [2, 121, 97], [2, 60, 38, 2, 61, 39], [4, 40, 18, 2, 41, 19], [4, 40, 14, 2, 41, 15], [2, 146, 116], [3, 58, 36, 2, 59, 37], [4, 36, 16, 4, 37, 17], [4, 36, 12, 4, 37, 13], [2, 86, 68, 2, 87, 69], [4, 69, 43, 1, 70, 44], [6, 43, 19, 2, 44, 20], [6, 43, 15, 2, 44, 16], [4, 101, 81], [1, 80, 50, 4, 81, 51], [4, 50, 22, 4, 51, 23], [3, 36, 12, 8, 37, 13], [2, 116, 92, 2, 117, 93], [6, 58, 36, 2, 59, 37], [4, 46, 20, 6, 47, 21], [7, 42, 14, 4, 43, 15], [4, 133, 107], [8, 59, 37, 1, 60, 38], [8, 44, 20, 4, 45, 21], [12, 33, 11, 4, 34, 12], [3, 145, 115, 1, 146, 116], [4, 64, 40, 5, 65, 41], [11, 36, 16, 5, 37, 17], [11, 36, 12, 5, 37, 13], [5, 109, 87, 1, 110, 88], [5, 65, 41, 5, 66, 42], [5, 54, 24, 7, 55, 25], [11, 36, 12], [5, 122, 98, 1, 123, 99], [7, 73, 45, 3, 74, 46], [15, 43, 19, 2, 44, 20], [3, 45, 15, 13, 46, 16], [1, 135, 107, 5, 136, 108], [10, 74, 46, 1, 75, 47], [1, 50, 22, 15, 51, 23], [2, 42, 14, 17, 43, 15], [5, 150, 120, 1, 151, 121], [9, 69, 43, 4, 70, 44], [17, 50, 22, 1, 51, 23], [2, 42, 14, 19, 43, 15], [3, 141, 113, 4, 142, 114], [3, 70, 44, 11, 71, 45], [17, 47, 21, 4, 48, 22], [9, 39, 13, 16, 40, 14], [3, 135, 107, 5, 136, 108], [3, 67, 41, 13, 68, 42], [15, 54, 24, 5, 55, 25], [15, 43, 15, 10, 44, 16], [4, 144, 116, 4, 145, 117], [17, 68, 42], [17, 50, 22, 6, 51, 23], [19, 46, 16, 6, 47, 17], [2, 139, 111, 7, 140, 112], [17, 74, 46], [7, 54, 24, 16, 55, 25], [34, 37, 13], [4, 151, 121, 5, 152, 122], [4, 75, 47, 14, 76, 48], [11, 54, 24, 14, 55, 25], [16, 45, 15, 14, 46, 16], [6, 147, 117, 4, 148, 118], [6, 73, 45, 14, 74, 46], [11, 54, 24, 16, 55, 25], [30, 46, 16, 2, 47, 17], [8, 132, 106, 4, 133, 107], [8, 75, 47, 13, 76, 48], [7, 54, 24, 22, 55, 25], [22, 45, 15, 13, 46, 16], [10, 142, 114, 2, 143, 115], [19, 74, 46, 4, 75, 47], [28, 50, 22, 6, 51, 23], [33, 46, 16, 4, 47, 17], [8, 152, 122, 4, 153, 123], [22, 73, 45, 3, 74, 46], [8, 53, 23, 26, 54, 24], [12, 45, 15, 28, 46, 16], [3, 147, 117, 10, 148, 118], [3, 73, 45, 23, 74, 46], [4, 54, 24, 31, 55, 25], [11, 45, 15, 31, 46, 16], [7, 146, 116, 7, 147, 117], [21, 73, 45, 7, 74, 46], [1, 53, 23, 37, 54, 24], [19, 45, 15, 26, 46, 16], [5, 145, 115, 10, 146, 116], [19, 75, 47, 10, 76, 48], [15, 54, 24, 25, 55, 25], [23, 45, 15, 25, 46, 16], [13, 145, 115, 3, 146, 116], [2, 74, 46, 29, 75, 47], [42, 54, 24, 1, 55, 25], [23, 45, 15, 28, 46, 16], [17, 145, 115], [10, 74, 46, 23, 75, 47], [10, 54, 24, 35, 55, 25], [19, 45, 15, 35, 46, 16], [17, 145, 115, 1, 146, 116], [14, 74, 46, 21, 75, 47], [29, 54, 24, 19, 55, 25], [11, 45, 15, 46, 46, 16], [13, 145, 115, 6, 146, 116], [14, 74, 46, 23, 75, 47], [44, 54, 24, 7, 55, 25], [59, 46, 16, 1, 47, 17], [12, 151, 121, 7, 152, 122], [12, 75, 47, 26, 76, 48], [39, 54, 24, 14, 55, 25], [22, 45, 15, 41, 46, 16], [6, 151, 121, 14, 152, 122], [6, 75, 47, 34, 76, 48], [46, 54, 24, 10, 55, 25], [2, 45, 15, 64, 46, 16], [17, 152, 122, 4, 153, 123], [29, 74, 46, 14, 75, 47], [49, 54, 24, 10, 55, 25], [24, 45, 15, 46, 46, 16], [4, 152, 122, 18, 153, 123], [13, 74, 46, 32, 75, 47], [48, 54, 24, 14, 55, 25], [42, 45, 15, 32, 46, 16], [20, 147, 117, 4, 148, 118], [40, 75, 47, 7, 76, 48], [43, 54, 24, 22, 55, 25], [10, 45, 15, 67, 46, 16], [19, 148, 118, 6, 149, 119], [18, 75, 47, 31, 76, 48], [34, 54, 24, 34, 55, 25], [20, 45, 15, 61, 46, 16]],
  590. QRRSBlock.getRSBlocks = function(a, b) {
  591. var d, e, f, g, h, i, j, c = QRRSBlock.getRsBlockTable(a, b);
  592. if (void 0 == c) {
  593. throw new Error("bad rs block @ typeNumber:" + a + "/errorCorrectLevel:" + b)
  594. }
  595. for (d = c.length / 3,
  596. e = new Array(),
  597. f = 0; d > f; f++) {
  598. for (g = c[3 * f + 0],
  599. h = c[3 * f + 1],
  600. i = c[3 * f + 2],
  601. j = 0; g > j; j++) {
  602. e.push(new QRRSBlock(h,i))
  603. }
  604. }
  605. return e
  606. }
  607. ,
  608. QRRSBlock.getRsBlockTable = function(a, b) {
  609. switch (b) {
  610. case QRErrorCorrectLevel.L:
  611. return QRRSBlock.RS_BLOCK_TABLE[4 * (a - 1) + 0];
  612. case QRErrorCorrectLevel.M:
  613. return QRRSBlock.RS_BLOCK_TABLE[4 * (a - 1) + 1];
  614. case QRErrorCorrectLevel.Q:
  615. return QRRSBlock.RS_BLOCK_TABLE[4 * (a - 1) + 2];
  616. case QRErrorCorrectLevel.H:
  617. return QRRSBlock.RS_BLOCK_TABLE[4 * (a - 1) + 3];
  618. default:
  619. return void 0
  620. }
  621. }
  622. ,
  623. QRBitBuffer.prototype = {
  624. get: function(a) {
  625. var b = Math.floor(a / 8);
  626. return 1 == (1 & this.buffer[b] >>> 7 - a % 8)
  627. },
  628. put: function(a, b) {
  629. for (var c = 0; b > c; c++) {
  630. this.putBit(1 == (1 & a >>> b - c - 1))
  631. }
  632. },
  633. getLengthInBits: function() {
  634. return this.length
  635. },
  636. putBit: function(a) {
  637. var b = Math.floor(this.length / 8);
  638. this.buffer.length <= b && this.buffer.push(0),
  639. a && (this.buffer[b] |= 128 >>> this.length % 8),
  640. this.length++
  641. }
  642. };