uqrcode.js 30 KB

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