qrcode.js 28 KB

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