QRCodeAlg.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /* eslint-disable */
  2. /**
  3. * 获取单个字符的utf8编码
  4. * unicode BMP平面约65535个字符
  5. * @param {num} code
  6. * return {array}
  7. */
  8. function unicodeFormat8(code) {
  9. // 1 byte
  10. var c0, c1, c2;
  11. if (code < 128) {
  12. return [code];
  13. // 2 bytes
  14. } else if (code < 2048) {
  15. c0 = 192 + (code >> 6);
  16. c1 = 128 + (code & 63);
  17. return [c0, c1];
  18. // 3 bytes
  19. } else {
  20. c0 = 224 + (code >> 12);
  21. c1 = 128 + (code >> 6 & 63);
  22. c2 = 128 + (code & 63);
  23. return [c0, c1, c2];
  24. }
  25. }
  26. /**
  27. * 获取字符串的utf8编码字节串
  28. * @param {string} string
  29. * @return {array}
  30. */
  31. function getUTF8Bytes(string) {
  32. var utf8codes = [];
  33. for (var i = 0; i < string.length; i++) {
  34. var code = string.charCodeAt(i);
  35. var utf8 = unicodeFormat8(code);
  36. for (var j = 0; j < utf8.length; j++) {
  37. utf8codes.push(utf8[j]);
  38. }
  39. }
  40. return utf8codes;
  41. }
  42. /**
  43. * 二维码算法实现
  44. * @param {string} data 要编码的信息字符串
  45. * @param {num} errorCorrectLevel 纠错等级
  46. */
  47. export default function QRCodeAlg(data, errorCorrectLevel) {
  48. this.typeNumber = -1; //版本
  49. this.errorCorrectLevel = errorCorrectLevel;
  50. this.modules = null; //二维矩阵,存放最终结果
  51. this.moduleCount = 0; //矩阵大小
  52. this.dataCache = null; //数据缓存
  53. this.rsBlocks = null; //版本数据信息
  54. this.totalDataCount = -1; //可使用的数据量
  55. this.data = data;
  56. this.utf8bytes = getUTF8Bytes(data);
  57. this.make();
  58. }
  59. QRCodeAlg.prototype = {
  60. constructor: QRCodeAlg,
  61. /**
  62. * 获取二维码矩阵大小
  63. * @return {num} 矩阵大小
  64. */
  65. getModuleCount: function () {
  66. return this.moduleCount;
  67. },
  68. /**
  69. * 编码
  70. */
  71. make: function () {
  72. this.getRightType();
  73. this.dataCache = this.createData();
  74. this.createQrcode();
  75. },
  76. /**
  77. * 设置二位矩阵功能图形
  78. * @param {bool} test 表示是否在寻找最好掩膜阶段
  79. * @param {num} maskPattern 掩膜的版本
  80. */
  81. makeImpl: function (maskPattern) {
  82. this.moduleCount = this.typeNumber * 4 + 17;
  83. this.modules = new Array(this.moduleCount);
  84. for (var row = 0; row < this.moduleCount; row++) {
  85. this.modules[row] = new Array(this.moduleCount);
  86. }
  87. this.setupPositionProbePattern(0, 0);
  88. this.setupPositionProbePattern(this.moduleCount - 7, 0);
  89. this.setupPositionProbePattern(0, this.moduleCount - 7);
  90. this.setupPositionAdjustPattern();
  91. this.setupTimingPattern();
  92. this.setupTypeInfo(true, maskPattern);
  93. if (this.typeNumber >= 7) {
  94. this.setupTypeNumber(true);
  95. }
  96. this.mapData(this.dataCache, maskPattern);
  97. },
  98. /**
  99. * 设置二维码的位置探测图形
  100. * @param {num} row 探测图形的中心横坐标
  101. * @param {num} col 探测图形的中心纵坐标
  102. */
  103. setupPositionProbePattern: function (row, col) {
  104. for (var r = -1; r <= 7; r++) {
  105. if (row + r <= -1 || this.moduleCount <= row + r) continue;
  106. for (var c = -1; c <= 7; c++) {
  107. if (col + c <= -1 || this.moduleCount <= col + c) continue;
  108. 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)) {
  109. this.modules[row + r][col + c] = true;
  110. } else {
  111. this.modules[row + r][col + c] = false;
  112. }
  113. }
  114. }
  115. },
  116. /**
  117. * 创建二维码
  118. * @return {[type]} [description]
  119. */
  120. createQrcode: function () {
  121. var minLostPoint = 0;
  122. var pattern = 0;
  123. var bestModules = null;
  124. for (var i = 0; i < 8; i++) {
  125. this.makeImpl(i);
  126. var lostPoint = QRUtil.getLostPoint(this);
  127. if (i == 0 || minLostPoint > lostPoint) {
  128. minLostPoint = lostPoint;
  129. pattern = i;
  130. bestModules = this.modules;
  131. }
  132. }
  133. this.modules = bestModules;
  134. this.setupTypeInfo(false, pattern);
  135. if (this.typeNumber >= 7) {
  136. this.setupTypeNumber(false);
  137. }
  138. },
  139. /**
  140. * 设置定位图形
  141. * @return {[type]} [description]
  142. */
  143. setupTimingPattern: function () {
  144. for (var r = 8; r < this.moduleCount - 8; r++) {
  145. if (this.modules[r][6] != null) {
  146. continue;
  147. }
  148. this.modules[r][6] = (r % 2 == 0);
  149. if (this.modules[6][r] != null) {
  150. continue;
  151. }
  152. this.modules[6][r] = (r % 2 == 0);
  153. }
  154. },
  155. /**
  156. * 设置矫正图形
  157. * @return {[type]} [description]
  158. */
  159. setupPositionAdjustPattern: function () {
  160. var pos = QRUtil.getPatternPosition(this.typeNumber);
  161. for (var i = 0; i < pos.length; i++) {
  162. for (var j = 0; j < pos.length; j++) {
  163. var row = pos[i];
  164. var col = pos[j];
  165. if (this.modules[row][col] != null) {
  166. continue;
  167. }
  168. for (var r = -2; r <= 2; r++) {
  169. for (var c = -2; c <= 2; c++) {
  170. if (r == -2 || r == 2 || c == -2 || c == 2 || (r == 0 && c == 0)) {
  171. this.modules[row + r][col + c] = true;
  172. } else {
  173. this.modules[row + r][col + c] = false;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. },
  180. /**
  181. * 设置版本信息(7以上版本才有)
  182. * @param {bool} test 是否处于判断最佳掩膜阶段
  183. * @return {[type]} [description]
  184. */
  185. setupTypeNumber: function (test) {
  186. var bits = QRUtil.getBCHTypeNumber(this.typeNumber);
  187. for (var i = 0; i < 18; i++) {
  188. var mod = (!test && ((bits >> i) & 1) == 1);
  189. this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod;
  190. this.modules[i % 3 + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod;
  191. }
  192. },
  193. /**
  194. * 设置格式信息(纠错等级和掩膜版本)
  195. * @param {bool} test
  196. * @param {num} maskPattern 掩膜版本
  197. * @return {}
  198. */
  199. setupTypeInfo: function (test, maskPattern) {
  200. var data = (QRErrorCorrectLevel[this.errorCorrectLevel] << 3) | maskPattern;
  201. var bits = QRUtil.getBCHTypeInfo(data);
  202. // vertical
  203. for (var i = 0; i < 15; i++) {
  204. var mod = (!test && ((bits >> i) & 1) == 1);
  205. if (i < 6) {
  206. this.modules[i][8] = mod;
  207. } else if (i < 8) {
  208. this.modules[i + 1][8] = mod;
  209. } else {
  210. this.modules[this.moduleCount - 15 + i][8] = mod;
  211. }
  212. // horizontal
  213. var mod = (!test && ((bits >> i) & 1) == 1);
  214. if (i < 8) {
  215. this.modules[8][this.moduleCount - i - 1] = mod;
  216. } else if (i < 9) {
  217. this.modules[8][15 - i - 1 + 1] = mod;
  218. } else {
  219. this.modules[8][15 - i - 1] = mod;
  220. }
  221. }
  222. // fixed module
  223. this.modules[this.moduleCount - 8][8] = (!test);
  224. },
  225. /**
  226. * 数据编码
  227. * @return {[type]} [description]
  228. */
  229. createData: function () {
  230. var buffer = new QRBitBuffer();
  231. var lengthBits = this.typeNumber > 9 ? 16 : 8;
  232. buffer.put(4, 4); //添加模式
  233. buffer.put(this.utf8bytes.length, lengthBits);
  234. for (var i = 0, l = this.utf8bytes.length; i < l; i++) {
  235. buffer.put(this.utf8bytes[i], 8);
  236. }
  237. if (buffer.length + 4 <= this.totalDataCount * 8) {
  238. buffer.put(0, 4);
  239. }
  240. // padding
  241. while (buffer.length % 8 != 0) {
  242. buffer.putBit(false);
  243. }
  244. // padding
  245. while (true) {
  246. if (buffer.length >= this.totalDataCount * 8) {
  247. break;
  248. }
  249. buffer.put(QRCodeAlg.PAD0, 8);
  250. if (buffer.length >= this.totalDataCount * 8) {
  251. break;
  252. }
  253. buffer.put(QRCodeAlg.PAD1, 8);
  254. }
  255. return this.createBytes(buffer);
  256. },
  257. /**
  258. * 纠错码编码
  259. * @param {buffer} buffer 数据编码
  260. * @return {[type]}
  261. */
  262. createBytes: function (buffer) {
  263. var offset = 0;
  264. var maxDcCount = 0;
  265. var maxEcCount = 0;
  266. var length = this.rsBlock.length / 3;
  267. var rsBlocks = new Array();
  268. for (var i = 0; i < length; i++) {
  269. var count = this.rsBlock[i * 3 + 0];
  270. var totalCount = this.rsBlock[i * 3 + 1];
  271. var dataCount = this.rsBlock[i * 3 + 2];
  272. for (var j = 0; j < count; j++) {
  273. rsBlocks.push([dataCount, totalCount]);
  274. }
  275. }
  276. var dcdata = new Array(rsBlocks.length);
  277. var ecdata = new Array(rsBlocks.length);
  278. for (var r = 0; r < rsBlocks.length; r++) {
  279. var dcCount = rsBlocks[r][0];
  280. var ecCount = rsBlocks[r][1] - dcCount;
  281. maxDcCount = Math.max(maxDcCount, dcCount);
  282. maxEcCount = Math.max(maxEcCount, ecCount);
  283. dcdata[r] = new Array(dcCount);
  284. for (var i = 0; i < dcdata[r].length; i++) {
  285. dcdata[r][i] = 0xff & buffer.buffer[i + offset];
  286. }
  287. offset += dcCount;
  288. var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount);
  289. var rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1);
  290. var modPoly = rawPoly.mod(rsPoly);
  291. ecdata[r] = new Array(rsPoly.getLength() - 1);
  292. for (var i = 0; i < ecdata[r].length; i++) {
  293. var modIndex = i + modPoly.getLength() - ecdata[r].length;
  294. ecdata[r][i] = (modIndex >= 0) ? modPoly.get(modIndex) : 0;
  295. }
  296. }
  297. var data = new Array(this.totalDataCount);
  298. var index = 0;
  299. for (var i = 0; i < maxDcCount; i++) {
  300. for (var r = 0; r < rsBlocks.length; r++) {
  301. if (i < dcdata[r].length) {
  302. data[index++] = dcdata[r][i];
  303. }
  304. }
  305. }
  306. for (var i = 0; i < maxEcCount; i++) {
  307. for (var r = 0; r < rsBlocks.length; r++) {
  308. if (i < ecdata[r].length) {
  309. data[index++] = ecdata[r][i];
  310. }
  311. }
  312. }
  313. return data;
  314. },
  315. /**
  316. * 布置模块,构建最终信息
  317. * @param {} data
  318. * @param {} maskPattern
  319. * @return {}
  320. */
  321. mapData: function (data, maskPattern) {
  322. var inc = -1;
  323. var row = this.moduleCount - 1;
  324. var bitIndex = 7;
  325. var byteIndex = 0;
  326. for (var col = this.moduleCount - 1; col > 0; col -= 2) {
  327. if (col == 6) col--;
  328. while (true) {
  329. for (var c = 0; c < 2; c++) {
  330. if (this.modules[row][col - c] == null) {
  331. var dark = false;
  332. if (byteIndex < data.length) {
  333. dark = (((data[byteIndex] >>> bitIndex) & 1) == 1);
  334. }
  335. var mask = QRUtil.getMask(maskPattern, row, col - c);
  336. if (mask) {
  337. dark = !dark;
  338. }
  339. this.modules[row][col - c] = dark;
  340. bitIndex--;
  341. if (bitIndex == -1) {
  342. byteIndex++;
  343. bitIndex = 7;
  344. }
  345. }
  346. }
  347. row += inc;
  348. if (row < 0 || this.moduleCount <= row) {
  349. row -= inc;
  350. inc = -inc;
  351. break;
  352. }
  353. }
  354. }
  355. }
  356. };
  357. /**
  358. * 填充字段
  359. */
  360. QRCodeAlg.PAD0 = 0xEC;
  361. QRCodeAlg.PAD1 = 0x11;
  362. //---------------------------------------------------------------------
  363. // 纠错等级对应的编码
  364. //---------------------------------------------------------------------
  365. var QRErrorCorrectLevel = [1, 0, 3, 2];
  366. //---------------------------------------------------------------------
  367. // 掩膜版本
  368. //---------------------------------------------------------------------
  369. var QRMaskPattern = {
  370. PATTERN000: 0,
  371. PATTERN001: 1,
  372. PATTERN010: 2,
  373. PATTERN011: 3,
  374. PATTERN100: 4,
  375. PATTERN101: 5,
  376. PATTERN110: 6,
  377. PATTERN111: 7
  378. };
  379. //---------------------------------------------------------------------
  380. // 工具类
  381. //---------------------------------------------------------------------
  382. var QRUtil = {
  383. /*
  384. 每个版本矫正图形的位置
  385. */
  386. PATTERN_POSITION_TABLE: [
  387. [],
  388. [6, 18],
  389. [6, 22],
  390. [6, 26],
  391. [6, 30],
  392. [6, 34],
  393. [6, 22, 38],
  394. [6, 24, 42],
  395. [6, 26, 46],
  396. [6, 28, 50],
  397. [6, 30, 54],
  398. [6, 32, 58],
  399. [6, 34, 62],
  400. [6, 26, 46, 66],
  401. [6, 26, 48, 70],
  402. [6, 26, 50, 74],
  403. [6, 30, 54, 78],
  404. [6, 30, 56, 82],
  405. [6, 30, 58, 86],
  406. [6, 34, 62, 90],
  407. [6, 28, 50, 72, 94],
  408. [6, 26, 50, 74, 98],
  409. [6, 30, 54, 78, 102],
  410. [6, 28, 54, 80, 106],
  411. [6, 32, 58, 84, 110],
  412. [6, 30, 58, 86, 114],
  413. [6, 34, 62, 90, 118],
  414. [6, 26, 50, 74, 98, 122],
  415. [6, 30, 54, 78, 102, 126],
  416. [6, 26, 52, 78, 104, 130],
  417. [6, 30, 56, 82, 108, 134],
  418. [6, 34, 60, 86, 112, 138],
  419. [6, 30, 58, 86, 114, 142],
  420. [6, 34, 62, 90, 118, 146],
  421. [6, 30, 54, 78, 102, 126, 150],
  422. [6, 24, 50, 76, 102, 128, 154],
  423. [6, 28, 54, 80, 106, 132, 158],
  424. [6, 32, 58, 84, 110, 136, 162],
  425. [6, 26, 54, 82, 110, 138, 166],
  426. [6, 30, 58, 86, 114, 142, 170]
  427. ],
  428. G15: (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0),
  429. G18: (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0),
  430. G15_MASK: (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1),
  431. /*
  432. BCH编码格式信息
  433. */
  434. getBCHTypeInfo: function (data) {
  435. var d = data << 10;
  436. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) {
  437. d ^= (QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15)));
  438. }
  439. return ((data << 10) | d) ^ QRUtil.G15_MASK;
  440. },
  441. /*
  442. BCH编码版本信息
  443. */
  444. getBCHTypeNumber: function (data) {
  445. var d = data << 12;
  446. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) {
  447. d ^= (QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18)));
  448. }
  449. return (data << 12) | d;
  450. },
  451. /*
  452. 获取BCH位信息
  453. */
  454. getBCHDigit: function (data) {
  455. var digit = 0;
  456. while (data != 0) {
  457. digit++;
  458. data >>>= 1;
  459. }
  460. return digit;
  461. },
  462. /*
  463. 获取版本对应的矫正图形位置
  464. */
  465. getPatternPosition: function (typeNumber) {
  466. return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1];
  467. },
  468. /*
  469. 掩膜算法
  470. */
  471. getMask: function (maskPattern, i, j) {
  472. switch (maskPattern) {
  473. case QRMaskPattern.PATTERN000:
  474. return (i + j) % 2 == 0;
  475. case QRMaskPattern.PATTERN001:
  476. return i % 2 == 0;
  477. case QRMaskPattern.PATTERN010:
  478. return j % 3 == 0;
  479. case QRMaskPattern.PATTERN011:
  480. return (i + j) % 3 == 0;
  481. case QRMaskPattern.PATTERN100:
  482. return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0;
  483. case QRMaskPattern.PATTERN101:
  484. return (i * j) % 2 + (i * j) % 3 == 0;
  485. case QRMaskPattern.PATTERN110:
  486. return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
  487. case QRMaskPattern.PATTERN111:
  488. return ((i * j) % 3 + (i + j) % 2) % 2 == 0;
  489. default:
  490. throw new Error("bad maskPattern:" + maskPattern);
  491. }
  492. },
  493. /*
  494. 获取RS的纠错多项式
  495. */
  496. getErrorCorrectPolynomial: function (errorCorrectLength) {
  497. var a = new QRPolynomial([1], 0);
  498. for (var i = 0; i < errorCorrectLength; i++) {
  499. a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0));
  500. }
  501. return a;
  502. },
  503. /*
  504. 获取评价
  505. */
  506. getLostPoint: function (qrCode) {
  507. var moduleCount = qrCode.getModuleCount(),
  508. lostPoint = 0,
  509. darkCount = 0;
  510. for (var row = 0; row < moduleCount; row++) {
  511. var sameCount = 0;
  512. var head = qrCode.modules[row][0];
  513. for (var col = 0; col < moduleCount; col++) {
  514. var current = qrCode.modules[row][col];
  515. //level 3 评价
  516. if (col < moduleCount - 6) {
  517. 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]) {
  518. if (col < moduleCount - 10) {
  519. if (qrCode.modules[row][col + 7] && qrCode.modules[row][col + 8] && qrCode.modules[row][col + 9] && qrCode.modules[row][col + 10]) {
  520. lostPoint += 40;
  521. }
  522. } else if (col > 3) {
  523. if (qrCode.modules[row][col - 1] && qrCode.modules[row][col - 2] && qrCode.modules[row][col - 3] && qrCode.modules[row][col - 4]) {
  524. lostPoint += 40;
  525. }
  526. }
  527. }
  528. }
  529. //level 2 评价
  530. if ((row < moduleCount - 1) && (col < moduleCount - 1)) {
  531. var count = 0;
  532. if (current) count++;
  533. if (qrCode.modules[row + 1][col]) count++;
  534. if (qrCode.modules[row][col + 1]) count++;
  535. if (qrCode.modules[row + 1][col + 1]) count++;
  536. if (count == 0 || count == 4) {
  537. lostPoint += 3;
  538. }
  539. }
  540. //level 1 评价
  541. if (head ^ current) {
  542. sameCount++;
  543. } else {
  544. head = current;
  545. if (sameCount >= 5) {
  546. lostPoint += (3 + sameCount - 5);
  547. }
  548. sameCount = 1;
  549. }
  550. //level 4 评价
  551. if (current) {
  552. darkCount++;
  553. }
  554. }
  555. }
  556. for (var col = 0; col < moduleCount; col++) {
  557. var sameCount = 0;
  558. var head = qrCode.modules[0][col];
  559. for (var row = 0; row < moduleCount; row++) {
  560. var current = qrCode.modules[row][col];
  561. //level 3 评价
  562. if (row < moduleCount - 6) {
  563. 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]) {
  564. if (row < moduleCount - 10) {
  565. if (qrCode.modules[row + 7][col] && qrCode.modules[row + 8][col] && qrCode.modules[row + 9][col] && qrCode.modules[row + 10][col]) {
  566. lostPoint += 40;
  567. }
  568. } else if (row > 3) {
  569. if (qrCode.modules[row - 1][col] && qrCode.modules[row - 2][col] && qrCode.modules[row - 3][col] && qrCode.modules[row - 4][col]) {
  570. lostPoint += 40;
  571. }
  572. }
  573. }
  574. }
  575. //level 1 评价
  576. if (head ^ current) {
  577. sameCount++;
  578. } else {
  579. head = current;
  580. if (sameCount >= 5) {
  581. lostPoint += (3 + sameCount - 5);
  582. }
  583. sameCount = 1;
  584. }
  585. }
  586. }
  587. // LEVEL4
  588. var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5;
  589. lostPoint += ratio * 10;
  590. return lostPoint;
  591. }
  592. };
  593. //---------------------------------------------------------------------
  594. // QRMath使用的数学工具
  595. //---------------------------------------------------------------------
  596. var QRMath = {
  597. /*
  598. 将n转化为a^m
  599. */
  600. glog: function (n) {
  601. if (n < 1) {
  602. throw new Error("glog(" + n + ")");
  603. }
  604. return QRMath.LOG_TABLE[n];
  605. },
  606. /*
  607. 将a^m转化为n
  608. */
  609. gexp: function (n) {
  610. while (n < 0) {
  611. n += 255;
  612. }
  613. while (n >= 256) {
  614. n -= 255;
  615. }
  616. return QRMath.EXP_TABLE[n];
  617. },
  618. EXP_TABLE: new Array(256),
  619. LOG_TABLE: new Array(256)
  620. };
  621. for (var i = 0; i < 8; i++) {
  622. QRMath.EXP_TABLE[i] = 1 << i;
  623. }
  624. for (var i = 8; i < 256; i++) {
  625. QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath.EXP_TABLE[i - 8];
  626. }
  627. for (var i = 0; i < 255; i++) {
  628. QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
  629. }
  630. //---------------------------------------------------------------------
  631. // QRPolynomial 多项式
  632. //---------------------------------------------------------------------
  633. /**
  634. * 多项式类
  635. * @param {Array} num 系数
  636. * @param {num} shift a^shift
  637. */
  638. function QRPolynomial(num, shift) {
  639. if (num.length == undefined) {
  640. throw new Error(num.length + "/" + shift);
  641. }
  642. var offset = 0;
  643. while (offset < num.length && num[offset] == 0) {
  644. offset++;
  645. }
  646. this.num = new Array(num.length - offset + shift);
  647. for (var i = 0; i < num.length - offset; i++) {
  648. this.num[i] = num[i + offset];
  649. }
  650. }
  651. QRPolynomial.prototype = {
  652. get: function (index) {
  653. return this.num[index];
  654. },
  655. getLength: function () {
  656. return this.num.length;
  657. },
  658. /**
  659. * 多项式乘法
  660. * @param {QRPolynomial} e 被乘多项式
  661. * @return {[type]} [description]
  662. */
  663. multiply: function (e) {
  664. var num = new Array(this.getLength() + e.getLength() - 1);
  665. for (var i = 0; i < this.getLength(); i++) {
  666. for (var j = 0; j < e.getLength(); j++) {
  667. num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j)));
  668. }
  669. }
  670. return new QRPolynomial(num, 0);
  671. },
  672. /**
  673. * 多项式模运算
  674. * @param {QRPolynomial} e 模多项式
  675. * @return {}
  676. */
  677. mod: function (e) {
  678. var tl = this.getLength(),
  679. el = e.getLength();
  680. if (tl - el < 0) {
  681. return this;
  682. }
  683. var num = new Array(tl);
  684. for (var i = 0; i < tl; i++) {
  685. num[i] = this.get(i);
  686. }
  687. while (num.length >= el) {
  688. var ratio = QRMath.glog(num[0]) - QRMath.glog(e.get(0));
  689. for (var i = 0; i < e.getLength(); i++) {
  690. num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio);
  691. }
  692. while (num[0] == 0) {
  693. num.shift();
  694. }
  695. }
  696. return new QRPolynomial(num, 0);
  697. }
  698. };
  699. //---------------------------------------------------------------------
  700. // RS_BLOCK_TABLE
  701. //---------------------------------------------------------------------
  702. /*
  703. 二维码各个版本信息[块数, 每块中的数据块数, 每块中的信息块数]
  704. */
  705. var RS_BLOCK_TABLE = [
  706. // L
  707. // M
  708. // Q
  709. // H
  710. // 1
  711. [1, 26, 19],
  712. [1, 26, 16],
  713. [1, 26, 13],
  714. [1, 26, 9],
  715. // 2
  716. [1, 44, 34],
  717. [1, 44, 28],
  718. [1, 44, 22],
  719. [1, 44, 16],
  720. // 3
  721. [1, 70, 55],
  722. [1, 70, 44],
  723. [2, 35, 17],
  724. [2, 35, 13],
  725. // 4
  726. [1, 100, 80],
  727. [2, 50, 32],
  728. [2, 50, 24],
  729. [4, 25, 9],
  730. // 5
  731. [1, 134, 108],
  732. [2, 67, 43],
  733. [2, 33, 15, 2, 34, 16],
  734. [2, 33, 11, 2, 34, 12],
  735. // 6
  736. [2, 86, 68],
  737. [4, 43, 27],
  738. [4, 43, 19],
  739. [4, 43, 15],
  740. // 7
  741. [2, 98, 78],
  742. [4, 49, 31],
  743. [2, 32, 14, 4, 33, 15],
  744. [4, 39, 13, 1, 40, 14],
  745. // 8
  746. [2, 121, 97],
  747. [2, 60, 38, 2, 61, 39],
  748. [4, 40, 18, 2, 41, 19],
  749. [4, 40, 14, 2, 41, 15],
  750. // 9
  751. [2, 146, 116],
  752. [3, 58, 36, 2, 59, 37],
  753. [4, 36, 16, 4, 37, 17],
  754. [4, 36, 12, 4, 37, 13],
  755. // 10
  756. [2, 86, 68, 2, 87, 69],
  757. [4, 69, 43, 1, 70, 44],
  758. [6, 43, 19, 2, 44, 20],
  759. [6, 43, 15, 2, 44, 16],
  760. // 11
  761. [4, 101, 81],
  762. [1, 80, 50, 4, 81, 51],
  763. [4, 50, 22, 4, 51, 23],
  764. [3, 36, 12, 8, 37, 13],
  765. // 12
  766. [2, 116, 92, 2, 117, 93],
  767. [6, 58, 36, 2, 59, 37],
  768. [4, 46, 20, 6, 47, 21],
  769. [7, 42, 14, 4, 43, 15],
  770. // 13
  771. [4, 133, 107],
  772. [8, 59, 37, 1, 60, 38],
  773. [8, 44, 20, 4, 45, 21],
  774. [12, 33, 11, 4, 34, 12],
  775. // 14
  776. [3, 145, 115, 1, 146, 116],
  777. [4, 64, 40, 5, 65, 41],
  778. [11, 36, 16, 5, 37, 17],
  779. [11, 36, 12, 5, 37, 13],
  780. // 15
  781. [5, 109, 87, 1, 110, 88],
  782. [5, 65, 41, 5, 66, 42],
  783. [5, 54, 24, 7, 55, 25],
  784. [11, 36, 12],
  785. // 16
  786. [5, 122, 98, 1, 123, 99],
  787. [7, 73, 45, 3, 74, 46],
  788. [15, 43, 19, 2, 44, 20],
  789. [3, 45, 15, 13, 46, 16],
  790. // 17
  791. [1, 135, 107, 5, 136, 108],
  792. [10, 74, 46, 1, 75, 47],
  793. [1, 50, 22, 15, 51, 23],
  794. [2, 42, 14, 17, 43, 15],
  795. // 18
  796. [5, 150, 120, 1, 151, 121],
  797. [9, 69, 43, 4, 70, 44],
  798. [17, 50, 22, 1, 51, 23],
  799. [2, 42, 14, 19, 43, 15],
  800. // 19
  801. [3, 141, 113, 4, 142, 114],
  802. [3, 70, 44, 11, 71, 45],
  803. [17, 47, 21, 4, 48, 22],
  804. [9, 39, 13, 16, 40, 14],
  805. // 20
  806. [3, 135, 107, 5, 136, 108],
  807. [3, 67, 41, 13, 68, 42],
  808. [15, 54, 24, 5, 55, 25],
  809. [15, 43, 15, 10, 44, 16],
  810. // 21
  811. [4, 144, 116, 4, 145, 117],
  812. [17, 68, 42],
  813. [17, 50, 22, 6, 51, 23],
  814. [19, 46, 16, 6, 47, 17],
  815. // 22
  816. [2, 139, 111, 7, 140, 112],
  817. [17, 74, 46],
  818. [7, 54, 24, 16, 55, 25],
  819. [34, 37, 13],
  820. // 23
  821. [4, 151, 121, 5, 152, 122],
  822. [4, 75, 47, 14, 76, 48],
  823. [11, 54, 24, 14, 55, 25],
  824. [16, 45, 15, 14, 46, 16],
  825. // 24
  826. [6, 147, 117, 4, 148, 118],
  827. [6, 73, 45, 14, 74, 46],
  828. [11, 54, 24, 16, 55, 25],
  829. [30, 46, 16, 2, 47, 17],
  830. // 25
  831. [8, 132, 106, 4, 133, 107],
  832. [8, 75, 47, 13, 76, 48],
  833. [7, 54, 24, 22, 55, 25],
  834. [22, 45, 15, 13, 46, 16],
  835. // 26
  836. [10, 142, 114, 2, 143, 115],
  837. [19, 74, 46, 4, 75, 47],
  838. [28, 50, 22, 6, 51, 23],
  839. [33, 46, 16, 4, 47, 17],
  840. // 27
  841. [8, 152, 122, 4, 153, 123],
  842. [22, 73, 45, 3, 74, 46],
  843. [8, 53, 23, 26, 54, 24],
  844. [12, 45, 15, 28, 46, 16],
  845. // 28
  846. [3, 147, 117, 10, 148, 118],
  847. [3, 73, 45, 23, 74, 46],
  848. [4, 54, 24, 31, 55, 25],
  849. [11, 45, 15, 31, 46, 16],
  850. // 29
  851. [7, 146, 116, 7, 147, 117],
  852. [21, 73, 45, 7, 74, 46],
  853. [1, 53, 23, 37, 54, 24],
  854. [19, 45, 15, 26, 46, 16],
  855. // 30
  856. [5, 145, 115, 10, 146, 116],
  857. [19, 75, 47, 10, 76, 48],
  858. [15, 54, 24, 25, 55, 25],
  859. [23, 45, 15, 25, 46, 16],
  860. // 31
  861. [13, 145, 115, 3, 146, 116],
  862. [2, 74, 46, 29, 75, 47],
  863. [42, 54, 24, 1, 55, 25],
  864. [23, 45, 15, 28, 46, 16],
  865. // 32
  866. [17, 145, 115],
  867. [10, 74, 46, 23, 75, 47],
  868. [10, 54, 24, 35, 55, 25],
  869. [19, 45, 15, 35, 46, 16],
  870. // 33
  871. [17, 145, 115, 1, 146, 116],
  872. [14, 74, 46, 21, 75, 47],
  873. [29, 54, 24, 19, 55, 25],
  874. [11, 45, 15, 46, 46, 16],
  875. // 34
  876. [13, 145, 115, 6, 146, 116],
  877. [14, 74, 46, 23, 75, 47],
  878. [44, 54, 24, 7, 55, 25],
  879. [59, 46, 16, 1, 47, 17],
  880. // 35
  881. [12, 151, 121, 7, 152, 122],
  882. [12, 75, 47, 26, 76, 48],
  883. [39, 54, 24, 14, 55, 25],
  884. [22, 45, 15, 41, 46, 16],
  885. // 36
  886. [6, 151, 121, 14, 152, 122],
  887. [6, 75, 47, 34, 76, 48],
  888. [46, 54, 24, 10, 55, 25],
  889. [2, 45, 15, 64, 46, 16],
  890. // 37
  891. [17, 152, 122, 4, 153, 123],
  892. [29, 74, 46, 14, 75, 47],
  893. [49, 54, 24, 10, 55, 25],
  894. [24, 45, 15, 46, 46, 16],
  895. // 38
  896. [4, 152, 122, 18, 153, 123],
  897. [13, 74, 46, 32, 75, 47],
  898. [48, 54, 24, 14, 55, 25],
  899. [42, 45, 15, 32, 46, 16],
  900. // 39
  901. [20, 147, 117, 4, 148, 118],
  902. [40, 75, 47, 7, 76, 48],
  903. [43, 54, 24, 22, 55, 25],
  904. [10, 45, 15, 67, 46, 16],
  905. // 40
  906. [19, 148, 118, 6, 149, 119],
  907. [18, 75, 47, 31, 76, 48],
  908. [34, 54, 24, 34, 55, 25],
  909. [20, 45, 15, 61, 46, 16]
  910. ];
  911. /**
  912. * 根据数据获取对应版本
  913. * @return {[type]} [description]
  914. */
  915. QRCodeAlg.prototype.getRightType = function () {
  916. for (var typeNumber = 1; typeNumber < 41; typeNumber++) {
  917. var rsBlock = RS_BLOCK_TABLE[(typeNumber - 1) * 4 + this.errorCorrectLevel];
  918. if (rsBlock == undefined) {
  919. throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" + this.errorCorrectLevel);
  920. }
  921. var length = rsBlock.length / 3;
  922. var totalDataCount = 0;
  923. for (var i = 0; i < length; i++) {
  924. var count = rsBlock[i * 3 + 0];
  925. var dataCount = rsBlock[i * 3 + 2];
  926. totalDataCount += dataCount * count;
  927. }
  928. var lengthBytes = typeNumber > 9 ? 2 : 1;
  929. if (this.utf8bytes.length + lengthBytes < totalDataCount || typeNumber == 40) {
  930. this.typeNumber = typeNumber;
  931. this.rsBlock = rsBlock;
  932. this.totalDataCount = totalDataCount;
  933. break;
  934. }
  935. }
  936. };
  937. //---------------------------------------------------------------------
  938. // QRBitBuffer
  939. //---------------------------------------------------------------------
  940. function QRBitBuffer() {
  941. this.buffer = new Array();
  942. this.length = 0;
  943. }
  944. QRBitBuffer.prototype = {
  945. get: function (index) {
  946. var bufIndex = Math.floor(index / 8);
  947. return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1);
  948. },
  949. put: function (num, length) {
  950. for (var i = 0; i < length; i++) {
  951. this.putBit(((num >>> (length - i - 1)) & 1));
  952. }
  953. },
  954. putBit: function (bit) {
  955. var bufIndex = Math.floor(this.length / 8);
  956. if (this.buffer.length <= bufIndex) {
  957. this.buffer.push(0);
  958. }
  959. if (bit) {
  960. this.buffer[bufIndex] |= (0x80 >>> (this.length % 8));
  961. }
  962. this.length++;
  963. }
  964. };