uqrcode.js 30 KB

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