qrcode.js 42 KB

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