uqrcode.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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. errorCorrectLevel: QRErrorCorrectLevel,
  1007. defaults: {
  1008. size: 354,
  1009. margin: 0,
  1010. backgroundColor: '#ffffff',
  1011. foregroundColor: '#000000',
  1012. fileType: 'png', // 'jpg', 'png'
  1013. errorCorrectLevel: QRErrorCorrectLevel.H,
  1014. typeNumber: -1
  1015. },
  1016. make: function(options) {
  1017. var defaultOptions = {
  1018. canvasId: options.canvasId,
  1019. componentInstance: options.componentInstance,
  1020. text: options.text,
  1021. size: this.defaults.size,
  1022. margin: this.defaults.margin,
  1023. backgroundColor: this.defaults.backgroundColor,
  1024. foregroundColor: this.defaults.foregroundColor,
  1025. fileType: this.defaults.fileType,
  1026. errorCorrectLevel: this.defaults.errorCorrectLevel,
  1027. typeNumber: this.defaults.typeNumber
  1028. };
  1029. if (options) {
  1030. for (var i in options) {
  1031. defaultOptions[i] = options[i];
  1032. }
  1033. }
  1034. options = defaultOptions;
  1035. if (!options.canvasId) {
  1036. console.error('uQRCode: Please set canvasId!');
  1037. return;
  1038. }
  1039. function createCanvas() {
  1040. var qrcode = new QRCode(options.typeNumber, options.errorCorrectLevel);
  1041. qrcode.addData(utf16To8(options.text));
  1042. qrcode.make();
  1043. var ctx = uni.createCanvasContext(options.canvasId, options.componentInstance);
  1044. ctx.setFillStyle(options.backgroundColor);
  1045. ctx.fillRect(0, 0, options.size, options.size);
  1046. var tileW = (options.size - options.margin * 2) / qrcode.getModuleCount();
  1047. var tileH = tileW;
  1048. for (var row = 0; row < qrcode.getModuleCount(); row++) {
  1049. for (var col = 0; col < qrcode.getModuleCount(); col++) {
  1050. var style = qrcode.isDark(row, col) ? options.foregroundColor : options.backgroundColor;
  1051. ctx.setFillStyle(style);
  1052. var x = Math.round(col * tileW) + options.margin;
  1053. var y = Math.round(row * tileH) + options.margin;
  1054. var w = Math.ceil((col + 1) * tileW) - Math.floor(col * tileW);
  1055. var h = Math.ceil((row + 1) * tileW) - Math.floor(row * tileW);
  1056. ctx.fillRect(x, y, w, h);
  1057. }
  1058. }
  1059. setTimeout(function() {
  1060. ctx.draw(false, (function() {
  1061. setTimeout(function() {
  1062. uni.canvasToTempFilePath({
  1063. canvasId: options.canvasId,
  1064. fileType: options.fileType,
  1065. width: options.size,
  1066. height: options.size,
  1067. destWidth: options.size,
  1068. destHeight: options.size,
  1069. success: function(res) {
  1070. options.success && options.success(res.tempFilePath);
  1071. },
  1072. fail: function(error) {
  1073. options.fail && options.fail(error);
  1074. },
  1075. complete: function(res) {
  1076. options.complete && options.complete(res);
  1077. }
  1078. }, options.componentInstance);
  1079. }, options.text.length + 100);
  1080. })());
  1081. }, 150);
  1082. }
  1083. createCanvas();
  1084. }
  1085. }
  1086. })()
  1087. export default uQRCode