weapp-qrcode.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. //Core code comes from https://github.com/davidshimjs/qrcodejs
  2. var QRCode;
  3. (function() {
  4. /**
  5. * Get the type by string length
  6. *
  7. * @private
  8. * @param {String} sText
  9. * @param {Number} nCorrectLevel
  10. * @return {Number} type
  11. */
  12. function _getTypeNumber(sText, nCorrectLevel) {
  13. var nType = 1;
  14. var length = _getUTF8Length(sText);
  15. for (var i = 0, len = QRCodeLimitLength.length; i <= len; i++) {
  16. var nLimit = 0;
  17. switch (nCorrectLevel) {
  18. case QRErrorCorrectLevel.L:
  19. nLimit = QRCodeLimitLength[i][0];
  20. break;
  21. case QRErrorCorrectLevel.M:
  22. nLimit = QRCodeLimitLength[i][1];
  23. break;
  24. case QRErrorCorrectLevel.Q:
  25. nLimit = QRCodeLimitLength[i][2];
  26. break;
  27. case QRErrorCorrectLevel.H:
  28. nLimit = QRCodeLimitLength[i][3];
  29. break;
  30. }
  31. if (length <= nLimit) {
  32. break;
  33. } else {
  34. nType++;
  35. }
  36. }
  37. if (nType > QRCodeLimitLength.length) {
  38. throw new Error("Too long data");
  39. }
  40. return nType;
  41. }
  42. function _getUTF8Length(sText) {
  43. var replacedText = encodeURI(sText).toString().replace(/\%[0-9a-fA-F]{2}/g, 'a');
  44. return replacedText.length + (replacedText.length != sText ? 3 : 0);
  45. }
  46. function QR8bitByte(data) {
  47. this.mode = QRMode.MODE_8BIT_BYTE;
  48. this.data = data;
  49. this.parsedData = [];
  50. // Added to support UTF-8 Characters
  51. for (var i = 0, l = this.data.length; i < l; i++) {
  52. var byteArray = [];
  53. var code = this.data.charCodeAt(i);
  54. if (code > 0x10000) {
  55. byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18);
  56. byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12);
  57. byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6);
  58. byteArray[3] = 0x80 | (code & 0x3F);
  59. } else if (code > 0x800) {
  60. byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12);
  61. byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6);
  62. byteArray[2] = 0x80 | (code & 0x3F);
  63. } else if (code > 0x80) {
  64. byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6);
  65. byteArray[1] = 0x80 | (code & 0x3F);
  66. } else {
  67. byteArray[0] = code;
  68. }
  69. this.parsedData.push(byteArray);
  70. }
  71. this.parsedData = Array.prototype.concat.apply([], this.parsedData);
  72. if (this.parsedData.length != this.data.length) {
  73. this.parsedData.unshift(191);
  74. this.parsedData.unshift(187);
  75. this.parsedData.unshift(239);
  76. }
  77. }
  78. QR8bitByte.prototype = {
  79. getLength: function(buffer) {
  80. return this.parsedData.length;
  81. },
  82. write: function(buffer) {
  83. for (var i = 0, l = this.parsedData.length; i < l; i++) {
  84. buffer.put(this.parsedData[i], 8);
  85. }
  86. }
  87. };
  88. // QRCodeModel
  89. function QRCodeModel(typeNumber, errorCorrectLevel) {
  90. this.typeNumber = typeNumber;
  91. this.errorCorrectLevel = errorCorrectLevel;
  92. this.modules = null;
  93. this.moduleCount = 0;
  94. this.dataCache = null;
  95. this.dataList = [];
  96. }
  97. QRCodeModel.prototype = {
  98. addData: function(data) {
  99. var newData = new QR8bitByte(data);
  100. this.dataList.push(newData);
  101. this.dataCache = null;
  102. },
  103. isDark: function(row, col) {
  104. if (row < 0 || this.moduleCount <= row || col < 0 || this.moduleCount <= col) {
  105. throw new Error(row + "," + col);
  106. }
  107. return this.modules[row][col];
  108. },
  109. getModuleCount: function() {
  110. return this.moduleCount;
  111. },
  112. make: function() {
  113. this.makeImpl(false, this.getBestMaskPattern());
  114. },
  115. makeImpl: function(test, maskPattern) {
  116. this.moduleCount = this.typeNumber * 4 + 17;
  117. this.modules = new Array(this.moduleCount);
  118. for (var row = 0; row < this.moduleCount; row++) {
  119. this.modules[row] = new Array(this.moduleCount);
  120. for (var col = 0; col < this.moduleCount; col++) {
  121. this.modules[row][col] = null;
  122. }
  123. }
  124. this.setupPositionProbePattern(0, 0);
  125. this.setupPositionProbePattern(this.moduleCount - 7, 0);
  126. this.setupPositionProbePattern(0, this.moduleCount - 7);
  127. this.setupPositionAdjustPattern();
  128. this.setupTimingPattern();
  129. this.setupTypeInfo(test, maskPattern);
  130. if (this.typeNumber >= 7) {
  131. this.setupTypeNumber(test);
  132. }
  133. if (this.dataCache == null) {
  134. this.dataCache = QRCodeModel.createData(this.typeNumber, this.errorCorrectLevel, this
  135. .dataList);
  136. }
  137. this.mapData(this.dataCache, maskPattern);
  138. },
  139. setupPositionProbePattern: function(row, col) {
  140. for (var r = -1; r <= 7; r++) {
  141. if (row + r <= -1 || this.moduleCount <= row + r) continue;
  142. for (var c = -1; c <= 7; c++) {
  143. if (col + c <= -1 || this.moduleCount <= col + c) continue;
  144. if ((0 <= r && r <= 6 && (c == 0 || c == 6)) || (0 <= c && c <= 6 && (r == 0 || r ==
  145. 6)) || (2 <= r && r <= 4 && 2 <= c && c <= 4)) {
  146. this.modules[row + r][col + c] = true;
  147. } else {
  148. this.modules[row + r][col + c] = false;
  149. }
  150. }
  151. }
  152. },
  153. getBestMaskPattern: function() {
  154. var minLostPoint = 0;
  155. var pattern = 0;
  156. for (var i = 0; i < 8; i++) {
  157. this.makeImpl(true, i);
  158. var lostPoint = QRUtil.getLostPoint(this);
  159. if (i == 0 || minLostPoint > lostPoint) {
  160. minLostPoint = lostPoint;
  161. pattern = i;
  162. }
  163. }
  164. return pattern;
  165. },
  166. createMovieClip: function(target_mc, instance_name, depth) {
  167. var qr_mc = target_mc.createEmptyMovieClip(instance_name, depth);
  168. var cs = 1;
  169. this.make();
  170. for (var row = 0; row < this.modules.length; row++) {
  171. var y = row * cs;
  172. for (var col = 0; col < this.modules[row].length; col++) {
  173. var x = col * cs;
  174. var dark = this.modules[row][col];
  175. if (dark) {
  176. qr_mc.beginFill(0, 100);
  177. qr_mc.moveTo(x, y);
  178. qr_mc.lineTo(x + cs, y);
  179. qr_mc.lineTo(x + cs, y + cs);
  180. qr_mc.lineTo(x, y + cs);
  181. qr_mc.endFill();
  182. }
  183. }
  184. }
  185. return qr_mc;
  186. },
  187. setupTimingPattern: function() {
  188. for (var r = 8; r < this.moduleCount - 8; r++) {
  189. if (this.modules[r][6] != null) {
  190. continue;
  191. }
  192. this.modules[r][6] = (r % 2 == 0);
  193. }
  194. for (var c = 8; c < this.moduleCount - 8; c++) {
  195. if (this.modules[6][c] != null) {
  196. continue;
  197. }
  198. this.modules[6][c] = (c % 2 == 0);
  199. }
  200. },
  201. setupPositionAdjustPattern: function() {
  202. var pos = QRUtil.getPatternPosition(this.typeNumber);
  203. for (var i = 0; i < pos.length; i++) {
  204. for (var j = 0; j < pos.length; j++) {
  205. var row = pos[i];
  206. var col = pos[j];
  207. if (this.modules[row][col] != null) {
  208. continue;
  209. }
  210. for (var r = -2; r <= 2; r++) {
  211. for (var c = -2; c <= 2; c++) {
  212. if (r == -2 || r == 2 || c == -2 || c == 2 || (r == 0 && c == 0)) {
  213. this.modules[row + r][col + c] = true;
  214. } else {
  215. this.modules[row + r][col + c] = false;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. },
  222. setupTypeNumber: function(test) {
  223. var bits = QRUtil.getBCHTypeNumber(this.typeNumber);
  224. for (var i = 0; i < 18; i++) {
  225. var mod = (!test && ((bits >> i) & 1) == 1);
  226. this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod;
  227. }
  228. for (var i = 0; i < 18; i++) {
  229. var mod = (!test && ((bits >> i) & 1) == 1);
  230. this.modules[i % 3 + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod;
  231. }
  232. },
  233. setupTypeInfo: function(test, maskPattern) {
  234. var data = (this.errorCorrectLevel << 3) | maskPattern;
  235. var bits = QRUtil.getBCHTypeInfo(data);
  236. for (var i = 0; i < 15; i++) {
  237. var mod = (!test && ((bits >> i) & 1) == 1);
  238. if (i < 6) {
  239. this.modules[i][8] = mod;
  240. } else if (i < 8) {
  241. this.modules[i + 1][8] = mod;
  242. } else {
  243. this.modules[this.moduleCount - 15 + i][8] = mod;
  244. }
  245. }
  246. for (var i = 0; i < 15; i++) {
  247. var mod = (!test && ((bits >> i) & 1) == 1);
  248. if (i < 8) {
  249. this.modules[8][this.moduleCount - i - 1] = mod;
  250. } else if (i < 9) {
  251. this.modules[8][15 - i - 1 + 1] = mod;
  252. } else {
  253. this.modules[8][15 - i - 1] = mod;
  254. }
  255. }
  256. this.modules[this.moduleCount - 8][8] = (!test);
  257. },
  258. mapData: function(data, maskPattern) {
  259. var inc = -1;
  260. var row = this.moduleCount - 1;
  261. var bitIndex = 7;
  262. var byteIndex = 0;
  263. for (var col = this.moduleCount - 1; col > 0; col -= 2) {
  264. if (col == 6) col--;
  265. while (true) {
  266. for (var c = 0; c < 2; c++) {
  267. if (this.modules[row][col - c] == null) {
  268. var dark = false;
  269. if (byteIndex < data.length) {
  270. dark = (((data[byteIndex] >>> bitIndex) & 1) == 1);
  271. }
  272. var mask = QRUtil.getMask(maskPattern, row, col - c);
  273. if (mask) {
  274. dark = !dark;
  275. }
  276. this.modules[row][col - c] = dark;
  277. bitIndex--;
  278. if (bitIndex == -1) {
  279. byteIndex++;
  280. bitIndex = 7;
  281. }
  282. }
  283. }
  284. row += inc;
  285. if (row < 0 || this.moduleCount <= row) {
  286. row -= inc;
  287. inc = -inc;
  288. break;
  289. }
  290. }
  291. }
  292. }
  293. };
  294. QRCodeModel.PAD0 = 0xEC;
  295. QRCodeModel.PAD1 = 0x11;
  296. QRCodeModel.createData = function(typeNumber, errorCorrectLevel, dataList) {
  297. var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, errorCorrectLevel);
  298. var buffer = new QRBitBuffer();
  299. for (var i = 0; i < dataList.length; i++) {
  300. var data = dataList[i];
  301. buffer.put(data.mode, 4);
  302. buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, typeNumber));
  303. data.write(buffer);
  304. }
  305. var totalDataCount = 0;
  306. for (var i = 0; i < rsBlocks.length; i++) {
  307. totalDataCount += rsBlocks[i].dataCount;
  308. }
  309. if (buffer.getLengthInBits() > totalDataCount * 8) {
  310. throw new Error("code length overflow. (" +
  311. buffer.getLengthInBits() +
  312. ">" +
  313. totalDataCount * 8 +
  314. ")");
  315. }
  316. if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
  317. buffer.put(0, 4);
  318. }
  319. while (buffer.getLengthInBits() % 8 != 0) {
  320. buffer.putBit(false);
  321. }
  322. while (true) {
  323. if (buffer.getLengthInBits() >= totalDataCount * 8) {
  324. break;
  325. }
  326. buffer.put(QRCodeModel.PAD0, 8);
  327. if (buffer.getLengthInBits() >= totalDataCount * 8) {
  328. break;
  329. }
  330. buffer.put(QRCodeModel.PAD1, 8);
  331. }
  332. return QRCodeModel.createBytes(buffer, rsBlocks);
  333. };
  334. QRCodeModel.createBytes = function(buffer, rsBlocks) {
  335. var offset = 0;
  336. var maxDcCount = 0;
  337. var maxEcCount = 0;
  338. var dcdata = new Array(rsBlocks.length);
  339. var ecdata = new Array(rsBlocks.length);
  340. for (var r = 0; r < rsBlocks.length; r++) {
  341. var dcCount = rsBlocks[r].dataCount;
  342. var ecCount = rsBlocks[r].totalCount - dcCount;
  343. maxDcCount = Math.max(maxDcCount, dcCount);
  344. maxEcCount = Math.max(maxEcCount, ecCount);
  345. dcdata[r] = new Array(dcCount);
  346. for (var i = 0; i < dcdata[r].length; i++) {
  347. dcdata[r][i] = 0xff & buffer.buffer[i + offset];
  348. }
  349. offset += dcCount;
  350. var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount);
  351. var rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1);
  352. var modPoly = rawPoly.mod(rsPoly);
  353. ecdata[r] = new Array(rsPoly.getLength() - 1);
  354. for (var i = 0; i < ecdata[r].length; i++) {
  355. var modIndex = i + modPoly.getLength() - ecdata[r].length;
  356. ecdata[r][i] = (modIndex >= 0) ? modPoly.get(modIndex) : 0;
  357. }
  358. }
  359. var totalCodeCount = 0;
  360. for (var i = 0; i < rsBlocks.length; i++) {
  361. totalCodeCount += rsBlocks[i].totalCount;
  362. }
  363. var data = new Array(totalCodeCount);
  364. var index = 0;
  365. for (var i = 0; i < maxDcCount; i++) {
  366. for (var r = 0; r < rsBlocks.length; r++) {
  367. if (i < dcdata[r].length) {
  368. data[index++] = dcdata[r][i];
  369. }
  370. }
  371. }
  372. for (var i = 0; i < maxEcCount; i++) {
  373. for (var r = 0; r < rsBlocks.length; r++) {
  374. if (i < ecdata[r].length) {
  375. data[index++] = ecdata[r][i];
  376. }
  377. }
  378. }
  379. return data;
  380. };
  381. var QRMode = {
  382. MODE_NUMBER: 1 << 0,
  383. MODE_ALPHA_NUM: 1 << 1,
  384. MODE_8BIT_BYTE: 1 << 2,
  385. MODE_KANJI: 1 << 3
  386. };
  387. var QRErrorCorrectLevel = {
  388. L: 1,
  389. M: 0,
  390. Q: 3,
  391. H: 2
  392. };
  393. var QRMaskPattern = {
  394. PATTERN000: 0,
  395. PATTERN001: 1,
  396. PATTERN010: 2,
  397. PATTERN011: 3,
  398. PATTERN100: 4,
  399. PATTERN101: 5,
  400. PATTERN110: 6,
  401. PATTERN111: 7
  402. };
  403. var QRUtil = {
  404. PATTERN_POSITION_TABLE: [
  405. [],
  406. [6, 18],
  407. [6, 22],
  408. [6, 26],
  409. [6, 30],
  410. [6, 34],
  411. [6, 22, 38],
  412. [6, 24, 42],
  413. [6, 26, 46],
  414. [6, 28, 50],
  415. [6, 30, 54],
  416. [6, 32, 58],
  417. [6, 34, 62],
  418. [6, 26, 46, 66],
  419. [6, 26, 48, 70],
  420. [6, 26, 50, 74],
  421. [6, 30, 54, 78],
  422. [6, 30, 56, 82],
  423. [6, 30, 58, 86],
  424. [6, 34, 62, 90],
  425. [6, 28, 50, 72, 94],
  426. [6, 26, 50, 74, 98],
  427. [6, 30, 54, 78, 102],
  428. [6, 28, 54, 80, 106],
  429. [6, 32, 58, 84, 110],
  430. [6, 30, 58, 86, 114],
  431. [6, 34, 62, 90, 118],
  432. [6, 26, 50, 74, 98, 122],
  433. [6, 30, 54, 78, 102, 126],
  434. [6, 26, 52, 78, 104, 130],
  435. [6, 30, 56, 82, 108, 134],
  436. [6, 34, 60, 86, 112, 138],
  437. [6, 30, 58, 86, 114, 142],
  438. [6, 34, 62, 90, 118, 146],
  439. [6, 30, 54, 78, 102, 126, 150],
  440. [6, 24, 50, 76, 102, 128, 154],
  441. [6, 28, 54, 80, 106, 132, 158],
  442. [6, 32, 58, 84, 110, 136, 162],
  443. [6, 26, 54, 82, 110, 138, 166],
  444. [6, 30, 58, 86, 114, 142, 170]
  445. ],
  446. G15: (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0),
  447. G18: (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0),
  448. G15_MASK: (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1),
  449. getBCHTypeInfo: function(data) {
  450. var d = data << 10;
  451. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) {
  452. d ^= (QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15)));
  453. }
  454. return ((data << 10) | d) ^ QRUtil.G15_MASK;
  455. },
  456. getBCHTypeNumber: function(data) {
  457. var d = data << 12;
  458. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) {
  459. d ^= (QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18)));
  460. }
  461. return (data << 12) | d;
  462. },
  463. getBCHDigit: function(data) {
  464. var digit = 0;
  465. while (data != 0) {
  466. digit++;
  467. data >>>= 1;
  468. }
  469. return digit;
  470. },
  471. getPatternPosition: function(typeNumber) {
  472. return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1];
  473. },
  474. getMask: function(maskPattern, i, j) {
  475. switch (maskPattern) {
  476. case QRMaskPattern.PATTERN000:
  477. return (i + j) % 2 == 0;
  478. case QRMaskPattern.PATTERN001:
  479. return i % 2 == 0;
  480. case QRMaskPattern.PATTERN010:
  481. return j % 3 == 0;
  482. case QRMaskPattern.PATTERN011:
  483. return (i + j) % 3 == 0;
  484. case QRMaskPattern.PATTERN100:
  485. return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0;
  486. case QRMaskPattern.PATTERN101:
  487. return (i * j) % 2 + (i * j) % 3 == 0;
  488. case QRMaskPattern.PATTERN110:
  489. return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
  490. case QRMaskPattern.PATTERN111:
  491. return ((i * j) % 3 + (i + j) % 2) % 2 == 0;
  492. default:
  493. throw new Error("bad maskPattern:" + maskPattern);
  494. }
  495. },
  496. getErrorCorrectPolynomial: function(errorCorrectLength) {
  497. var a = new QRPolynomial([1], 0);
  498. for (var i = 0; i < errorCorrectLength; i++) {
  499. a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0));
  500. }
  501. return a;
  502. },
  503. getLengthInBits: function(mode, type) {
  504. if (1 <= type && type < 10) {
  505. switch (mode) {
  506. case QRMode.MODE_NUMBER:
  507. return 10;
  508. case QRMode.MODE_ALPHA_NUM:
  509. return 9;
  510. case QRMode.MODE_8BIT_BYTE:
  511. return 8;
  512. case QRMode.MODE_KANJI:
  513. return 8;
  514. default:
  515. throw new Error("mode:" + mode);
  516. }
  517. } else if (type < 27) {
  518. switch (mode) {
  519. case QRMode.MODE_NUMBER:
  520. return 12;
  521. case QRMode.MODE_ALPHA_NUM:
  522. return 11;
  523. case QRMode.MODE_8BIT_BYTE:
  524. return 16;
  525. case QRMode.MODE_KANJI:
  526. return 10;
  527. default:
  528. throw new Error("mode:" + mode);
  529. }
  530. } else if (type < 41) {
  531. switch (mode) {
  532. case QRMode.MODE_NUMBER:
  533. return 14;
  534. case QRMode.MODE_ALPHA_NUM:
  535. return 13;
  536. case QRMode.MODE_8BIT_BYTE:
  537. return 16;
  538. case QRMode.MODE_KANJI:
  539. return 12;
  540. default:
  541. throw new Error("mode:" + mode);
  542. }
  543. } else {
  544. throw new Error("type:" + type);
  545. }
  546. },
  547. getLostPoint: function(qrCode) {
  548. var moduleCount = qrCode.getModuleCount();
  549. var lostPoint = 0;
  550. for (var row = 0; row < moduleCount; row++) {
  551. for (var col = 0; col < moduleCount; col++) {
  552. var sameCount = 0;
  553. var dark = qrCode.isDark(row, col);
  554. for (var r = -1; r <= 1; r++) {
  555. if (row + r < 0 || moduleCount <= row + r) {
  556. continue;
  557. }
  558. for (var c = -1; c <= 1; c++) {
  559. if (col + c < 0 || moduleCount <= col + c) {
  560. continue;
  561. }
  562. if (r == 0 && c == 0) {
  563. continue;
  564. }
  565. if (dark == qrCode.isDark(row + r, col + c)) {
  566. sameCount++;
  567. }
  568. }
  569. }
  570. if (sameCount > 5) {
  571. lostPoint += (3 + sameCount - 5);
  572. }
  573. }
  574. }
  575. for (var row = 0; row < moduleCount - 1; row++) {
  576. for (var col = 0; col < moduleCount - 1; col++) {
  577. var count = 0;
  578. if (qrCode.isDark(row, col)) count++;
  579. if (qrCode.isDark(row + 1, col)) count++;
  580. if (qrCode.isDark(row, col + 1)) count++;
  581. if (qrCode.isDark(row + 1, col + 1)) count++;
  582. if (count == 0 || count == 4) {
  583. lostPoint += 3;
  584. }
  585. }
  586. }
  587. for (var row = 0; row < moduleCount; row++) {
  588. for (var col = 0; col < moduleCount - 6; col++) {
  589. if (qrCode.isDark(row, col) && !qrCode.isDark(row, col + 1) && qrCode.isDark(row, col +
  590. 2) && qrCode.isDark(row, col + 3) && qrCode.isDark(row, col + 4) && !qrCode
  591. .isDark(row, col + 5) && qrCode.isDark(row, col + 6)) {
  592. lostPoint += 40;
  593. }
  594. }
  595. }
  596. for (var col = 0; col < moduleCount; col++) {
  597. for (var row = 0; row < moduleCount - 6; row++) {
  598. if (qrCode.isDark(row, col) && !qrCode.isDark(row + 1, col) && qrCode.isDark(row + 2,
  599. col) && qrCode.isDark(row + 3, col) && qrCode.isDark(row + 4, col) && !qrCode
  600. .isDark(row + 5, col) && qrCode.isDark(row + 6, col)) {
  601. lostPoint += 40;
  602. }
  603. }
  604. }
  605. var darkCount = 0;
  606. for (var col = 0; col < moduleCount; col++) {
  607. for (var row = 0; row < moduleCount; row++) {
  608. if (qrCode.isDark(row, col)) {
  609. darkCount++;
  610. }
  611. }
  612. }
  613. var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5;
  614. lostPoint += ratio * 10;
  615. return lostPoint;
  616. }
  617. };
  618. var QRMath = {
  619. glog: function(n) {
  620. if (n < 1) {
  621. throw new Error("glog(" + n + ")");
  622. }
  623. return QRMath.LOG_TABLE[n];
  624. },
  625. gexp: function(n) {
  626. while (n < 0) {
  627. n += 255;
  628. }
  629. while (n >= 256) {
  630. n -= 255;
  631. }
  632. return QRMath.EXP_TABLE[n];
  633. },
  634. EXP_TABLE: new Array(256),
  635. LOG_TABLE: new Array(256)
  636. };
  637. for (var i = 0; i < 8; i++) {
  638. QRMath.EXP_TABLE[i] = 1 << i;
  639. }
  640. for (var i = 8; i < 256; i++) {
  641. QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath
  642. .EXP_TABLE[i - 8];
  643. }
  644. for (var i = 0; i < 255; i++) {
  645. QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
  646. }
  647. function QRPolynomial(num, shift) {
  648. if (num.length == undefined) {
  649. throw new Error(num.length + "/" + shift);
  650. }
  651. var offset = 0;
  652. while (offset < num.length && num[offset] == 0) {
  653. offset++;
  654. }
  655. this.num = new Array(num.length - offset + shift);
  656. for (var i = 0; i < num.length - offset; i++) {
  657. this.num[i] = num[i + offset];
  658. }
  659. }
  660. QRPolynomial.prototype = {
  661. get: function(index) {
  662. return this.num[index];
  663. },
  664. getLength: function() {
  665. return this.num.length;
  666. },
  667. multiply: function(e) {
  668. var num = new Array(this.getLength() + e.getLength() - 1);
  669. for (var i = 0; i < this.getLength(); i++) {
  670. for (var j = 0; j < e.getLength(); j++) {
  671. num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j)));
  672. }
  673. }
  674. return new QRPolynomial(num, 0);
  675. },
  676. mod: function(e) {
  677. if (this.getLength() - e.getLength() < 0) {
  678. return this;
  679. }
  680. var ratio = QRMath.glog(this.get(0)) - QRMath.glog(e.get(0));
  681. var num = new Array(this.getLength());
  682. for (var i = 0; i < this.getLength(); i++) {
  683. num[i] = this.get(i);
  684. }
  685. for (var i = 0; i < e.getLength(); i++) {
  686. num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio);
  687. }
  688. return new QRPolynomial(num, 0).mod(e);
  689. }
  690. };
  691. function QRRSBlock(totalCount, dataCount) {
  692. this.totalCount = totalCount;
  693. this.dataCount = dataCount;
  694. }
  695. QRRSBlock.RS_BLOCK_TABLE = [
  696. [1, 26, 19],
  697. [1, 26, 16],
  698. [1, 26, 13],
  699. [1, 26, 9],
  700. [1, 44, 34],
  701. [1, 44, 28],
  702. [1, 44, 22],
  703. [1, 44, 16],
  704. [1, 70, 55],
  705. [1, 70, 44],
  706. [2, 35, 17],
  707. [2, 35, 13],
  708. [1, 100, 80],
  709. [2, 50, 32],
  710. [2, 50, 24],
  711. [4, 25, 9],
  712. [1, 134, 108],
  713. [2, 67, 43],
  714. [2, 33, 15, 2, 34, 16],
  715. [2, 33, 11, 2, 34, 12],
  716. [2, 86, 68],
  717. [4, 43, 27],
  718. [4, 43, 19],
  719. [4, 43, 15],
  720. [2, 98, 78],
  721. [4, 49, 31],
  722. [2, 32, 14, 4, 33, 15],
  723. [4, 39, 13, 1, 40, 14],
  724. [2, 121, 97],
  725. [2, 60, 38, 2, 61, 39],
  726. [4, 40, 18, 2, 41, 19],
  727. [4, 40, 14, 2, 41, 15],
  728. [2, 146, 116],
  729. [3, 58, 36, 2, 59, 37],
  730. [4, 36, 16, 4, 37, 17],
  731. [4, 36, 12, 4, 37, 13],
  732. [2, 86, 68, 2, 87, 69],
  733. [4, 69, 43, 1, 70, 44],
  734. [6, 43, 19, 2, 44, 20],
  735. [6, 43, 15, 2, 44, 16],
  736. [4, 101, 81],
  737. [1, 80, 50, 4, 81, 51],
  738. [4, 50, 22, 4, 51, 23],
  739. [3, 36, 12, 8, 37, 13],
  740. [2, 116, 92, 2, 117, 93],
  741. [6, 58, 36, 2, 59, 37],
  742. [4, 46, 20, 6, 47, 21],
  743. [7, 42, 14, 4, 43, 15],
  744. [4, 133, 107],
  745. [8, 59, 37, 1, 60, 38],
  746. [8, 44, 20, 4, 45, 21],
  747. [12, 33, 11, 4, 34, 12],
  748. [3, 145, 115, 1, 146, 116],
  749. [4, 64, 40, 5, 65, 41],
  750. [11, 36, 16, 5, 37, 17],
  751. [11, 36, 12, 5, 37, 13],
  752. [5, 109, 87, 1, 110, 88],
  753. [5, 65, 41, 5, 66, 42],
  754. [5, 54, 24, 7, 55, 25],
  755. [11, 36, 12],
  756. [5, 122, 98, 1, 123, 99],
  757. [7, 73, 45, 3, 74, 46],
  758. [15, 43, 19, 2, 44, 20],
  759. [3, 45, 15, 13, 46, 16],
  760. [1, 135, 107, 5, 136, 108],
  761. [10, 74, 46, 1, 75, 47],
  762. [1, 50, 22, 15, 51, 23],
  763. [2, 42, 14, 17, 43, 15],
  764. [5, 150, 120, 1, 151, 121],
  765. [9, 69, 43, 4, 70, 44],
  766. [17, 50, 22, 1, 51, 23],
  767. [2, 42, 14, 19, 43, 15],
  768. [3, 141, 113, 4, 142, 114],
  769. [3, 70, 44, 11, 71, 45],
  770. [17, 47, 21, 4, 48, 22],
  771. [9, 39, 13, 16, 40, 14],
  772. [3, 135, 107, 5, 136, 108],
  773. [3, 67, 41, 13, 68, 42],
  774. [15, 54, 24, 5, 55, 25],
  775. [15, 43, 15, 10, 44, 16],
  776. [4, 144, 116, 4, 145, 117],
  777. [17, 68, 42],
  778. [17, 50, 22, 6, 51, 23],
  779. [19, 46, 16, 6, 47, 17],
  780. [2, 139, 111, 7, 140, 112],
  781. [17, 74, 46],
  782. [7, 54, 24, 16, 55, 25],
  783. [34, 37, 13],
  784. [4, 151, 121, 5, 152, 122],
  785. [4, 75, 47, 14, 76, 48],
  786. [11, 54, 24, 14, 55, 25],
  787. [16, 45, 15, 14, 46, 16],
  788. [6, 147, 117, 4, 148, 118],
  789. [6, 73, 45, 14, 74, 46],
  790. [11, 54, 24, 16, 55, 25],
  791. [30, 46, 16, 2, 47, 17],
  792. [8, 132, 106, 4, 133, 107],
  793. [8, 75, 47, 13, 76, 48],
  794. [7, 54, 24, 22, 55, 25],
  795. [22, 45, 15, 13, 46, 16],
  796. [10, 142, 114, 2, 143, 115],
  797. [19, 74, 46, 4, 75, 47],
  798. [28, 50, 22, 6, 51, 23],
  799. [33, 46, 16, 4, 47, 17],
  800. [8, 152, 122, 4, 153, 123],
  801. [22, 73, 45, 3, 74, 46],
  802. [8, 53, 23, 26, 54, 24],
  803. [12, 45, 15, 28, 46, 16],
  804. [3, 147, 117, 10, 148, 118],
  805. [3, 73, 45, 23, 74, 46],
  806. [4, 54, 24, 31, 55, 25],
  807. [11, 45, 15, 31, 46, 16],
  808. [7, 146, 116, 7, 147, 117],
  809. [21, 73, 45, 7, 74, 46],
  810. [1, 53, 23, 37, 54, 24],
  811. [19, 45, 15, 26, 46, 16],
  812. [5, 145, 115, 10, 146, 116],
  813. [19, 75, 47, 10, 76, 48],
  814. [15, 54, 24, 25, 55, 25],
  815. [23, 45, 15, 25, 46, 16],
  816. [13, 145, 115, 3, 146, 116],
  817. [2, 74, 46, 29, 75, 47],
  818. [42, 54, 24, 1, 55, 25],
  819. [23, 45, 15, 28, 46, 16],
  820. [17, 145, 115],
  821. [10, 74, 46, 23, 75, 47],
  822. [10, 54, 24, 35, 55, 25],
  823. [19, 45, 15, 35, 46, 16],
  824. [17, 145, 115, 1, 146, 116],
  825. [14, 74, 46, 21, 75, 47],
  826. [29, 54, 24, 19, 55, 25],
  827. [11, 45, 15, 46, 46, 16],
  828. [13, 145, 115, 6, 146, 116],
  829. [14, 74, 46, 23, 75, 47],
  830. [44, 54, 24, 7, 55, 25],
  831. [59, 46, 16, 1, 47, 17],
  832. [12, 151, 121, 7, 152, 122],
  833. [12, 75, 47, 26, 76, 48],
  834. [39, 54, 24, 14, 55, 25],
  835. [22, 45, 15, 41, 46, 16],
  836. [6, 151, 121, 14, 152, 122],
  837. [6, 75, 47, 34, 76, 48],
  838. [46, 54, 24, 10, 55, 25],
  839. [2, 45, 15, 64, 46, 16],
  840. [17, 152, 122, 4, 153, 123],
  841. [29, 74, 46, 14, 75, 47],
  842. [49, 54, 24, 10, 55, 25],
  843. [24, 45, 15, 46, 46, 16],
  844. [4, 152, 122, 18, 153, 123],
  845. [13, 74, 46, 32, 75, 47],
  846. [48, 54, 24, 14, 55, 25],
  847. [42, 45, 15, 32, 46, 16],
  848. [20, 147, 117, 4, 148, 118],
  849. [40, 75, 47, 7, 76, 48],
  850. [43, 54, 24, 22, 55, 25],
  851. [10, 45, 15, 67, 46, 16],
  852. [19, 148, 118, 6, 149, 119],
  853. [18, 75, 47, 31, 76, 48],
  854. [34, 54, 24, 34, 55, 25],
  855. [20, 45, 15, 61, 46, 16]
  856. ];
  857. QRRSBlock.getRSBlocks = function(typeNumber, errorCorrectLevel) {
  858. var rsBlock = QRRSBlock.getRsBlockTable(typeNumber, errorCorrectLevel);
  859. if (rsBlock == undefined) {
  860. throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" +
  861. errorCorrectLevel);
  862. }
  863. var length = rsBlock.length / 3;
  864. var list = [];
  865. for (var i = 0; i < length; i++) {
  866. var count = rsBlock[i * 3 + 0];
  867. var totalCount = rsBlock[i * 3 + 1];
  868. var dataCount = rsBlock[i * 3 + 2];
  869. for (var j = 0; j < count; j++) {
  870. list.push(new QRRSBlock(totalCount, dataCount));
  871. }
  872. }
  873. return list;
  874. };
  875. QRRSBlock.getRsBlockTable = function(typeNumber, errorCorrectLevel) {
  876. switch (errorCorrectLevel) {
  877. case QRErrorCorrectLevel.L:
  878. return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
  879. case QRErrorCorrectLevel.M:
  880. return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
  881. case QRErrorCorrectLevel.Q:
  882. return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
  883. case QRErrorCorrectLevel.H:
  884. return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
  885. default:
  886. return undefined;
  887. }
  888. };
  889. function QRBitBuffer() {
  890. this.buffer = [];
  891. this.length = 0;
  892. }
  893. QRBitBuffer.prototype = {
  894. get: function(index) {
  895. var bufIndex = Math.floor(index / 8);
  896. return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1) == 1;
  897. },
  898. put: function(num, length) {
  899. for (var i = 0; i < length; i++) {
  900. this.putBit(((num >>> (length - i - 1)) & 1) == 1);
  901. }
  902. },
  903. getLengthInBits: function() {
  904. return this.length;
  905. },
  906. putBit: function(bit) {
  907. var bufIndex = Math.floor(this.length / 8);
  908. if (this.buffer.length <= bufIndex) {
  909. this.buffer.push(0);
  910. }
  911. if (bit) {
  912. this.buffer[bufIndex] |= (0x80 >>> (this.length % 8));
  913. }
  914. this.length++;
  915. }
  916. };
  917. var QRCodeLimitLength = [
  918. [17, 14, 11, 7],
  919. [32, 26, 20, 14],
  920. [53, 42, 32, 24],
  921. [78, 62, 46, 34],
  922. [106, 84, 60, 44],
  923. [134, 106, 74, 58],
  924. [154, 122, 86, 64],
  925. [192, 152, 108, 84],
  926. [230, 180, 130, 98],
  927. [271, 213, 151, 119],
  928. [321, 251, 177, 137],
  929. [367, 287, 203, 155],
  930. [425, 331, 241, 177],
  931. [458, 362, 258, 194],
  932. [520, 412, 292, 220],
  933. [586, 450, 322, 250],
  934. [644, 504, 364, 280],
  935. [718, 560, 394, 310],
  936. [792, 624, 442, 338],
  937. [858, 666, 482, 382],
  938. [929, 711, 509, 403],
  939. [1003, 779, 565, 439],
  940. [1091, 857, 611, 461],
  941. [1171, 911, 661, 511],
  942. [1273, 997, 715, 535],
  943. [1367, 1059, 751, 593],
  944. [1465, 1125, 805, 625],
  945. [1528, 1190, 868, 658],
  946. [1628, 1264, 908, 698],
  947. [1732, 1370, 982, 742],
  948. [1840, 1452, 1030, 790],
  949. [1952, 1538, 1112, 842],
  950. [2068, 1628, 1168, 898],
  951. [2188, 1722, 1228, 958],
  952. [2303, 1809, 1283, 983],
  953. [2431, 1911, 1351, 1051],
  954. [2563, 1989, 1423, 1093],
  955. [2699, 2099, 1499, 1139],
  956. [2809, 2213, 1579, 1219],
  957. [2953, 2331, 1663, 1273]
  958. ];
  959. // QRCode object
  960. QRCode = function(canvasId, vOption) {
  961. this._htOption = {
  962. width: 256,
  963. height: 256,
  964. typeNumber: 4,
  965. colorDark: "#000000",
  966. colorLight: "#ffffff",
  967. correctLevel: QRErrorCorrectLevel.H
  968. };
  969. if (typeof vOption === 'string') {
  970. vOption = {
  971. text: vOption
  972. };
  973. }
  974. // Overwrites options
  975. if (vOption) {
  976. for (var i in vOption) {
  977. this._htOption[i] = vOption[i];
  978. }
  979. }
  980. this._oQRCode = null;
  981. this.canvasId = canvasId
  982. if (this._htOption.text && this.canvasId) {
  983. this.makeCode(this._htOption.text);
  984. }
  985. };
  986. QRCode.prototype.makeCode = function(sText) {
  987. this._oQRCode = new QRCodeModel(_getTypeNumber(sText, this._htOption.correctLevel), this._htOption
  988. .correctLevel);
  989. this._oQRCode.addData(sText);
  990. this._oQRCode.make();
  991. this.makeImage();
  992. };
  993. QRCode.prototype.makeImage = function() {
  994. var _oContext
  995. if (this._htOption.usingIn) {
  996. _oContext = wx.createCanvasContext(this.canvasId, this._htOption.usingIn)
  997. } else {
  998. _oContext = wx.createCanvasContext(this.canvasId)
  999. }
  1000. var _htOption = this._htOption;
  1001. var oQRCode = this._oQRCode
  1002. var nCount = oQRCode.getModuleCount();
  1003. var nWidth = _htOption.width / nCount;
  1004. var nHeight = _htOption.height / nCount;
  1005. var nRoundedWidth = Math.round(nWidth);
  1006. var nRoundedHeight = Math.round(nHeight);
  1007. if (_htOption.image && _htOption.image != '') {
  1008. _oContext.drawImage(_htOption.image, 0, 0, _htOption.width, _htOption.height)
  1009. }
  1010. for (var row = 0; row < nCount; row++) {
  1011. for (var col = 0; col < nCount; col++) {
  1012. var bIsDark = oQRCode.isDark(row, col);
  1013. var nLeft = col * nWidth;
  1014. var nTop = row * nHeight;
  1015. _oContext.setStrokeStyle(bIsDark ? _htOption.colorDark : _htOption.colorLight)
  1016. // _oContext.setStrokeStyle('yellow')
  1017. _oContext.setLineWidth(1)
  1018. _oContext.setFillStyle(bIsDark ? _htOption.colorDark : _htOption.colorLight)
  1019. // _oContext.setFillStyle('red')
  1020. // if (bIsDark) {
  1021. _oContext.fillRect(nLeft, nTop, nWidth, nHeight);
  1022. // }
  1023. // 안티 앨리어싱 방지 처리
  1024. // if (bIsDark) {
  1025. _oContext.strokeRect(
  1026. Math.floor(nLeft) + 0.5,
  1027. Math.floor(nTop) + 0.5,
  1028. nRoundedWidth,
  1029. nRoundedHeight
  1030. );
  1031. _oContext.strokeRect(
  1032. Math.ceil(nLeft) - 0.5,
  1033. Math.ceil(nTop) - 0.5,
  1034. nRoundedWidth,
  1035. nRoundedHeight
  1036. );
  1037. // }
  1038. // _oContext.fillRect(
  1039. // Math.floor(nLeft) + 0.5,
  1040. // Math.floor(nTop) + 0.5,
  1041. // nRoundedWidth,
  1042. // nRoundedHeight
  1043. // );
  1044. // _oContext.fillRect(
  1045. // Math.ceil(nLeft) - 0.5,
  1046. // Math.ceil(nTop) - 0.5,
  1047. // nRoundedWidth,
  1048. // nRoundedHeight
  1049. // );
  1050. // _oContext.clearRect(
  1051. // Math.floor(nLeft) + 0.5,
  1052. // Math.floor(nTop) + 0.5,
  1053. // nRoundedWidth,
  1054. // nRoundedHeight
  1055. // );
  1056. // _oContext.clearRect(
  1057. // Math.ceil(nLeft) - 0.5,
  1058. // Math.ceil(nTop) - 0.5,
  1059. // nRoundedWidth,
  1060. // nRoundedHeight
  1061. // );
  1062. }
  1063. }
  1064. _oContext.draw()
  1065. };
  1066. // 保存为图片,将临时路径传给回调
  1067. QRCode.prototype.exportImage = function(callback) {
  1068. if (!callback) {
  1069. return
  1070. }
  1071. wx.canvasToTempFilePath({
  1072. x: 0,
  1073. y: 0,
  1074. width: this._htOption.width,
  1075. height: this._htOption.height,
  1076. destWidth: this._htOption.width,
  1077. destHeight: this._htOption.height,
  1078. canvasId: this.canvasId,
  1079. success: function(res) {
  1080. console.log(res.tempFilePath)
  1081. callback(res.tempFilePath)
  1082. }
  1083. })
  1084. }
  1085. QRCode.CorrectLevel = QRErrorCorrectLevel;
  1086. })();
  1087. module.exports = QRCode