qrcode.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. //qrcode.js,复制下面代码粘贴即可
  2. let QRCode = {};
  3. (function () {
  4. /**
  5. * 获取单个字符的utf8编码
  6. * unicode BMP平面约65535个字符
  7. * @param {num} code
  8. * return {array}
  9. */
  10. function unicodeFormat8(code) {
  11. // 1 byte
  12. var c0, c1, c2;
  13. if (code < 128) {
  14. return [code];
  15. // 2 bytes
  16. } else if (code < 2048) {
  17. c0 = 192 + (code >> 6);
  18. c1 = 128 + (code & 63);
  19. return [c0, c1];
  20. // 3 bytes
  21. } else {
  22. c0 = 224 + (code >> 12);
  23. c1 = 128 + (code >> 6 & 63);
  24. c2 = 128 + (code & 63);
  25. return [c0, c1, c2];
  26. }
  27. }
  28. /**
  29. * 获取字符串的utf8编码字节串
  30. * @param {string} string
  31. * @return {array}
  32. */
  33. function getUTF8Bytes(string) {
  34. var utf8codes = [];
  35. for (var i = 0; i < string.length; i++) {
  36. var code = string.charCodeAt(i);
  37. var utf8 = unicodeFormat8(code);
  38. for (var j = 0; j < utf8.length; j++) {
  39. utf8codes.push(utf8[j]);
  40. }
  41. }
  42. return utf8codes;
  43. }
  44. /**
  45. * 二维码算法实现
  46. * @param {string} data 要编码的信息字符串
  47. * @param {num} errorCorrectLevel 纠错等级
  48. */
  49. function QRCodeAlg(data, errorCorrectLevel) {
  50. this.typeNumber = -1; //版本
  51. this.errorCorrectLevel = errorCorrectLevel;
  52. this.modules = null; //二维矩阵,存放最终结果
  53. this.moduleCount = 0; //矩阵大小
  54. this.dataCache = null; //数据缓存
  55. this.rsBlocks = null; //版本数据信息
  56. this.totalDataCount = -1; //可使用的数据量
  57. this.data = data;
  58. this.utf8bytes = getUTF8Bytes(data);
  59. this.make();
  60. }
  61. QRCodeAlg.prototype = {
  62. constructor: QRCodeAlg,
  63. /**
  64. * 获取二维码矩阵大小
  65. * @return {num} 矩阵大小
  66. */
  67. getModuleCount: function () {
  68. return this.moduleCount;
  69. },
  70. /**
  71. * 编码
  72. */
  73. make: function () {
  74. this.getRightType();
  75. this.dataCache = this.createData();
  76. this.createQrcode();
  77. },
  78. /**
  79. * 设置二位矩阵功能图形
  80. * @param {bool} test 表示是否在寻找最好掩膜阶段
  81. * @param {num} maskPattern 掩膜的版本
  82. */
  83. makeImpl: function (maskPattern) {
  84. this.moduleCount = this.typeNumber * 4 + 17;
  85. this.modules = new Array(this.moduleCount);
  86. for (var row = 0; row < this.moduleCount; row++) {
  87. this.modules[row] = new Array(this.moduleCount);
  88. }
  89. this.setupPositionProbePattern(0, 0);
  90. this.setupPositionProbePattern(this.moduleCount - 7, 0);
  91. this.setupPositionProbePattern(0, this.moduleCount - 7);
  92. this.setupPositionAdjustPattern();
  93. this.setupTimingPattern();
  94. this.setupTypeInfo(true, maskPattern);
  95. if (this.typeNumber >= 7) {
  96. this.setupTypeNumber(true);
  97. }
  98. this.mapData(this.dataCache, maskPattern);
  99. },
  100. /**
  101. * 设置二维码的位置探测图形
  102. * @param {num} row 探测图形的中心横坐标
  103. * @param {num} col 探测图形的中心纵坐标
  104. */
  105. setupPositionProbePattern: function (row, col) {
  106. for (var r = -1; r <= 7; r++) {
  107. if (row + r <= -1 || this.moduleCount <= row + r) continue;
  108. for (var c = -1; c <= 7; c++) {
  109. if (col + c <= -1 || this.moduleCount <= col + c) continue;
  110. if ((0 <= r && r <= 6 && (c == 0 || c == 6)) || (0 <= c && c <= 6 && (r == 0 || r == 6)) || (2 <= r && r <= 4 && 2 <= c && c <= 4)) {
  111. this.modules[row + r][col + c] = true;
  112. } else {
  113. this.modules[row + r][col + c] = false;
  114. }
  115. }
  116. }
  117. },
  118. /**
  119. * 创建二维码
  120. * @return {[type]} [description]
  121. */
  122. createQrcode: function () {
  123. var minLostPoint = 0;
  124. var pattern = 0;
  125. var bestModules = null;
  126. for (var i = 0; i < 8; i++) {
  127. this.makeImpl(i);
  128. var lostPoint = QRUtil.getLostPoint(this);
  129. if (i == 0 || minLostPoint > lostPoint) {
  130. minLostPoint = lostPoint;
  131. pattern = i;
  132. bestModules = this.modules;
  133. }
  134. }
  135. this.modules = bestModules;
  136. this.setupTypeInfo(false, pattern);
  137. if (this.typeNumber >= 7) {
  138. this.setupTypeNumber(false);
  139. }
  140. },
  141. /**
  142. * 设置定位图形
  143. * @return {[type]} [description]
  144. */
  145. setupTimingPattern: function () {
  146. for (var r = 8; r < this.moduleCount - 8; r++) {
  147. if (this.modules[r][6] != null) {
  148. continue;
  149. }
  150. this.modules[r][6] = (r % 2 == 0);
  151. if (this.modules[6][r] != null) {
  152. continue;
  153. }
  154. this.modules[6][r] = (r % 2 == 0);
  155. }
  156. },
  157. /**
  158. * 设置矫正图形
  159. * @return {[type]} [description]
  160. */
  161. setupPositionAdjustPattern: function () {
  162. var pos = QRUtil.getPatternPosition(this.typeNumber);
  163. for (var i = 0; i < pos.length; i++) {
  164. for (var j = 0; j < pos.length; j++) {
  165. var row = pos[i];
  166. var col = pos[j];
  167. if (this.modules[row][col] != null) {
  168. continue;
  169. }
  170. for (var r = -2; r <= 2; r++) {
  171. for (var c = -2; c <= 2; c++) {
  172. if (r == -2 || r == 2 || c == -2 || c == 2 || (r == 0 && c == 0)) {
  173. this.modules[row + r][col + c] = true;
  174. } else {
  175. this.modules[row + r][col + c] = false;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. },
  182. /**
  183. * 设置版本信息(7以上版本才有)
  184. * @param {bool} test 是否处于判断最佳掩膜阶段
  185. * @return {[type]} [description]
  186. */
  187. setupTypeNumber: function (test) {
  188. var bits = QRUtil.getBCHTypeNumber(this.typeNumber);
  189. for (var i = 0; i < 18; i++) {
  190. var mod = (!test && ((bits >> i) & 1) == 1);
  191. this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod;
  192. this.modules[i % 3 + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod;
  193. }
  194. },
  195. /**
  196. * 设置格式信息(纠错等级和掩膜版本)
  197. * @param {bool} test
  198. * @param {num} maskPattern 掩膜版本
  199. * @return {}
  200. */
  201. setupTypeInfo: function (test, maskPattern) {
  202. var data = (QRErrorCorrectLevel[this.errorCorrectLevel] << 3) | maskPattern;
  203. var bits = QRUtil.getBCHTypeInfo(data);
  204. // vertical
  205. for (var i = 0; i < 15; i++) {
  206. var mod = (!test && ((bits >> i) & 1) == 1);
  207. if (i < 6) {
  208. this.modules[i][8] = mod;
  209. } else if (i < 8) {
  210. this.modules[i + 1][8] = mod;
  211. } else {
  212. this.modules[this.moduleCount - 15 + i][8] = mod;
  213. }
  214. // horizontal
  215. var mod = (!test && ((bits >> i) & 1) == 1);
  216. if (i < 8) {
  217. this.modules[8][this.moduleCount - i - 1] = mod;
  218. } else if (i < 9) {
  219. this.modules[8][15 - i - 1 + 1] = mod;
  220. } else {
  221. this.modules[8][15 - i - 1] = mod;
  222. }
  223. }
  224. // fixed module
  225. this.modules[this.moduleCount - 8][8] = (!test);
  226. },
  227. /**
  228. * 数据编码
  229. * @return {[type]} [description]
  230. */
  231. createData: function () {
  232. var buffer = new QRBitBuffer();
  233. var lengthBits = this.typeNumber > 9 ? 16 : 8;
  234. buffer.put(4, 4); //添加模式
  235. buffer.put(this.utf8bytes.length, lengthBits);
  236. for (var i = 0, l = this.utf8bytes.length; i < l; i++) {
  237. buffer.put(this.utf8bytes[i], 8);
  238. }
  239. if (buffer.length + 4 <= this.totalDataCount * 8) {
  240. buffer.put(0, 4);
  241. }
  242. // padding
  243. while (buffer.length % 8 != 0) {
  244. buffer.putBit(false);
  245. }
  246. // padding
  247. while (true) {
  248. if (buffer.length >= this.totalDataCount * 8) {
  249. break;
  250. }
  251. buffer.put(QRCodeAlg.PAD0, 8);
  252. if (buffer.length >= this.totalDataCount * 8) {
  253. break;
  254. }
  255. buffer.put(QRCodeAlg.PAD1, 8);
  256. }
  257. return this.createBytes(buffer);
  258. },
  259. /**
  260. * 纠错码编码
  261. * @param {buffer} buffer 数据编码
  262. * @return {[type]}
  263. */
  264. createBytes: function (buffer) {
  265. var offset = 0;
  266. var maxDcCount = 0;
  267. var maxEcCount = 0;
  268. var length = this.rsBlock.length / 3;
  269. var rsBlocks = new Array();
  270. for (var i = 0; i < length; i++) {
  271. var count = this.rsBlock[i * 3 + 0];
  272. var totalCount = this.rsBlock[i * 3 + 1];
  273. var dataCount = this.rsBlock[i * 3 + 2];
  274. for (var j = 0; j < count; j++) {
  275. rsBlocks.push([dataCount, totalCount]);
  276. }
  277. }
  278. var dcdata = new Array(rsBlocks.length);
  279. var ecdata = new Array(rsBlocks.length);
  280. for (var r = 0; r < rsBlocks.length; r++) {
  281. var dcCount = rsBlocks[r][0];
  282. var ecCount = rsBlocks[r][1] - dcCount;
  283. maxDcCount = Math.max(maxDcCount, dcCount);
  284. maxEcCount = Math.max(maxEcCount, ecCount);
  285. dcdata[r] = new Array(dcCount);
  286. for (var i = 0; i < dcdata[r].length; i++) {
  287. dcdata[r][i] = 0xff & buffer.buffer[i + offset];
  288. }
  289. offset += dcCount;
  290. var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount);
  291. var rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1);
  292. var modPoly = rawPoly.mod(rsPoly);
  293. ecdata[r] = new Array(rsPoly.getLength() - 1);
  294. for (var i = 0; i < ecdata[r].length; i++) {
  295. var modIndex = i + modPoly.getLength() - ecdata[r].length;
  296. ecdata[r][i] = (modIndex >= 0) ? modPoly.get(modIndex) : 0;
  297. }
  298. }
  299. var data = new Array(this.totalDataCount);
  300. var index = 0;
  301. for (var i = 0; i < maxDcCount; i++) {
  302. for (var r = 0; r < rsBlocks.length; r++) {
  303. if (i < dcdata[r].length) {
  304. data[index++] = dcdata[r][i];
  305. }
  306. }
  307. }
  308. for (var i = 0; i < maxEcCount; i++) {
  309. for (var r = 0; r < rsBlocks.length; r++) {
  310. if (i < ecdata[r].length) {
  311. data[index++] = ecdata[r][i];
  312. }
  313. }
  314. }
  315. return data;
  316. },
  317. /**
  318. * 布置模块,构建最终信息
  319. * @param {} data
  320. * @param {} maskPattern
  321. * @return {}
  322. */
  323. mapData: function (data, maskPattern) {
  324. var inc = -1;
  325. var row = this.moduleCount - 1;
  326. var bitIndex = 7;
  327. var byteIndex = 0;
  328. for (var col = this.moduleCount - 1; col > 0; col -= 2) {
  329. if (col == 6) col--;
  330. while (true) {
  331. for (var c = 0; c < 2; c++) {
  332. if (this.modules[row][col - c] == null) {
  333. var dark = false;
  334. if (byteIndex < data.length) {
  335. dark = (((data[byteIndex] >>> bitIndex) & 1) == 1);
  336. }
  337. var mask = QRUtil.getMask(maskPattern, row, col - c);
  338. if (mask) {
  339. dark = !dark;
  340. }
  341. this.modules[row][col - c] = dark;
  342. bitIndex--;
  343. if (bitIndex == -1) {
  344. byteIndex++;
  345. bitIndex = 7;
  346. }
  347. }
  348. }
  349. row += inc;
  350. if (row < 0 || this.moduleCount <= row) {
  351. row -= inc;
  352. inc = -inc;
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. };
  359. /**
  360. * 填充字段
  361. */
  362. QRCodeAlg.PAD0 = 0xEC;
  363. QRCodeAlg.PAD1 = 0x11;
  364. //---------------------------------------------------------------------
  365. // 纠错等级对应的编码
  366. //---------------------------------------------------------------------
  367. var QRErrorCorrectLevel = [1, 0, 3, 2];
  368. //---------------------------------------------------------------------
  369. // 掩膜版本
  370. //---------------------------------------------------------------------
  371. var QRMaskPattern = {
  372. PATTERN000: 0,
  373. PATTERN001: 1,
  374. PATTERN010: 2,
  375. PATTERN011: 3,
  376. PATTERN100: 4,
  377. PATTERN101: 5,
  378. PATTERN110: 6,
  379. PATTERN111: 7
  380. };
  381. //---------------------------------------------------------------------
  382. // 工具类
  383. //---------------------------------------------------------------------
  384. var QRUtil = {
  385. /*
  386. 每个版本矫正图形的位置
  387. */
  388. PATTERN_POSITION_TABLE: [
  389. [],
  390. [6, 18],
  391. [6, 22],
  392. [6, 26],
  393. [6, 30],
  394. [6, 34],
  395. [6, 22, 38],
  396. [6, 24, 42],
  397. [6, 26, 46],
  398. [6, 28, 50],
  399. [6, 30, 54],
  400. [6, 32, 58],
  401. [6, 34, 62],
  402. [6, 26, 46, 66],
  403. [6, 26, 48, 70],
  404. [6, 26, 50, 74],
  405. [6, 30, 54, 78],
  406. [6, 30, 56, 82],
  407. [6, 30, 58, 86],
  408. [6, 34, 62, 90],
  409. [6, 28, 50, 72, 94],
  410. [6, 26, 50, 74, 98],
  411. [6, 30, 54, 78, 102],
  412. [6, 28, 54, 80, 106],
  413. [6, 32, 58, 84, 110],
  414. [6, 30, 58, 86, 114],
  415. [6, 34, 62, 90, 118],
  416. [6, 26, 50, 74, 98, 122],
  417. [6, 30, 54, 78, 102, 126],
  418. [6, 26, 52, 78, 104, 130],
  419. [6, 30, 56, 82, 108, 134],
  420. [6, 34, 60, 86, 112, 138],
  421. [6, 30, 58, 86, 114, 142],
  422. [6, 34, 62, 90, 118, 146],
  423. [6, 30, 54, 78, 102, 126, 150],
  424. [6, 24, 50, 76, 102, 128, 154],
  425. [6, 28, 54, 80, 106, 132, 158],
  426. [6, 32, 58, 84, 110, 136, 162],
  427. [6, 26, 54, 82, 110, 138, 166],
  428. [6, 30, 58, 86, 114, 142, 170]
  429. ],
  430. G15: (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0),
  431. G18: (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0),
  432. G15_MASK: (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1),
  433. /*
  434. BCH编码格式信息
  435. */
  436. getBCHTypeInfo: function (data) {
  437. var d = data << 10;
  438. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) {
  439. d ^= (QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15)));
  440. }
  441. return ((data << 10) | d) ^ QRUtil.G15_MASK;
  442. },
  443. /*
  444. BCH编码版本信息
  445. */
  446. getBCHTypeNumber: function (data) {
  447. var d = data << 12;
  448. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) {
  449. d ^= (QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18)));
  450. }
  451. return (data << 12) | d;
  452. },
  453. /*
  454. 获取BCH位信息
  455. */
  456. getBCHDigit: function (data) {
  457. var digit = 0;
  458. while (data != 0) {
  459. digit++;
  460. data >>>= 1;
  461. }
  462. return digit;
  463. },
  464. /*
  465. 获取版本对应的矫正图形位置
  466. */
  467. getPatternPosition: function (typeNumber) {
  468. return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1];
  469. },
  470. /*
  471. 掩膜算法
  472. */
  473. getMask: function (maskPattern, i, j) {
  474. switch (maskPattern) {
  475. case QRMaskPattern.PATTERN000:
  476. return (i + j) % 2 == 0;
  477. case QRMaskPattern.PATTERN001:
  478. return i % 2 == 0;
  479. case QRMaskPattern.PATTERN010:
  480. return j % 3 == 0;
  481. case QRMaskPattern.PATTERN011:
  482. return (i + j) % 3 == 0;
  483. case QRMaskPattern.PATTERN100:
  484. return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0;
  485. case QRMaskPattern.PATTERN101:
  486. return (i * j) % 2 + (i * j) % 3 == 0;
  487. case QRMaskPattern.PATTERN110:
  488. return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
  489. case QRMaskPattern.PATTERN111:
  490. return ((i * j) % 3 + (i + j) % 2) % 2 == 0;
  491. default:
  492. throw new Error("bad maskPattern:" + maskPattern);
  493. }
  494. },
  495. /*
  496. 获取RS的纠错多项式
  497. */
  498. getErrorCorrectPolynomial: function (errorCorrectLength) {
  499. var a = new QRPolynomial([1], 0);
  500. for (var i = 0; i < errorCorrectLength; i++) {
  501. a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0));
  502. }
  503. return a;
  504. },
  505. /*
  506. 获取评价
  507. */
  508. getLostPoint: function (qrCode) {
  509. var moduleCount = qrCode.getModuleCount(),
  510. lostPoint = 0,
  511. darkCount = 0;
  512. for (var row = 0; row < moduleCount; row++) {
  513. var sameCount = 0;
  514. var head = qrCode.modules[row][0];
  515. for (var col = 0; col < moduleCount; col++) {
  516. var current = qrCode.modules[row][col];
  517. //level 3 评价
  518. if (col < moduleCount - 6) {
  519. if (current && !qrCode.modules[row][col + 1] && qrCode.modules[row][col + 2] && qrCode.modules[row][col + 3] && qrCode.modules[row][col + 4] && !qrCode.modules[row][col + 5] && qrCode.modules[row][col + 6]) {
  520. if (col < moduleCount - 10) {
  521. if (qrCode.modules[row][col + 7] && qrCode.modules[row][col + 8] && qrCode.modules[row][col + 9] && qrCode.modules[row][col + 10]) {
  522. lostPoint += 40;
  523. }
  524. } else if (col > 3) {
  525. if (qrCode.modules[row][col - 1] && qrCode.modules[row][col - 2] && qrCode.modules[row][col - 3] && qrCode.modules[row][col - 4]) {
  526. lostPoint += 40;
  527. }
  528. }
  529. }
  530. }
  531. //level 2 评价
  532. if ((row < moduleCount - 1) && (col < moduleCount - 1)) {
  533. var count = 0;
  534. if (current) count++;
  535. if (qrCode.modules[row + 1][col]) count++;
  536. if (qrCode.modules[row][col + 1]) count++;
  537. if (qrCode.modules[row + 1][col + 1]) count++;
  538. if (count == 0 || count == 4) {
  539. lostPoint += 3;
  540. }
  541. }
  542. //level 1 评价
  543. if (head ^ current) {
  544. sameCount++;
  545. } else {
  546. head = current;
  547. if (sameCount >= 5) {
  548. lostPoint += (3 + sameCount - 5);
  549. }
  550. sameCount = 1;
  551. }
  552. //level 4 评价
  553. if (current) {
  554. darkCount++;
  555. }
  556. }
  557. }
  558. for (var col = 0; col < moduleCount; col++) {
  559. var sameCount = 0;
  560. var head = qrCode.modules[0][col];
  561. for (var row = 0; row < moduleCount; row++) {
  562. var current = qrCode.modules[row][col];
  563. //level 3 评价
  564. if (row < moduleCount - 6) {
  565. if (current && !qrCode.modules[row + 1][col] && qrCode.modules[row + 2][col] && qrCode.modules[row + 3][col] && qrCode.modules[row + 4][col] && !qrCode.modules[row + 5][col] && qrCode.modules[row + 6][col]) {
  566. if (row < moduleCount - 10) {
  567. if (qrCode.modules[row + 7][col] && qrCode.modules[row + 8][col] && qrCode.modules[row + 9][col] && qrCode.modules[row + 10][col]) {
  568. lostPoint += 40;
  569. }
  570. } else if (row > 3) {
  571. if (qrCode.modules[row - 1][col] && qrCode.modules[row - 2][col] && qrCode.modules[row - 3][col] && qrCode.modules[row - 4][col]) {
  572. lostPoint += 40;
  573. }
  574. }
  575. }
  576. }
  577. //level 1 评价
  578. if (head ^ current) {
  579. sameCount++;
  580. } else {
  581. head = current;
  582. if (sameCount >= 5) {
  583. lostPoint += (3 + sameCount - 5);
  584. }
  585. sameCount = 1;
  586. }
  587. }
  588. }
  589. // LEVEL4
  590. var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5;
  591. lostPoint += ratio * 10;
  592. return lostPoint;
  593. }
  594. };
  595. //---------------------------------------------------------------------
  596. // QRMath使用的数学工具
  597. //---------------------------------------------------------------------
  598. var QRMath = {
  599. /*
  600. 将n转化为a^m
  601. */
  602. glog: function (n) {
  603. if (n < 1) {
  604. throw new Error("glog(" + n + ")");
  605. }
  606. return QRMath.LOG_TABLE[n];
  607. },
  608. /*
  609. 将a^m转化为n
  610. */
  611. gexp: function (n) {
  612. while (n < 0) {
  613. n += 255;
  614. }
  615. while (n >= 256) {
  616. n -= 255;
  617. }
  618. return QRMath.EXP_TABLE[n];
  619. },
  620. EXP_TABLE: new Array(256),
  621. LOG_TABLE: new Array(256)
  622. };
  623. for (var i = 0; i < 8; i++) {
  624. QRMath.EXP_TABLE[i] = 1 << i;
  625. }
  626. for (var i = 8; i < 256; i++) {
  627. QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath.EXP_TABLE[i - 8];
  628. }
  629. for (var i = 0; i < 255; i++) {
  630. QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
  631. }
  632. //---------------------------------------------------------------------
  633. // QRPolynomial 多项式
  634. //---------------------------------------------------------------------
  635. /**
  636. * 多项式类
  637. * @param {Array} num 系数
  638. * @param {num} shift a^shift
  639. */
  640. function QRPolynomial(num, shift) {
  641. if (num.length == undefined) {
  642. throw new Error(num.length + "/" + shift);
  643. }
  644. var offset = 0;
  645. while (offset < num.length && num[offset] == 0) {
  646. offset++;
  647. }
  648. this.num = new Array(num.length - offset + shift);
  649. for (var i = 0; i < num.length - offset; i++) {
  650. this.num[i] = num[i + offset];
  651. }
  652. }
  653. QRPolynomial.prototype = {
  654. get: function (index) {
  655. return this.num[index];
  656. },
  657. getLength: function () {
  658. return this.num.length;
  659. },
  660. /**
  661. * 多项式乘法
  662. * @param {QRPolynomial} e 被乘多项式
  663. * @return {[type]} [description]
  664. */
  665. multiply: function (e) {
  666. var num = new Array(this.getLength() + e.getLength() - 1);
  667. for (var i = 0; i < this.getLength(); i++) {
  668. for (var j = 0; j < e.getLength(); j++) {
  669. num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j)));
  670. }
  671. }
  672. return new QRPolynomial(num, 0);
  673. },
  674. /**
  675. * 多项式模运算
  676. * @param {QRPolynomial} e 模多项式
  677. * @return {}
  678. */
  679. mod: function (e) {
  680. var tl = this.getLength(),
  681. el = e.getLength();
  682. if (tl - el < 0) {
  683. return this;
  684. }
  685. var num = new Array(tl);
  686. for (var i = 0; i < tl; i++) {
  687. num[i] = this.get(i);
  688. }
  689. while (num.length >= el) {
  690. var ratio = QRMath.glog(num[0]) - QRMath.glog(e.get(0));
  691. for (var i = 0; i < e.getLength(); i++) {
  692. num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio);
  693. }
  694. while (num[0] == 0) {
  695. num.shift();
  696. }
  697. }
  698. return new QRPolynomial(num, 0);
  699. }
  700. };
  701. //---------------------------------------------------------------------
  702. // RS_BLOCK_TABLE
  703. //---------------------------------------------------------------------
  704. /*
  705. 二维码各个版本信息[块数, 每块中的数据块数, 每块中的信息块数]
  706. */
  707. var RS_BLOCK_TABLE = [
  708. // L
  709. // M
  710. // Q
  711. // H
  712. // 1
  713. [1, 26, 19],
  714. [1, 26, 16],
  715. [1, 26, 13],
  716. [1, 26, 9],
  717. // 2
  718. [1, 44, 34],
  719. [1, 44, 28],
  720. [1, 44, 22],
  721. [1, 44, 16],
  722. // 3
  723. [1, 70, 55],
  724. [1, 70, 44],
  725. [2, 35, 17],
  726. [2, 35, 13],
  727. // 4
  728. [1, 100, 80],
  729. [2, 50, 32],
  730. [2, 50, 24],
  731. [4, 25, 9],
  732. // 5
  733. [1, 134, 108],
  734. [2, 67, 43],
  735. [2, 33, 15, 2, 34, 16],
  736. [2, 33, 11, 2, 34, 12],
  737. // 6
  738. [2, 86, 68],
  739. [4, 43, 27],
  740. [4, 43, 19],
  741. [4, 43, 15],
  742. // 7
  743. [2, 98, 78],
  744. [4, 49, 31],
  745. [2, 32, 14, 4, 33, 15],
  746. [4, 39, 13, 1, 40, 14],
  747. // 8
  748. [2, 121, 97],
  749. [2, 60, 38, 2, 61, 39],
  750. [4, 40, 18, 2, 41, 19],
  751. [4, 40, 14, 2, 41, 15],
  752. // 9
  753. [2, 146, 116],
  754. [3, 58, 36, 2, 59, 37],
  755. [4, 36, 16, 4, 37, 17],
  756. [4, 36, 12, 4, 37, 13],
  757. // 10
  758. [2, 86, 68, 2, 87, 69],
  759. [4, 69, 43, 1, 70, 44],
  760. [6, 43, 19, 2, 44, 20],
  761. [6, 43, 15, 2, 44, 16],
  762. // 11
  763. [4, 101, 81],
  764. [1, 80, 50, 4, 81, 51],
  765. [4, 50, 22, 4, 51, 23],
  766. [3, 36, 12, 8, 37, 13],
  767. // 12
  768. [2, 116, 92, 2, 117, 93],
  769. [6, 58, 36, 2, 59, 37],
  770. [4, 46, 20, 6, 47, 21],
  771. [7, 42, 14, 4, 43, 15],
  772. // 13
  773. [4, 133, 107],
  774. [8, 59, 37, 1, 60, 38],
  775. [8, 44, 20, 4, 45, 21],
  776. [12, 33, 11, 4, 34, 12],
  777. // 14
  778. [3, 145, 115, 1, 146, 116],
  779. [4, 64, 40, 5, 65, 41],
  780. [11, 36, 16, 5, 37, 17],
  781. [11, 36, 12, 5, 37, 13],
  782. // 15
  783. [5, 109, 87, 1, 110, 88],
  784. [5, 65, 41, 5, 66, 42],
  785. [5, 54, 24, 7, 55, 25],
  786. [11, 36, 12],
  787. // 16
  788. [5, 122, 98, 1, 123, 99],
  789. [7, 73, 45, 3, 74, 46],
  790. [15, 43, 19, 2, 44, 20],
  791. [3, 45, 15, 13, 46, 16],
  792. // 17
  793. [1, 135, 107, 5, 136, 108],
  794. [10, 74, 46, 1, 75, 47],
  795. [1, 50, 22, 15, 51, 23],
  796. [2, 42, 14, 17, 43, 15],
  797. // 18
  798. [5, 150, 120, 1, 151, 121],
  799. [9, 69, 43, 4, 70, 44],
  800. [17, 50, 22, 1, 51, 23],
  801. [2, 42, 14, 19, 43, 15],
  802. // 19
  803. [3, 141, 113, 4, 142, 114],
  804. [3, 70, 44, 11, 71, 45],
  805. [17, 47, 21, 4, 48, 22],
  806. [9, 39, 13, 16, 40, 14],
  807. // 20
  808. [3, 135, 107, 5, 136, 108],
  809. [3, 67, 41, 13, 68, 42],
  810. [15, 54, 24, 5, 55, 25],
  811. [15, 43, 15, 10, 44, 16],
  812. // 21
  813. [4, 144, 116, 4, 145, 117],
  814. [17, 68, 42],
  815. [17, 50, 22, 6, 51, 23],
  816. [19, 46, 16, 6, 47, 17],
  817. // 22
  818. [2, 139, 111, 7, 140, 112],
  819. [17, 74, 46],
  820. [7, 54, 24, 16, 55, 25],
  821. [34, 37, 13],
  822. // 23
  823. [4, 151, 121, 5, 152, 122],
  824. [4, 75, 47, 14, 76, 48],
  825. [11, 54, 24, 14, 55, 25],
  826. [16, 45, 15, 14, 46, 16],
  827. // 24
  828. [6, 147, 117, 4, 148, 118],
  829. [6, 73, 45, 14, 74, 46],
  830. [11, 54, 24, 16, 55, 25],
  831. [30, 46, 16, 2, 47, 17],
  832. // 25
  833. [8, 132, 106, 4, 133, 107],
  834. [8, 75, 47, 13, 76, 48],
  835. [7, 54, 24, 22, 55, 25],
  836. [22, 45, 15, 13, 46, 16],
  837. // 26
  838. [10, 142, 114, 2, 143, 115],
  839. [19, 74, 46, 4, 75, 47],
  840. [28, 50, 22, 6, 51, 23],
  841. [33, 46, 16, 4, 47, 17],
  842. // 27
  843. [8, 152, 122, 4, 153, 123],
  844. [22, 73, 45, 3, 74, 46],
  845. [8, 53, 23, 26, 54, 24],
  846. [12, 45, 15, 28, 46, 16],
  847. // 28
  848. [3, 147, 117, 10, 148, 118],
  849. [3, 73, 45, 23, 74, 46],
  850. [4, 54, 24, 31, 55, 25],
  851. [11, 45, 15, 31, 46, 16],
  852. // 29
  853. [7, 146, 116, 7, 147, 117],
  854. [21, 73, 45, 7, 74, 46],
  855. [1, 53, 23, 37, 54, 24],
  856. [19, 45, 15, 26, 46, 16],
  857. // 30
  858. [5, 145, 115, 10, 146, 116],
  859. [19, 75, 47, 10, 76, 48],
  860. [15, 54, 24, 25, 55, 25],
  861. [23, 45, 15, 25, 46, 16],
  862. // 31
  863. [13, 145, 115, 3, 146, 116],
  864. [2, 74, 46, 29, 75, 47],
  865. [42, 54, 24, 1, 55, 25],
  866. [23, 45, 15, 28, 46, 16],
  867. // 32
  868. [17, 145, 115],
  869. [10, 74, 46, 23, 75, 47],
  870. [10, 54, 24, 35, 55, 25],
  871. [19, 45, 15, 35, 46, 16],
  872. // 33
  873. [17, 145, 115, 1, 146, 116],
  874. [14, 74, 46, 21, 75, 47],
  875. [29, 54, 24, 19, 55, 25],
  876. [11, 45, 15, 46, 46, 16],
  877. // 34
  878. [13, 145, 115, 6, 146, 116],
  879. [14, 74, 46, 23, 75, 47],
  880. [44, 54, 24, 7, 55, 25],
  881. [59, 46, 16, 1, 47, 17],
  882. // 35
  883. [12, 151, 121, 7, 152, 122],
  884. [12, 75, 47, 26, 76, 48],
  885. [39, 54, 24, 14, 55, 25],
  886. [22, 45, 15, 41, 46, 16],
  887. // 36
  888. [6, 151, 121, 14, 152, 122],
  889. [6, 75, 47, 34, 76, 48],
  890. [46, 54, 24, 10, 55, 25],
  891. [2, 45, 15, 64, 46, 16],
  892. // 37
  893. [17, 152, 122, 4, 153, 123],
  894. [29, 74, 46, 14, 75, 47],
  895. [49, 54, 24, 10, 55, 25],
  896. [24, 45, 15, 46, 46, 16],
  897. // 38
  898. [4, 152, 122, 18, 153, 123],
  899. [13, 74, 46, 32, 75, 47],
  900. [48, 54, 24, 14, 55, 25],
  901. [42, 45, 15, 32, 46, 16],
  902. // 39
  903. [20, 147, 117, 4, 148, 118],
  904. [40, 75, 47, 7, 76, 48],
  905. [43, 54, 24, 22, 55, 25],
  906. [10, 45, 15, 67, 46, 16],
  907. // 40
  908. [19, 148, 118, 6, 149, 119],
  909. [18, 75, 47, 31, 76, 48],
  910. [34, 54, 24, 34, 55, 25],
  911. [20, 45, 15, 61, 46, 16]
  912. ];
  913. /**
  914. * 根据数据获取对应版本
  915. * @return {[type]} [description]
  916. */
  917. QRCodeAlg.prototype.getRightType = function () {
  918. for (var typeNumber = 1; typeNumber < 41; typeNumber++) {
  919. var rsBlock = RS_BLOCK_TABLE[(typeNumber - 1) * 4 + this.errorCorrectLevel];
  920. if (rsBlock == undefined) {
  921. throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" + this.errorCorrectLevel);
  922. }
  923. var length = rsBlock.length / 3;
  924. var totalDataCount = 0;
  925. for (var i = 0; i < length; i++) {
  926. var count = rsBlock[i * 3 + 0];
  927. var dataCount = rsBlock[i * 3 + 2];
  928. totalDataCount += dataCount * count;
  929. }
  930. var lengthBytes = typeNumber > 9 ? 2 : 1;
  931. if (this.utf8bytes.length + lengthBytes < totalDataCount || typeNumber == 40) {
  932. this.typeNumber = typeNumber;
  933. this.rsBlock = rsBlock;
  934. this.totalDataCount = totalDataCount;
  935. break;
  936. }
  937. }
  938. };
  939. //---------------------------------------------------------------------
  940. // QRBitBuffer
  941. //---------------------------------------------------------------------
  942. function QRBitBuffer() {
  943. this.buffer = new Array();
  944. this.length = 0;
  945. }
  946. QRBitBuffer.prototype = {
  947. get: function (index) {
  948. var bufIndex = Math.floor(index / 8);
  949. return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1);
  950. },
  951. put: function (num, length) {
  952. for (var i = 0; i < length; i++) {
  953. this.putBit(((num >>> (length - i - 1)) & 1));
  954. }
  955. },
  956. putBit: function (bit) {
  957. var bufIndex = Math.floor(this.length / 8);
  958. if (this.buffer.length <= bufIndex) {
  959. this.buffer.push(0);
  960. }
  961. if (bit) {
  962. this.buffer[bufIndex] |= (0x80 >>> (this.length % 8));
  963. }
  964. this.length++;
  965. }
  966. };
  967. // xzedit
  968. let qrcodeAlgObjCache = [];
  969. /**
  970. * 二维码构造函数,主要用于绘制
  971. * @param {参数列表} opt 传递参数
  972. * @return {}
  973. */
  974. var options;
  975. QRCode = function (opt) {
  976. //设置默认参数
  977. options = {
  978. text: '',
  979. size: 256,
  980. correctLevel: 3,
  981. background: '#ffffff',
  982. foreground: '#000000',
  983. pdground: '#000000',
  984. image: '',
  985. imageSize: 30,
  986. canvasId: opt.canvasId,
  987. context: opt.context,
  988. usingComponents: opt.usingComponents,
  989. showLoading: opt.showLoading,
  990. loadingText: opt.loadingText,
  991. };
  992. if (typeof opt === 'string') { // 只编码ASCII字符串
  993. opt = {
  994. text: opt
  995. };
  996. }
  997. if (opt) {
  998. for (var i in opt) {
  999. options[i] = opt[i];
  1000. }
  1001. }
  1002. //使用QRCodeAlg创建二维码结构
  1003. var qrCodeAlg = null;
  1004. for (var i = 0, l = qrcodeAlgObjCache.length; i < l; i++) {
  1005. if (qrcodeAlgObjCache[i].text == options.text && qrcodeAlgObjCache[i].text.correctLevel == options.correctLevel) {
  1006. qrCodeAlg = qrcodeAlgObjCache[i].obj;
  1007. break;
  1008. }
  1009. }
  1010. if (i == l) {
  1011. qrCodeAlg = new QRCodeAlg(options.text, options.correctLevel);
  1012. qrcodeAlgObjCache.push({
  1013. text: options.text,
  1014. correctLevel: options.correctLevel,
  1015. obj: qrCodeAlg
  1016. });
  1017. }
  1018. /**
  1019. * 计算矩阵点的前景色
  1020. * @param {Obj} config
  1021. * @param {Number} config.row 点x坐标
  1022. * @param {Number} config.col 点y坐标
  1023. * @param {Number} config.count 矩阵大小
  1024. * @param {Number} config.options 组件的options
  1025. * @return {String}
  1026. */
  1027. let getForeGround = function (config) {
  1028. var options = config.options;
  1029. if (options.pdground && (
  1030. (config.row > 1 && config.row < 5 && config.col > 1 && config.col < 5) ||
  1031. (config.row > (config.count - 6) && config.row < (config.count - 2) && config.col > 1 && config.col < 5) ||
  1032. (config.row > 1 && config.row < 5 && config.col > (config.count - 6) && config.col < (config.count - 2))
  1033. )) {
  1034. return options.pdground;
  1035. }
  1036. return options.foreground;
  1037. }
  1038. // 创建canvas
  1039. let createCanvas = function (options) {
  1040. if (options.showLoading) {
  1041. wx.showLoading({
  1042. title: options.loadingText,
  1043. mask: true
  1044. });
  1045. }
  1046. var ctx = wx.createCanvasContext(options.canvasId, options.context);
  1047. var count = qrCodeAlg.getModuleCount();
  1048. var ratioSize = options.size;
  1049. var ratioImgSize = options.imageSize;
  1050. //计算每个点的长宽
  1051. var tileW = (ratioSize / count).toPrecision(4);
  1052. var tileH = (ratioSize / count).toPrecision(4);
  1053. //绘制
  1054. for (var row = 0; row < count; row++) {
  1055. for (var col = 0; col < count; col++) {
  1056. var w = (Math.ceil((col + 1) * tileW) - Math.floor(col * tileW));
  1057. var h = (Math.ceil((row + 1) * tileW) - Math.floor(row * tileW));
  1058. var foreground = getForeGround({
  1059. row: row,
  1060. col: col,
  1061. count: count,
  1062. options: options
  1063. });
  1064. ctx.setFillStyle(qrCodeAlg.modules[row][col] ? foreground : options.background);
  1065. ctx.fillRect(Math.round(col * tileW), Math.round(row * tileH), w, h);
  1066. }
  1067. }
  1068. if (options.image) {
  1069. var x = Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
  1070. var y = Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
  1071. drawRoundedRect(ctx, x, y, ratioImgSize, ratioImgSize, 2, 6, true, true)
  1072. ctx.drawImage(options.image, x, y, ratioImgSize, ratioImgSize);
  1073. // 画圆角矩形
  1074. function drawRoundedRect(ctxi, x, y, width, height, r, lineWidth, fill, stroke) {
  1075. ctxi.setLineWidth(lineWidth);
  1076. ctxi.setFillStyle(options.background);
  1077. ctxi.setStrokeStyle(options.background);
  1078. ctxi.beginPath(); // draw top and top right corner
  1079. ctxi.moveTo(x + r, y);
  1080. ctxi.arcTo(x + width, y, x + width, y + r, r); // draw right side and bottom right corner
  1081. ctxi.arcTo(x + width, y + height, x + width - r, y + height, r); // draw bottom and bottom left corner
  1082. ctxi.arcTo(x, y + height, x, y + height - r, r); // draw left and top left corner
  1083. ctxi.arcTo(x, y, x + r, y, r);
  1084. ctxi.closePath();
  1085. if (fill) {
  1086. ctxi.fill();
  1087. }
  1088. if (stroke) {
  1089. ctxi.stroke();
  1090. }
  1091. }
  1092. }
  1093. setTimeout(() => {
  1094. ctx.draw(true, () => {
  1095. // 保存到临时区域
  1096. setTimeout(() => {
  1097. wx.canvasToTempFilePath({
  1098. width: options.width,
  1099. height: options.height,
  1100. destWidth: options.width,
  1101. destHeight: options.height,
  1102. canvasId: options.canvasId,
  1103. quality: Number(1),
  1104. success: function (res) {
  1105. if (options.cbResult) {
  1106. options.cbResult(res.tempFilePath)
  1107. }
  1108. },
  1109. fail: function (res) {
  1110. if (options.cbResult) {
  1111. options.cbResult(res)
  1112. }
  1113. },
  1114. complete: function () {
  1115. if (options.showLoading){
  1116. wx.hideLoading();
  1117. }
  1118. },
  1119. }, options.context);
  1120. }, options.text.length + 100);
  1121. });
  1122. }, options.usingComponents ? 0 : 150);
  1123. }
  1124. createCanvas(options);
  1125. // 空判定
  1126. let empty = function (v) {
  1127. let tp = typeof v,
  1128. rt = false;
  1129. if (tp == "number" && String(v) == "") {
  1130. rt = true
  1131. } else if (tp == "undefined") {
  1132. rt = true
  1133. } else if (tp == "object") {
  1134. if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
  1135. } else if (tp == "string") {
  1136. if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
  1137. } else if (tp == "function") {
  1138. rt = false
  1139. }
  1140. return rt
  1141. }
  1142. };
  1143. QRCode.prototype.clear = function (fn) {
  1144. var ctx = wx.createCanvasContext(options.canvasId, options.context)
  1145. ctx.clearRect(0, 0, options.size, options.size)
  1146. ctx.draw(false, () => {
  1147. if (fn) {
  1148. fn()
  1149. }
  1150. })
  1151. };
  1152. })()
  1153. export default QRCode