qrcode.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. let QRCode = {};
  2. (function() {
  3. /**
  4. * 获取单个字符的utf8编码
  5. * unicode BMP平面约65535个字符
  6. * @param {num} code
  7. * return {array}
  8. */
  9. function unicodeFormat8(code) {
  10. // 1 byte
  11. var c0, c1, c2;
  12. if (code < 128) {
  13. return [code];
  14. // 2 bytes
  15. } else if (code < 2048) {
  16. c0 = 192 + (code >> 6);
  17. c1 = 128 + (code & 63);
  18. return [c0, c1];
  19. // 3 bytes
  20. } else {
  21. c0 = 224 + (code >> 12);
  22. c1 = 128 + (code >> 6 & 63);
  23. c2 = 128 + (code & 63);
  24. return [c0, c1, c2];
  25. }
  26. }
  27. /**
  28. * 获取字符串的utf8编码字节串
  29. * @param {string} string
  30. * @return {array}
  31. */
  32. function getUTF8Bytes(string) {
  33. var utf8codes = [];
  34. for (var i = 0; i < string.length; i++) {
  35. var code = string.charCodeAt(i);
  36. var utf8 = unicodeFormat8(code);
  37. for (var j = 0; j < utf8.length; j++) {
  38. utf8codes.push(utf8[j]);
  39. }
  40. }
  41. return utf8codes;
  42. }
  43. /**
  44. * 二维码算法实现
  45. * @param {string} data 要编码的信息字符串
  46. * @param {num} errorCorrectLevel 纠错等级
  47. */
  48. function QRCodeAlg(data, errorCorrectLevel) {
  49. this.typeNumber = -1; //版本
  50. this.errorCorrectLevel = errorCorrectLevel;
  51. this.modules = null; //二维矩阵,存放最终结果
  52. this.moduleCount = 0; //矩阵大小
  53. this.dataCache = null; //数据缓存
  54. this.rsBlocks = null; //版本数据信息
  55. this.totalDataCount = -1; //可使用的数据量
  56. this.data = data;
  57. this.utf8bytes = getUTF8Bytes(data);
  58. this.make();
  59. }
  60. QRCodeAlg.prototype = {
  61. constructor: QRCodeAlg,
  62. /**
  63. * 获取二维码矩阵大小
  64. * @return {num} 矩阵大小
  65. */
  66. getModuleCount: function() {
  67. return this.moduleCount;
  68. },
  69. /**
  70. * 编码
  71. */
  72. make: function() {
  73. this.getRightType();
  74. this.dataCache = this.createData();
  75. this.createQrcode();
  76. },
  77. /**
  78. * 设置二位矩阵功能图形
  79. * @param {bool} test 表示是否在寻找最好掩膜阶段
  80. * @param {num} maskPattern 掩膜的版本
  81. */
  82. makeImpl: function(maskPattern) {
  83. this.moduleCount = this.typeNumber * 4 + 17;
  84. this.modules = new Array(this.moduleCount);
  85. for (var row = 0; row < this.moduleCount; row++) {
  86. this.modules[row] = new Array(this.moduleCount);
  87. }
  88. this.setupPositionProbePattern(0, 0);
  89. this.setupPositionProbePattern(this.moduleCount - 7, 0);
  90. this.setupPositionProbePattern(0, this.moduleCount - 7);
  91. this.setupPositionAdjustPattern();
  92. this.setupTimingPattern();
  93. this.setupTypeInfo(true, maskPattern);
  94. if (this.typeNumber >= 7) {
  95. this.setupTypeNumber(true);
  96. }
  97. this.mapData(this.dataCache, maskPattern);
  98. },
  99. /**
  100. * 设置二维码的位置探测图形
  101. * @param {num} row 探测图形的中心横坐标
  102. * @param {num} col 探测图形的中心纵坐标
  103. */
  104. setupPositionProbePattern: function(row, col) {
  105. for (var r = -1; r <= 7; r++) {
  106. if (row + r <= -1 || this.moduleCount <= row + r) continue;
  107. for (var c = -1; c <= 7; c++) {
  108. if (col + c <= -1 || this.moduleCount <= col + c) continue;
  109. if ((0 <= r && r <= 6 && (c == 0 || c == 6)) || (0 <= c && c <= 6 && (r == 0 || r ==
  110. 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] &&
  520. qrCode.modules[row][col + 3] && qrCode.modules[row][col + 4] && !qrCode.modules[
  521. row][col + 5] && qrCode.modules[row][col + 6]) {
  522. if (col < moduleCount - 10) {
  523. if (qrCode.modules[row][col + 7] && qrCode.modules[row][col + 8] && qrCode
  524. .modules[row][col + 9] && qrCode.modules[row][col + 10]) {
  525. lostPoint += 40;
  526. }
  527. } else if (col > 3) {
  528. if (qrCode.modules[row][col - 1] && qrCode.modules[row][col - 2] && qrCode
  529. .modules[row][col - 3] && qrCode.modules[row][col - 4]) {
  530. lostPoint += 40;
  531. }
  532. }
  533. }
  534. }
  535. //level 2 评价
  536. if ((row < moduleCount - 1) && (col < moduleCount - 1)) {
  537. var count = 0;
  538. if (current) count++;
  539. if (qrCode.modules[row + 1][col]) count++;
  540. if (qrCode.modules[row][col + 1]) count++;
  541. if (qrCode.modules[row + 1][col + 1]) count++;
  542. if (count == 0 || count == 4) {
  543. lostPoint += 3;
  544. }
  545. }
  546. //level 1 评价
  547. if (head ^ current) {
  548. sameCount++;
  549. } else {
  550. head = current;
  551. if (sameCount >= 5) {
  552. lostPoint += (3 + sameCount - 5);
  553. }
  554. sameCount = 1;
  555. }
  556. //level 4 评价
  557. if (current) {
  558. darkCount++;
  559. }
  560. }
  561. }
  562. for (var col = 0; col < moduleCount; col++) {
  563. var sameCount = 0;
  564. var head = qrCode.modules[0][col];
  565. for (var row = 0; row < moduleCount; row++) {
  566. var current = qrCode.modules[row][col];
  567. //level 3 评价
  568. if (row < moduleCount - 6) {
  569. if (current && !qrCode.modules[row + 1][col] && qrCode.modules[row + 2][col] &&
  570. qrCode.modules[row + 3][col] && qrCode.modules[row + 4][col] && !qrCode.modules[
  571. row + 5][col] && qrCode.modules[row + 6][col]) {
  572. if (row < moduleCount - 10) {
  573. if (qrCode.modules[row + 7][col] && qrCode.modules[row + 8][col] && qrCode
  574. .modules[row + 9][col] && qrCode.modules[row + 10][col]) {
  575. lostPoint += 40;
  576. }
  577. } else if (row > 3) {
  578. if (qrCode.modules[row - 1][col] && qrCode.modules[row - 2][col] && qrCode
  579. .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
  636. .EXP_TABLE[i - 8];
  637. }
  638. for (var i = 0; i < 255; i++) {
  639. QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
  640. }
  641. //---------------------------------------------------------------------
  642. // QRPolynomial 多项式
  643. //---------------------------------------------------------------------
  644. /**
  645. * 多项式类
  646. * @param {Array} num 系数
  647. * @param {num} shift a^shift
  648. */
  649. function QRPolynomial(num, shift) {
  650. if (num.length == undefined) {
  651. throw new Error(num.length + "/" + shift);
  652. }
  653. var offset = 0;
  654. while (offset < num.length && num[offset] == 0) {
  655. offset++;
  656. }
  657. this.num = new Array(num.length - offset + shift);
  658. for (var i = 0; i < num.length - offset; i++) {
  659. this.num[i] = num[i + offset];
  660. }
  661. }
  662. QRPolynomial.prototype = {
  663. get: function(index) {
  664. return this.num[index];
  665. },
  666. getLength: function() {
  667. return this.num.length;
  668. },
  669. /**
  670. * 多项式乘法
  671. * @param {QRPolynomial} e 被乘多项式
  672. * @return {[type]} [description]
  673. */
  674. multiply: function(e) {
  675. var num = new Array(this.getLength() + e.getLength() - 1);
  676. for (var i = 0; i < this.getLength(); i++) {
  677. for (var j = 0; j < e.getLength(); j++) {
  678. num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j)));
  679. }
  680. }
  681. return new QRPolynomial(num, 0);
  682. },
  683. /**
  684. * 多项式模运算
  685. * @param {QRPolynomial} e 模多项式
  686. * @return {}
  687. */
  688. mod: function(e) {
  689. var tl = this.getLength(),
  690. el = e.getLength();
  691. if (tl - el < 0) {
  692. return this;
  693. }
  694. var num = new Array(tl);
  695. for (var i = 0; i < tl; i++) {
  696. num[i] = this.get(i);
  697. }
  698. while (num.length >= el) {
  699. var ratio = QRMath.glog(num[0]) - QRMath.glog(e.get(0));
  700. for (var i = 0; i < e.getLength(); i++) {
  701. num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio);
  702. }
  703. while (num[0] == 0) {
  704. num.shift();
  705. }
  706. }
  707. return new QRPolynomial(num, 0);
  708. }
  709. };
  710. //---------------------------------------------------------------------
  711. // RS_BLOCK_TABLE
  712. //---------------------------------------------------------------------
  713. /*
  714. 二维码各个版本信息[块数, 每块中的数据块数, 每块中的信息块数]
  715. */
  716. var RS_BLOCK_TABLE = [
  717. // L
  718. // M
  719. // Q
  720. // H
  721. // 1
  722. [1, 26, 19],
  723. [1, 26, 16],
  724. [1, 26, 13],
  725. [1, 26, 9],
  726. // 2
  727. [1, 44, 34],
  728. [1, 44, 28],
  729. [1, 44, 22],
  730. [1, 44, 16],
  731. // 3
  732. [1, 70, 55],
  733. [1, 70, 44],
  734. [2, 35, 17],
  735. [2, 35, 13],
  736. // 4
  737. [1, 100, 80],
  738. [2, 50, 32],
  739. [2, 50, 24],
  740. [4, 25, 9],
  741. // 5
  742. [1, 134, 108],
  743. [2, 67, 43],
  744. [2, 33, 15, 2, 34, 16],
  745. [2, 33, 11, 2, 34, 12],
  746. // 6
  747. [2, 86, 68],
  748. [4, 43, 27],
  749. [4, 43, 19],
  750. [4, 43, 15],
  751. // 7
  752. [2, 98, 78],
  753. [4, 49, 31],
  754. [2, 32, 14, 4, 33, 15],
  755. [4, 39, 13, 1, 40, 14],
  756. // 8
  757. [2, 121, 97],
  758. [2, 60, 38, 2, 61, 39],
  759. [4, 40, 18, 2, 41, 19],
  760. [4, 40, 14, 2, 41, 15],
  761. // 9
  762. [2, 146, 116],
  763. [3, 58, 36, 2, 59, 37],
  764. [4, 36, 16, 4, 37, 17],
  765. [4, 36, 12, 4, 37, 13],
  766. // 10
  767. [2, 86, 68, 2, 87, 69],
  768. [4, 69, 43, 1, 70, 44],
  769. [6, 43, 19, 2, 44, 20],
  770. [6, 43, 15, 2, 44, 16],
  771. // 11
  772. [4, 101, 81],
  773. [1, 80, 50, 4, 81, 51],
  774. [4, 50, 22, 4, 51, 23],
  775. [3, 36, 12, 8, 37, 13],
  776. // 12
  777. [2, 116, 92, 2, 117, 93],
  778. [6, 58, 36, 2, 59, 37],
  779. [4, 46, 20, 6, 47, 21],
  780. [7, 42, 14, 4, 43, 15],
  781. // 13
  782. [4, 133, 107],
  783. [8, 59, 37, 1, 60, 38],
  784. [8, 44, 20, 4, 45, 21],
  785. [12, 33, 11, 4, 34, 12],
  786. // 14
  787. [3, 145, 115, 1, 146, 116],
  788. [4, 64, 40, 5, 65, 41],
  789. [11, 36, 16, 5, 37, 17],
  790. [11, 36, 12, 5, 37, 13],
  791. // 15
  792. [5, 109, 87, 1, 110, 88],
  793. [5, 65, 41, 5, 66, 42],
  794. [5, 54, 24, 7, 55, 25],
  795. [11, 36, 12],
  796. // 16
  797. [5, 122, 98, 1, 123, 99],
  798. [7, 73, 45, 3, 74, 46],
  799. [15, 43, 19, 2, 44, 20],
  800. [3, 45, 15, 13, 46, 16],
  801. // 17
  802. [1, 135, 107, 5, 136, 108],
  803. [10, 74, 46, 1, 75, 47],
  804. [1, 50, 22, 15, 51, 23],
  805. [2, 42, 14, 17, 43, 15],
  806. // 18
  807. [5, 150, 120, 1, 151, 121],
  808. [9, 69, 43, 4, 70, 44],
  809. [17, 50, 22, 1, 51, 23],
  810. [2, 42, 14, 19, 43, 15],
  811. // 19
  812. [3, 141, 113, 4, 142, 114],
  813. [3, 70, 44, 11, 71, 45],
  814. [17, 47, 21, 4, 48, 22],
  815. [9, 39, 13, 16, 40, 14],
  816. // 20
  817. [3, 135, 107, 5, 136, 108],
  818. [3, 67, 41, 13, 68, 42],
  819. [15, 54, 24, 5, 55, 25],
  820. [15, 43, 15, 10, 44, 16],
  821. // 21
  822. [4, 144, 116, 4, 145, 117],
  823. [17, 68, 42],
  824. [17, 50, 22, 6, 51, 23],
  825. [19, 46, 16, 6, 47, 17],
  826. // 22
  827. [2, 139, 111, 7, 140, 112],
  828. [17, 74, 46],
  829. [7, 54, 24, 16, 55, 25],
  830. [34, 37, 13],
  831. // 23
  832. [4, 151, 121, 5, 152, 122],
  833. [4, 75, 47, 14, 76, 48],
  834. [11, 54, 24, 14, 55, 25],
  835. [16, 45, 15, 14, 46, 16],
  836. // 24
  837. [6, 147, 117, 4, 148, 118],
  838. [6, 73, 45, 14, 74, 46],
  839. [11, 54, 24, 16, 55, 25],
  840. [30, 46, 16, 2, 47, 17],
  841. // 25
  842. [8, 132, 106, 4, 133, 107],
  843. [8, 75, 47, 13, 76, 48],
  844. [7, 54, 24, 22, 55, 25],
  845. [22, 45, 15, 13, 46, 16],
  846. // 26
  847. [10, 142, 114, 2, 143, 115],
  848. [19, 74, 46, 4, 75, 47],
  849. [28, 50, 22, 6, 51, 23],
  850. [33, 46, 16, 4, 47, 17],
  851. // 27
  852. [8, 152, 122, 4, 153, 123],
  853. [22, 73, 45, 3, 74, 46],
  854. [8, 53, 23, 26, 54, 24],
  855. [12, 45, 15, 28, 46, 16],
  856. // 28
  857. [3, 147, 117, 10, 148, 118],
  858. [3, 73, 45, 23, 74, 46],
  859. [4, 54, 24, 31, 55, 25],
  860. [11, 45, 15, 31, 46, 16],
  861. // 29
  862. [7, 146, 116, 7, 147, 117],
  863. [21, 73, 45, 7, 74, 46],
  864. [1, 53, 23, 37, 54, 24],
  865. [19, 45, 15, 26, 46, 16],
  866. // 30
  867. [5, 145, 115, 10, 146, 116],
  868. [19, 75, 47, 10, 76, 48],
  869. [15, 54, 24, 25, 55, 25],
  870. [23, 45, 15, 25, 46, 16],
  871. // 31
  872. [13, 145, 115, 3, 146, 116],
  873. [2, 74, 46, 29, 75, 47],
  874. [42, 54, 24, 1, 55, 25],
  875. [23, 45, 15, 28, 46, 16],
  876. // 32
  877. [17, 145, 115],
  878. [10, 74, 46, 23, 75, 47],
  879. [10, 54, 24, 35, 55, 25],
  880. [19, 45, 15, 35, 46, 16],
  881. // 33
  882. [17, 145, 115, 1, 146, 116],
  883. [14, 74, 46, 21, 75, 47],
  884. [29, 54, 24, 19, 55, 25],
  885. [11, 45, 15, 46, 46, 16],
  886. // 34
  887. [13, 145, 115, 6, 146, 116],
  888. [14, 74, 46, 23, 75, 47],
  889. [44, 54, 24, 7, 55, 25],
  890. [59, 46, 16, 1, 47, 17],
  891. // 35
  892. [12, 151, 121, 7, 152, 122],
  893. [12, 75, 47, 26, 76, 48],
  894. [39, 54, 24, 14, 55, 25],
  895. [22, 45, 15, 41, 46, 16],
  896. // 36
  897. [6, 151, 121, 14, 152, 122],
  898. [6, 75, 47, 34, 76, 48],
  899. [46, 54, 24, 10, 55, 25],
  900. [2, 45, 15, 64, 46, 16],
  901. // 37
  902. [17, 152, 122, 4, 153, 123],
  903. [29, 74, 46, 14, 75, 47],
  904. [49, 54, 24, 10, 55, 25],
  905. [24, 45, 15, 46, 46, 16],
  906. // 38
  907. [4, 152, 122, 18, 153, 123],
  908. [13, 74, 46, 32, 75, 47],
  909. [48, 54, 24, 14, 55, 25],
  910. [42, 45, 15, 32, 46, 16],
  911. // 39
  912. [20, 147, 117, 4, 148, 118],
  913. [40, 75, 47, 7, 76, 48],
  914. [43, 54, 24, 22, 55, 25],
  915. [10, 45, 15, 67, 46, 16],
  916. // 40
  917. [19, 148, 118, 6, 149, 119],
  918. [18, 75, 47, 31, 76, 48],
  919. [34, 54, 24, 34, 55, 25],
  920. [20, 45, 15, 61, 46, 16]
  921. ];
  922. /**
  923. * 根据数据获取对应版本
  924. * @return {[type]} [description]
  925. */
  926. QRCodeAlg.prototype.getRightType = function() {
  927. for (var typeNumber = 1; typeNumber < 41; typeNumber++) {
  928. var rsBlock = RS_BLOCK_TABLE[(typeNumber - 1) * 4 + this.errorCorrectLevel];
  929. if (rsBlock == undefined) {
  930. throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" + this
  931. .errorCorrectLevel);
  932. }
  933. var length = rsBlock.length / 3;
  934. var totalDataCount = 0;
  935. for (var i = 0; i < length; i++) {
  936. var count = rsBlock[i * 3 + 0];
  937. var dataCount = rsBlock[i * 3 + 2];
  938. totalDataCount += dataCount * count;
  939. }
  940. var lengthBytes = typeNumber > 9 ? 2 : 1;
  941. if (this.utf8bytes.length + lengthBytes < totalDataCount || typeNumber == 40) {
  942. this.typeNumber = typeNumber;
  943. this.rsBlock = rsBlock;
  944. this.totalDataCount = totalDataCount;
  945. break;
  946. }
  947. }
  948. };
  949. //---------------------------------------------------------------------
  950. // QRBitBuffer
  951. //---------------------------------------------------------------------
  952. function QRBitBuffer() {
  953. this.buffer = new Array();
  954. this.length = 0;
  955. }
  956. QRBitBuffer.prototype = {
  957. get: function(index) {
  958. var bufIndex = Math.floor(index / 8);
  959. return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1);
  960. },
  961. put: function(num, length) {
  962. for (var i = 0; i < length; i++) {
  963. this.putBit(((num >>> (length - i - 1)) & 1));
  964. }
  965. },
  966. putBit: function(bit) {
  967. var bufIndex = Math.floor(this.length / 8);
  968. if (this.buffer.length <= bufIndex) {
  969. this.buffer.push(0);
  970. }
  971. if (bit) {
  972. this.buffer[bufIndex] |= (0x80 >>> (this.length % 8));
  973. }
  974. this.length++;
  975. }
  976. };
  977. // xzedit
  978. let qrcodeAlgObjCache = [];
  979. /**
  980. * 二维码构造函数,主要用于绘制
  981. * @param {参数列表} opt 传递参数
  982. * @return {}
  983. */
  984. QRCode = function(opt) {
  985. //设置默认参数
  986. this.options = {
  987. text: '',
  988. size: 256,
  989. correctLevel: 3,
  990. background: '#ffffff',
  991. foreground: '#000000',
  992. pdground: '#000000',
  993. image: '',
  994. imageSize: 30,
  995. canvasId: opt.canvasId,
  996. context: opt.context,
  997. usingComponents: opt.usingComponents,
  998. showLoading: opt.showLoading,
  999. loadingText: opt.loadingText,
  1000. };
  1001. if (typeof opt === 'string') { // 只编码ASCII字符串
  1002. opt = {
  1003. text: opt
  1004. };
  1005. }
  1006. if (opt) {
  1007. for (var i in opt) {
  1008. this.options[i] = opt[i];
  1009. }
  1010. }
  1011. //使用QRCodeAlg创建二维码结构
  1012. var qrCodeAlg = null;
  1013. for (var i = 0, l = qrcodeAlgObjCache.length; i < l; i++) {
  1014. if (qrcodeAlgObjCache[i].text == this.options.text && qrcodeAlgObjCache[i].text.correctLevel == this
  1015. .options.correctLevel) {
  1016. qrCodeAlg = qrcodeAlgObjCache[i].obj;
  1017. break;
  1018. }
  1019. }
  1020. if (i == l) {
  1021. qrCodeAlg = new QRCodeAlg(this.options.text, this.options.correctLevel);
  1022. qrcodeAlgObjCache.push({
  1023. text: this.options.text,
  1024. correctLevel: this.options.correctLevel,
  1025. obj: qrCodeAlg
  1026. });
  1027. }
  1028. /**
  1029. * 计算矩阵点的前景色
  1030. * @param {Obj} config
  1031. * @param {Number} config.row 点x坐标
  1032. * @param {Number} config.col 点y坐标
  1033. * @param {Number} config.count 矩阵大小
  1034. * @param {Number} config.options 组件的options
  1035. * @return {String}
  1036. */
  1037. let getForeGround = function(config) {
  1038. var options = config.options;
  1039. if (options.pdground && (
  1040. (config.row > 1 && config.row < 5 && config.col > 1 && config.col < 5) ||
  1041. (config.row > (config.count - 6) && config.row < (config.count - 2) && config.col > 1 &&
  1042. config.col < 5) ||
  1043. (config.row > 1 && config.row < 5 && config.col > (config.count - 6) && config.col < (
  1044. config.count - 2))
  1045. )) {
  1046. return options.pdground;
  1047. }
  1048. return options.foreground;
  1049. }
  1050. // 创建canvas
  1051. let createCanvas = function(options) {
  1052. if (options.showLoading) {
  1053. uni.showLoading({
  1054. title: options.loadingText,
  1055. mask: true
  1056. });
  1057. }
  1058. var ctx = uni.createCanvasContext(options.canvasId, options.context);
  1059. var count = qrCodeAlg.getModuleCount();
  1060. var ratioSize = options.size;
  1061. var ratioImgSize = options.imageSize;
  1062. //计算每个点的长宽
  1063. var tileW = (ratioSize / count).toPrecision(4);
  1064. var tileH = (ratioSize / count).toPrecision(4);
  1065. //绘制
  1066. for (var row = 0; row < count; row++) {
  1067. for (var col = 0; col < count; col++) {
  1068. var w = (Math.ceil((col + 1) * tileW) - Math.floor(col * tileW));
  1069. var h = (Math.ceil((row + 1) * tileW) - Math.floor(row * tileW));
  1070. var foreground = getForeGround({
  1071. row: row,
  1072. col: col,
  1073. count: count,
  1074. options: options
  1075. });
  1076. ctx.setFillStyle(qrCodeAlg.modules[row][col] ? foreground : options.background);
  1077. ctx.fillRect(Math.round(col * tileW), Math.round(row * tileH), w, h);
  1078. }
  1079. }
  1080. if (options.image) {
  1081. var x = Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
  1082. var y = Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
  1083. drawRoundedRect(ctx, x, y, ratioImgSize, ratioImgSize, 2, 6, true, true)
  1084. ctx.drawImage(options.image, x, y, ratioImgSize, ratioImgSize);
  1085. // 画圆角矩形
  1086. function drawRoundedRect(ctxi, x, y, width, height, r, lineWidth, fill, stroke) {
  1087. ctxi.setLineWidth(lineWidth);
  1088. ctxi.setFillStyle(options.background);
  1089. ctxi.setStrokeStyle(options.background);
  1090. ctxi.beginPath(); // draw top and top right corner
  1091. ctxi.moveTo(x + r, y);
  1092. ctxi.arcTo(x + width, y, x + width, y + r,
  1093. r); // draw right side and bottom right corner
  1094. ctxi.arcTo(x + width, y + height, x + width - r, y + height,
  1095. r); // draw bottom and bottom left corner
  1096. ctxi.arcTo(x, y + height, x, y + height - r, r); // draw left and top left corner
  1097. ctxi.arcTo(x, y, x + r, y, r);
  1098. ctxi.closePath();
  1099. if (fill) {
  1100. ctxi.fill();
  1101. }
  1102. if (stroke) {
  1103. ctxi.stroke();
  1104. }
  1105. }
  1106. }
  1107. setTimeout(() => {
  1108. ctx.draw(true, () => {
  1109. // 保存到临时区域
  1110. setTimeout(() => {
  1111. try {
  1112. uni.canvasToTempFilePath({
  1113. width: options.width,
  1114. height: options.height,
  1115. destWidth: options.width,
  1116. destHeight: options.height,
  1117. canvasId: options.canvasId,
  1118. quality: Number(1),
  1119. success: function(res) {
  1120. if (options.cbResult) {
  1121. options.cbResult(res
  1122. .tempFilePath)
  1123. }
  1124. },
  1125. fail: function(res) {
  1126. if (options.cbResult) {
  1127. options.cbResult(res)
  1128. }
  1129. },
  1130. complete: function() {
  1131. if (options.showLoading) {
  1132. uni.hideLoading();
  1133. }
  1134. },
  1135. }, options.context);
  1136. } catch (e) {
  1137. //TODO handle the exception
  1138. }
  1139. }, options.text.length + 100);
  1140. });
  1141. }, options.usingComponents ? 0 : 150);
  1142. }
  1143. createCanvas(this.options);
  1144. // 空判定
  1145. let empty = function(v) {
  1146. let tp = typeof v,
  1147. rt = false;
  1148. if (tp == "number" && String(v) == "") {
  1149. rt = true
  1150. } else if (tp == "undefined") {
  1151. rt = true
  1152. } else if (tp == "object") {
  1153. if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
  1154. } else if (tp == "string") {
  1155. if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
  1156. } else if (tp == "function") {
  1157. rt = false
  1158. }
  1159. return rt
  1160. }
  1161. };
  1162. QRCode.prototype.clear = function(fn) {
  1163. var ctx = uni.createCanvasContext(this.options.canvasId, this.options.context)
  1164. ctx.clearRect(0, 0, this.options.size, this.options.size)
  1165. ctx.draw(false, () => {
  1166. if (fn) {
  1167. fn()
  1168. }
  1169. })
  1170. };
  1171. })()
  1172. export default QRCode