QRCodeAlg.js 34 KB

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