qrcode.js 37 KB

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