qrcode.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. var qrcode = function (typeNumber, errorCorrectLevel) {
  2. var PAD0 = 0xEC;
  3. var PAD1 = 0x11;
  4. var _typeNumber = typeNumber;
  5. var _errorCorrectLevel = QRErrorCorrectLevel[errorCorrectLevel];
  6. var _modules = null;
  7. var _moduleCount = 0;
  8. var _dataCache = null;
  9. var _dataList = new Array();
  10. var _this = {};
  11. var makeImpl = function (test, maskPattern) {
  12. _moduleCount = _typeNumber * 4 + 17;
  13. _modules = function (moduleCount) {
  14. var modules = new Array(moduleCount);
  15. for (var row = 0; row < moduleCount; row += 1) {
  16. modules[row] = new Array(moduleCount);
  17. for (var col = 0; col < moduleCount; col += 1) {
  18. modules[row][col] = null;
  19. }
  20. }
  21. return modules;
  22. }(_moduleCount);
  23. setupPositionProbePattern(0, 0);
  24. setupPositionProbePattern(_moduleCount - 7, 0);
  25. setupPositionProbePattern(0, _moduleCount - 7);
  26. setupPositionAdjustPattern();
  27. setupTimingPattern();
  28. setupTypeInfo(test, maskPattern);
  29. if (_typeNumber >= 7) {
  30. setupTypeNumber(test);
  31. }
  32. if (_dataCache == null) {
  33. _dataCache = createData(_typeNumber, _errorCorrectLevel, _dataList);
  34. }
  35. mapData(_dataCache, maskPattern);
  36. };
  37. var setupPositionProbePattern = function (row, col) {
  38. for (var r = -1; r <= 7; r += 1) {
  39. if (row + r <= -1 || _moduleCount <= row + r) continue;
  40. for (var c = -1; c <= 7; c += 1) {
  41. if (col + c <= -1 || _moduleCount <= col + c) continue;
  42. if ((0 <= r && r <= 6 && (c == 0 || c == 6)) ||
  43. (0 <= c && c <= 6 && (r == 0 || r == 6)) ||
  44. (2 <= r && r <= 4 && 2 <= c && c <= 4)) {
  45. _modules[row + r][col + c] = true;
  46. } else {
  47. _modules[row + r][col + c] = false;
  48. }
  49. }
  50. }
  51. };
  52. var getBestMaskPattern = function () {
  53. var minLostPoint = 0;
  54. var pattern = 0;
  55. for (var i = 0; i < 8; i += 1) {
  56. makeImpl(true, i);
  57. var lostPoint = QRUtil.getLostPoint(_this);
  58. if (i == 0 || minLostPoint > lostPoint) {
  59. minLostPoint = lostPoint;
  60. pattern = i;
  61. }
  62. }
  63. return pattern;
  64. };
  65. var setupTimingPattern = function () {
  66. for (var r = 8; r < _moduleCount - 8; r += 1) {
  67. if (_modules[r][6] != null) {
  68. continue;
  69. }
  70. _modules[r][6] = (r % 2 == 0);
  71. }
  72. for (var c = 8; c < _moduleCount - 8; c += 1) {
  73. if (_modules[6][c] != null) {
  74. continue;
  75. }
  76. _modules[6][c] = (c % 2 == 0);
  77. }
  78. };
  79. var setupPositionAdjustPattern = function () {
  80. var pos = QRUtil.getPatternPosition(_typeNumber);
  81. for (var i = 0; i < pos.length; i += 1) {
  82. for (var j = 0; j < pos.length; j += 1) {
  83. var row = pos[i];
  84. var col = pos[j];
  85. if (_modules[row][col] != null) {
  86. continue;
  87. }
  88. for (var r = -2; r <= 2; r += 1) {
  89. for (var c = -2; c <= 2; c += 1) {
  90. if (r == -2 || r == 2 || c == -2 || c == 2 ||
  91. (r == 0 && c == 0)) {
  92. _modules[row + r][col + c] = true;
  93. } else {
  94. _modules[row + r][col + c] = false;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. };
  101. var setupTypeNumber = function (test) {
  102. var bits = QRUtil.getBCHTypeNumber(_typeNumber);
  103. for (var i = 0; i < 18; i += 1) {
  104. var mod = (!test && ((bits >> i) & 1) == 1);
  105. _modules[Math.floor(i / 3)][i % 3 + _moduleCount - 8 - 3] = mod;
  106. }
  107. for (var i = 0; i < 18; i += 1) {
  108. var mod = (!test && ((bits >> i) & 1) == 1);
  109. _modules[i % 3 + _moduleCount - 8 - 3][Math.floor(i / 3)] = mod;
  110. }
  111. };
  112. var setupTypeInfo = function (test, maskPattern) {
  113. var data = (_errorCorrectLevel << 3) | maskPattern;
  114. var bits = QRUtil.getBCHTypeInfo(data);
  115. // vertical
  116. for (var i = 0; i < 15; i += 1) {
  117. var mod = (!test && ((bits >> i) & 1) == 1);
  118. if (i < 6) {
  119. _modules[i][8] = mod;
  120. } else if (i < 8) {
  121. _modules[i + 1][8] = mod;
  122. } else {
  123. _modules[_moduleCount - 15 + i][8] = mod;
  124. }
  125. }
  126. // horizontal
  127. for (var i = 0; i < 15; i += 1) {
  128. var mod = (!test && ((bits >> i) & 1) == 1);
  129. if (i < 8) {
  130. _modules[8][_moduleCount - i - 1] = mod;
  131. } else if (i < 9) {
  132. _modules[8][15 - i - 1 + 1] = mod;
  133. } else {
  134. _modules[8][15 - i - 1] = mod;
  135. }
  136. }
  137. // fixed module
  138. _modules[_moduleCount - 8][8] = (!test);
  139. };
  140. var mapData = function (data, maskPattern) {
  141. var inc = -1;
  142. var row = _moduleCount - 1;
  143. var bitIndex = 7;
  144. var byteIndex = 0;
  145. var maskFunc = QRUtil.getMaskFunction(maskPattern);
  146. for (var col = _moduleCount - 1; col > 0; col -= 2) {
  147. if (col == 6) col -= 1;
  148. while (true) {
  149. for (var c = 0; c < 2; c += 1) {
  150. if (_modules[row][col - c] == null) {
  151. var dark = false;
  152. if (byteIndex < data.length) {
  153. dark = (((data[byteIndex] >>> bitIndex) & 1) == 1);
  154. }
  155. var mask = maskFunc(row, col - c);
  156. if (mask) {
  157. dark = !dark;
  158. }
  159. _modules[row][col - c] = dark;
  160. bitIndex -= 1;
  161. if (bitIndex == -1) {
  162. byteIndex += 1;
  163. bitIndex = 7;
  164. }
  165. }
  166. }
  167. row += inc;
  168. if (row < 0 || _moduleCount <= row) {
  169. row -= inc;
  170. inc = -inc;
  171. break;
  172. }
  173. }
  174. }
  175. };
  176. var createBytes = function (buffer, rsBlocks) {
  177. var offset = 0;
  178. var maxDcCount = 0;
  179. var maxEcCount = 0;
  180. var dcdata = new Array(rsBlocks.length);
  181. var ecdata = new Array(rsBlocks.length);
  182. for (var r = 0; r < rsBlocks.length; r += 1) {
  183. var dcCount = rsBlocks[r].dataCount;
  184. var ecCount = rsBlocks[r].totalCount - dcCount;
  185. maxDcCount = Math.max(maxDcCount, dcCount);
  186. maxEcCount = Math.max(maxEcCount, ecCount);
  187. dcdata[r] = new Array(dcCount);
  188. for (var i = 0; i < dcdata[r].length; i += 1) {
  189. dcdata[r][i] = 0xff & buffer.getBuffer()[i + offset];
  190. }
  191. offset += dcCount;
  192. var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount);
  193. var rawPoly = qrPolynomial(dcdata[r], rsPoly.getLength() - 1);
  194. var modPoly = rawPoly.mod(rsPoly);
  195. ecdata[r] = new Array(rsPoly.getLength() - 1);
  196. for (var i = 0; i < ecdata[r].length; i += 1) {
  197. var modIndex = i + modPoly.getLength() - ecdata[r].length;
  198. ecdata[r][i] = (modIndex >= 0) ? modPoly.getAt(modIndex) : 0;
  199. }
  200. }
  201. var totalCodeCount = 0;
  202. for (var i = 0; i < rsBlocks.length; i += 1) {
  203. totalCodeCount += rsBlocks[i].totalCount;
  204. }
  205. var data = new Array(totalCodeCount);
  206. var index = 0;
  207. for (var i = 0; i < maxDcCount; i += 1) {
  208. for (var r = 0; r < rsBlocks.length; r += 1) {
  209. if (i < dcdata[r].length) {
  210. data[index] = dcdata[r][i];
  211. index += 1;
  212. }
  213. }
  214. }
  215. for (var i = 0; i < maxEcCount; i += 1) {
  216. for (var r = 0; r < rsBlocks.length; r += 1) {
  217. if (i < ecdata[r].length) {
  218. data[index] = ecdata[r][i];
  219. index += 1;
  220. }
  221. }
  222. }
  223. return data;
  224. };
  225. var createData = function (typeNumber, errorCorrectLevel, dataList) {
  226. var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, errorCorrectLevel);
  227. var buffer = qrBitBuffer();
  228. for (var i = 0; i < dataList.length; i += 1) {
  229. var data = dataList[i];
  230. buffer.put(data.getMode(), 4);
  231. buffer.put(data.getLength(), QRUtil.getLengthInBits(data.getMode(), typeNumber));
  232. data.write(buffer);
  233. }
  234. // calc num max data.
  235. var totalDataCount = 0;
  236. for (var i = 0; i < rsBlocks.length; i += 1) {
  237. totalDataCount += rsBlocks[i].dataCount;
  238. }
  239. if (buffer.getLengthInBits() > totalDataCount * 8) {
  240. throw new Error('code length overflow. (' +
  241. buffer.getLengthInBits() +
  242. '>' +
  243. totalDataCount * 8 +
  244. ')');
  245. }
  246. // end code
  247. if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
  248. buffer.put(0, 4);
  249. }
  250. // padding
  251. while (buffer.getLengthInBits() % 8 != 0) {
  252. buffer.putBit(false);
  253. }
  254. // padding
  255. while (true) {
  256. if (buffer.getLengthInBits() >= totalDataCount * 8) {
  257. break;
  258. }
  259. buffer.put(PAD0, 8);
  260. if (buffer.getLengthInBits() >= totalDataCount * 8) {
  261. break;
  262. }
  263. buffer.put(PAD1, 8);
  264. }
  265. return createBytes(buffer, rsBlocks);
  266. };
  267. _this.addData = function (data) {
  268. var newData = qr8BitByte(data);
  269. _dataList.push(newData);
  270. _dataCache = null;
  271. };
  272. _this.isDark = function (row, col) {
  273. if (row < 0 || _moduleCount <= row || col < 0 || _moduleCount <= col) {
  274. throw new Error(row + ',' + col);
  275. }
  276. return _modules[row][col];
  277. };
  278. _this.getModuleCount = function () {
  279. return _moduleCount;
  280. };
  281. _this.make = function () {
  282. makeImpl(false, getBestMaskPattern());
  283. };
  284. _this.createTableTag = function (cellSize, margin) {
  285. cellSize = cellSize || 2;
  286. margin = (typeof margin == 'undefined') ? cellSize * 4 : margin;
  287. var qrHtml = '';
  288. qrHtml += '<table style="';
  289. qrHtml += ' border-width: 0px; border-style: none;';
  290. qrHtml += ' border-collapse: collapse;';
  291. qrHtml += ' padding: 0px; margin: ' + margin + 'px;';
  292. qrHtml += '">';
  293. qrHtml += '<tbody>';
  294. for (var r = 0; r < _this.getModuleCount(); r += 1) {
  295. qrHtml += '<tr>';
  296. for (var c = 0; c < _this.getModuleCount(); c += 1) {
  297. qrHtml += '<td style="';
  298. qrHtml += ' border-width: 0px; border-style: none;';
  299. qrHtml += ' border-collapse: collapse;';
  300. qrHtml += ' padding: 0px; margin: 0px;';
  301. qrHtml += ' width: ' + cellSize + 'px;';
  302. qrHtml += ' height: ' + cellSize + 'px;';
  303. qrHtml += ' background-color: ';
  304. qrHtml += _this.isDark(r, c) ? '#000000' : '#ffffff';
  305. qrHtml += ';';
  306. qrHtml += '"/>';
  307. }
  308. qrHtml += '</tr>';
  309. }
  310. qrHtml += '</tbody>';
  311. qrHtml += '</table>';
  312. return qrHtml;
  313. };
  314. _this.createImgTag = function (cellSize, margin, size) {
  315. cellSize = cellSize || 2;
  316. margin = (typeof margin == 'undefined') ? cellSize * 4 : margin;
  317. var min = margin;
  318. var max = _this.getModuleCount() * cellSize + margin;
  319. return createImgTag(size, size, function (x, y) {
  320. if (min <= x && x < max && min <= y && y < max) {
  321. var c = Math.floor((x - min) / cellSize);
  322. var r = Math.floor((y - min) / cellSize);
  323. return _this.isDark(r, c) ? 0 : 1;
  324. } else {
  325. return 1;
  326. }
  327. });
  328. };
  329. return _this;
  330. };
  331. //---------------------------------------------------------------------
  332. // qrcode.stringToBytes
  333. //---------------------------------------------------------------------
  334. qrcode.stringToBytes = function (s) {
  335. var bytes = new Array();
  336. for (var i = 0; i < s.length; i += 1) {
  337. var c = s.charCodeAt(i);
  338. bytes.push(c & 0xff);
  339. }
  340. return bytes;
  341. };
  342. //---------------------------------------------------------------------
  343. // qrcode.createStringToBytes
  344. //---------------------------------------------------------------------
  345. /**
  346. * @param unicodeData base64 string of byte array.
  347. * [16bit Unicode],[16bit Bytes], ...
  348. * @param numChars
  349. */
  350. qrcode.createStringToBytes = function (unicodeData, numChars) {
  351. // create conversion map.
  352. var unicodeMap = function () {
  353. var bin = base64DecodeInputStream(unicodeData);
  354. var read = function () {
  355. var b = bin.read();
  356. if (b == -1) throw new Error();
  357. return b;
  358. };
  359. var count = 0;
  360. var unicodeMap = {};
  361. while (true) {
  362. var b0 = bin.read();
  363. if (b0 == -1) break;
  364. var b1 = read();
  365. var b2 = read();
  366. var b3 = read();
  367. var k = String.fromCharCode((b0 << 8) | b1);
  368. var v = (b2 << 8) | b3;
  369. unicodeMap[k] = v;
  370. count += 1;
  371. }
  372. if (count != numChars) {
  373. throw new Error(count + ' != ' + numChars);
  374. }
  375. return unicodeMap;
  376. }();
  377. var unknownChar = '?'.charCodeAt(0);
  378. return function (s) {
  379. var bytes = new Array();
  380. for (var i = 0; i < s.length; i += 1) {
  381. var c = s.charCodeAt(i);
  382. if (c < 128) {
  383. bytes.push(c);
  384. } else {
  385. var b = unicodeMap[s.charAt(i)];
  386. if (typeof b == 'number') {
  387. if ((b & 0xff) == b) {
  388. // 1byte
  389. bytes.push(b);
  390. } else {
  391. // 2bytes
  392. bytes.push(b >>> 8);
  393. bytes.push(b & 0xff);
  394. }
  395. } else {
  396. bytes.push(unknownChar);
  397. }
  398. }
  399. }
  400. return bytes;
  401. };
  402. };
  403. //---------------------------------------------------------------------
  404. // QRMode
  405. //---------------------------------------------------------------------
  406. var QRMode = {
  407. MODE_NUMBER: 1 << 0,
  408. MODE_ALPHA_NUM: 1 << 1,
  409. MODE_8BIT_BYTE: 1 << 2,
  410. MODE_KANJI: 1 << 3
  411. };
  412. //---------------------------------------------------------------------
  413. // QRErrorCorrectLevel
  414. //---------------------------------------------------------------------
  415. var QRErrorCorrectLevel = {
  416. L: 1,
  417. M: 0,
  418. Q: 3,
  419. H: 2
  420. };
  421. //---------------------------------------------------------------------
  422. // QRMaskPattern
  423. //---------------------------------------------------------------------
  424. var QRMaskPattern = {
  425. PATTERN000: 0,
  426. PATTERN001: 1,
  427. PATTERN010: 2,
  428. PATTERN011: 3,
  429. PATTERN100: 4,
  430. PATTERN101: 5,
  431. PATTERN110: 6,
  432. PATTERN111: 7
  433. };
  434. //---------------------------------------------------------------------
  435. // QRUtil
  436. //---------------------------------------------------------------------
  437. var QRUtil = function () {
  438. var PATTERN_POSITION_TABLE = [
  439. [],
  440. [6, 18],
  441. [6, 22],
  442. [6, 26],
  443. [6, 30],
  444. [6, 34],
  445. [6, 22, 38],
  446. [6, 24, 42],
  447. [6, 26, 46],
  448. [6, 28, 50],
  449. [6, 30, 54],
  450. [6, 32, 58],
  451. [6, 34, 62],
  452. [6, 26, 46, 66],
  453. [6, 26, 48, 70],
  454. [6, 26, 50, 74],
  455. [6, 30, 54, 78],
  456. [6, 30, 56, 82],
  457. [6, 30, 58, 86],
  458. [6, 34, 62, 90],
  459. [6, 28, 50, 72, 94],
  460. [6, 26, 50, 74, 98],
  461. [6, 30, 54, 78, 102],
  462. [6, 28, 54, 80, 106],
  463. [6, 32, 58, 84, 110],
  464. [6, 30, 58, 86, 114],
  465. [6, 34, 62, 90, 118],
  466. [6, 26, 50, 74, 98, 122],
  467. [6, 30, 54, 78, 102, 126],
  468. [6, 26, 52, 78, 104, 130],
  469. [6, 30, 56, 82, 108, 134],
  470. [6, 34, 60, 86, 112, 138],
  471. [6, 30, 58, 86, 114, 142],
  472. [6, 34, 62, 90, 118, 146],
  473. [6, 30, 54, 78, 102, 126, 150],
  474. [6, 24, 50, 76, 102, 128, 154],
  475. [6, 28, 54, 80, 106, 132, 158],
  476. [6, 32, 58, 84, 110, 136, 162],
  477. [6, 26, 54, 82, 110, 138, 166],
  478. [6, 30, 58, 86, 114, 142, 170]
  479. ];
  480. var G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
  481. var G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);
  482. var G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
  483. var _this = {};
  484. var getBCHDigit = function (data) {
  485. var digit = 0;
  486. while (data != 0) {
  487. digit += 1;
  488. data >>>= 1;
  489. }
  490. return digit;
  491. };
  492. _this.getBCHTypeInfo = function (data) {
  493. var d = data << 10;
  494. while (getBCHDigit(d) - getBCHDigit(G15) >= 0) {
  495. d ^= (G15 << (getBCHDigit(d) - getBCHDigit(G15)));
  496. }
  497. return ((data << 10) | d) ^ G15_MASK;
  498. };
  499. _this.getBCHTypeNumber = function (data) {
  500. var d = data << 12;
  501. while (getBCHDigit(d) - getBCHDigit(G18) >= 0) {
  502. d ^= (G18 << (getBCHDigit(d) - getBCHDigit(G18)));
  503. }
  504. return (data << 12) | d;
  505. };
  506. _this.getPatternPosition = function (typeNumber) {
  507. return PATTERN_POSITION_TABLE[typeNumber - 1];
  508. };
  509. _this.getMaskFunction = function (maskPattern) {
  510. switch (maskPattern) {
  511. case QRMaskPattern.PATTERN000:
  512. return function (i, j) {
  513. return (i + j) % 2 == 0;
  514. };
  515. case QRMaskPattern.PATTERN001:
  516. return function (i, j) {
  517. return i % 2 == 0;
  518. };
  519. case QRMaskPattern.PATTERN010:
  520. return function (i, j) {
  521. return j % 3 == 0;
  522. };
  523. case QRMaskPattern.PATTERN011:
  524. return function (i, j) {
  525. return (i + j) % 3 == 0;
  526. };
  527. case QRMaskPattern.PATTERN100:
  528. return function (i, j) {
  529. return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0;
  530. };
  531. case QRMaskPattern.PATTERN101:
  532. return function (i, j) {
  533. return (i * j) % 2 + (i * j) % 3 == 0;
  534. };
  535. case QRMaskPattern.PATTERN110:
  536. return function (i, j) {
  537. return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
  538. };
  539. case QRMaskPattern.PATTERN111:
  540. return function (i, j) {
  541. return ((i * j) % 3 + (i + j) % 2) % 2 == 0;
  542. };
  543. default:
  544. throw new Error('bad maskPattern:' + maskPattern);
  545. }
  546. };
  547. _this.getErrorCorrectPolynomial = function (errorCorrectLength) {
  548. var a = qrPolynomial([1], 0);
  549. for (var i = 0; i < errorCorrectLength; i += 1) {
  550. a = a.multiply(qrPolynomial([1, QRMath.gexp(i)], 0));
  551. }
  552. return a;
  553. };
  554. _this.getLengthInBits = function (mode, type) {
  555. if (1 <= type && type < 10) {
  556. // 1 - 9
  557. switch (mode) {
  558. case QRMode.MODE_NUMBER:
  559. return 10;
  560. case QRMode.MODE_ALPHA_NUM:
  561. return 9;
  562. case QRMode.MODE_8BIT_BYTE:
  563. return 8;
  564. case QRMode.MODE_KANJI:
  565. return 8;
  566. default:
  567. throw new Error('mode:' + mode);
  568. }
  569. } else if (type < 27) {
  570. // 10 - 26
  571. switch (mode) {
  572. case QRMode.MODE_NUMBER:
  573. return 12;
  574. case QRMode.MODE_ALPHA_NUM:
  575. return 11;
  576. case QRMode.MODE_8BIT_BYTE:
  577. return 16;
  578. case QRMode.MODE_KANJI:
  579. return 10;
  580. default:
  581. throw new Error('mode:' + mode);
  582. }
  583. } else if (type < 41) {
  584. // 27 - 40
  585. switch (mode) {
  586. case QRMode.MODE_NUMBER:
  587. return 14;
  588. case QRMode.MODE_ALPHA_NUM:
  589. return 13;
  590. case QRMode.MODE_8BIT_BYTE:
  591. return 16;
  592. case QRMode.MODE_KANJI:
  593. return 12;
  594. default:
  595. throw new Error('mode:' + mode);
  596. }
  597. } else {
  598. throw new Error('type:' + type);
  599. }
  600. };
  601. _this.getLostPoint = function (qrcode) {
  602. var moduleCount = qrcode.getModuleCount();
  603. var lostPoint = 0;
  604. // LEVEL1
  605. for (var row = 0; row < moduleCount; row += 1) {
  606. for (var col = 0; col < moduleCount; col += 1) {
  607. var sameCount = 0;
  608. var dark = qrcode.isDark(row, col);
  609. for (var r = -1; r <= 1; r += 1) {
  610. if (row + r < 0 || moduleCount <= row + r) {
  611. continue;
  612. }
  613. for (var c = -1; c <= 1; c += 1) {
  614. if (col + c < 0 || moduleCount <= col + c) {
  615. continue;
  616. }
  617. if (r == 0 && c == 0) {
  618. continue;
  619. }
  620. if (dark == qrcode.isDark(row + r, col + c)) {
  621. sameCount += 1;
  622. }
  623. }
  624. }
  625. if (sameCount > 5) {
  626. lostPoint += (3 + sameCount - 5);
  627. }
  628. }
  629. };
  630. // LEVEL2
  631. for (var row = 0; row < moduleCount - 1; row += 1) {
  632. for (var col = 0; col < moduleCount - 1; col += 1) {
  633. var count = 0;
  634. if (qrcode.isDark(row, col)) count += 1;
  635. if (qrcode.isDark(row + 1, col)) count += 1;
  636. if (qrcode.isDark(row, col + 1)) count += 1;
  637. if (qrcode.isDark(row + 1, col + 1)) count += 1;
  638. if (count == 0 || count == 4) {
  639. lostPoint += 3;
  640. }
  641. }
  642. }
  643. // LEVEL3
  644. for (var row = 0; row < moduleCount; row += 1) {
  645. for (var col = 0; col < moduleCount - 6; col += 1) {
  646. if (qrcode.isDark(row, col) &&
  647. !qrcode.isDark(row, col + 1) &&
  648. qrcode.isDark(row, col + 2) &&
  649. qrcode.isDark(row, col + 3) &&
  650. qrcode.isDark(row, col + 4) &&
  651. !qrcode.isDark(row, col + 5) &&
  652. qrcode.isDark(row, col + 6)) {
  653. lostPoint += 40;
  654. }
  655. }
  656. }
  657. for (var col = 0; col < moduleCount; col += 1) {
  658. for (var row = 0; row < moduleCount - 6; row += 1) {
  659. if (qrcode.isDark(row, col) &&
  660. !qrcode.isDark(row + 1, col) &&
  661. qrcode.isDark(row + 2, col) &&
  662. qrcode.isDark(row + 3, col) &&
  663. qrcode.isDark(row + 4, col) &&
  664. !qrcode.isDark(row + 5, col) &&
  665. qrcode.isDark(row + 6, col)) {
  666. lostPoint += 40;
  667. }
  668. }
  669. }
  670. // LEVEL4
  671. var darkCount = 0;
  672. for (var col = 0; col < moduleCount; col += 1) {
  673. for (var row = 0; row < moduleCount; row += 1) {
  674. if (qrcode.isDark(row, col)) {
  675. darkCount += 1;
  676. }
  677. }
  678. }
  679. var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5;
  680. lostPoint += ratio * 10;
  681. return lostPoint;
  682. };
  683. return _this;
  684. }();
  685. //---------------------------------------------------------------------
  686. // QRMath
  687. //---------------------------------------------------------------------
  688. var QRMath = function () {
  689. var EXP_TABLE = new Array(256);
  690. var LOG_TABLE = new Array(256);
  691. // initialize tables
  692. for (var i = 0; i < 8; i += 1) {
  693. EXP_TABLE[i] = 1 << i;
  694. }
  695. for (var i = 8; i < 256; i += 1) {
  696. EXP_TABLE[i] = EXP_TABLE[i - 4] ^
  697. EXP_TABLE[i - 5] ^
  698. EXP_TABLE[i - 6] ^
  699. EXP_TABLE[i - 8];
  700. }
  701. for (var i = 0; i < 255; i += 1) {
  702. LOG_TABLE[EXP_TABLE[i]] = i;
  703. }
  704. var _this = {};
  705. _this.glog = function (n) {
  706. if (n < 1) {
  707. throw new Error('glog(' + n + ')');
  708. }
  709. return LOG_TABLE[n];
  710. };
  711. _this.gexp = function (n) {
  712. while (n < 0) {
  713. n += 255;
  714. }
  715. while (n >= 256) {
  716. n -= 255;
  717. }
  718. return EXP_TABLE[n];
  719. };
  720. return _this;
  721. }();
  722. //---------------------------------------------------------------------
  723. // qrPolynomial
  724. //---------------------------------------------------------------------
  725. function qrPolynomial(num, shift) {
  726. if (typeof num.length == 'undefined') {
  727. throw new Error(num.length + '/' + shift);
  728. }
  729. var _num = function () {
  730. var offset = 0;
  731. while (offset < num.length && num[offset] == 0) {
  732. offset += 1;
  733. }
  734. var _num = new Array(num.length - offset + shift);
  735. for (var i = 0; i < num.length - offset; i += 1) {
  736. _num[i] = num[i + offset];
  737. }
  738. return _num;
  739. }();
  740. var _this = {};
  741. _this.getAt = function (index) {
  742. return _num[index];
  743. };
  744. _this.getLength = function () {
  745. return _num.length;
  746. };
  747. _this.multiply = function (e) {
  748. var num = new Array(_this.getLength() + e.getLength() - 1);
  749. for (var i = 0; i < _this.getLength(); i += 1) {
  750. for (var j = 0; j < e.getLength(); j += 1) {
  751. num[i + j] ^= QRMath.gexp(QRMath.glog(_this.getAt(i)) + QRMath.glog(e.getAt(j)));
  752. }
  753. }
  754. return qrPolynomial(num, 0);
  755. };
  756. _this.mod = function (e) {
  757. if (_this.getLength() - e.getLength() < 0) {
  758. return _this;
  759. }
  760. var ratio = QRMath.glog(_this.getAt(0)) - QRMath.glog(e.getAt(0));
  761. var num = new Array(_this.getLength());
  762. for (var i = 0; i < _this.getLength(); i += 1) {
  763. num[i] = _this.getAt(i);
  764. }
  765. for (var i = 0; i < e.getLength(); i += 1) {
  766. num[i] ^= QRMath.gexp(QRMath.glog(e.getAt(i)) + ratio);
  767. }
  768. // recursive call
  769. return qrPolynomial(num, 0).mod(e);
  770. };
  771. return _this;
  772. };
  773. //---------------------------------------------------------------------
  774. // QRRSBlock
  775. //---------------------------------------------------------------------
  776. var QRRSBlock = function () {
  777. // [1: [L, M, Q, H], ..]
  778. var RS_BLOCK_TABLE = [
  779. [1, 26, 19],
  780. [1, 26, 16],
  781. [1, 26, 13],
  782. [1, 26, 9],
  783. [1, 44, 34],
  784. [1, 44, 28],
  785. [1, 44, 22],
  786. [1, 44, 16],
  787. [1, 70, 55],
  788. [1, 70, 44],
  789. [2, 35, 17],
  790. [2, 35, 13],
  791. [1, 100, 80],
  792. [2, 50, 32],
  793. [2, 50, 24],
  794. [4, 25, 9],
  795. [1, 134, 108],
  796. [2, 67, 43],
  797. [2, 33, 15, 2, 34, 16],
  798. [2, 33, 11, 2, 34, 12],
  799. [2, 86, 68],
  800. [4, 43, 27],
  801. [4, 43, 19],
  802. [4, 43, 15],
  803. [2, 98, 78],
  804. [4, 49, 31],
  805. [2, 32, 14, 4, 33, 15],
  806. [4, 39, 13, 1, 40, 14],
  807. [2, 121, 97],
  808. [2, 60, 38, 2, 61, 39],
  809. [4, 40, 18, 2, 41, 19],
  810. [4, 40, 14, 2, 41, 15],
  811. [2, 146, 116],
  812. [3, 58, 36, 2, 59, 37],
  813. [4, 36, 16, 4, 37, 17],
  814. [4, 36, 12, 4, 37, 13],
  815. [2, 86, 68, 2, 87, 69],
  816. [4, 69, 43, 1, 70, 44],
  817. [6, 43, 19, 2, 44, 20],
  818. [6, 43, 15, 2, 44, 16],
  819. [4, 101, 81],
  820. [1, 80, 50, 4, 81, 51],
  821. [4, 50, 22, 4, 51, 23],
  822. [3, 36, 12, 8, 37, 13],
  823. [2, 116, 92, 2, 117, 93],
  824. [6, 58, 36, 2, 59, 37],
  825. [4, 46, 20, 6, 47, 21],
  826. [7, 42, 14, 4, 43, 15],
  827. [4, 133, 107],
  828. [8, 59, 37, 1, 60, 38],
  829. [8, 44, 20, 4, 45, 21],
  830. [12, 33, 11, 4, 34, 12],
  831. [3, 145, 115, 1, 146, 116],
  832. [4, 64, 40, 5, 65, 41],
  833. [11, 36, 16, 5, 37, 17],
  834. [11, 36, 12, 5, 37, 13],
  835. [5, 109, 87, 1, 110, 88],
  836. [5, 65, 41, 5, 66, 42],
  837. [5, 54, 24, 7, 55, 25],
  838. [11, 36, 12],
  839. [5, 122, 98, 1, 123, 99],
  840. [7, 73, 45, 3, 74, 46],
  841. [15, 43, 19, 2, 44, 20],
  842. [3, 45, 15, 13, 46, 16],
  843. [1, 135, 107, 5, 136, 108],
  844. [10, 74, 46, 1, 75, 47],
  845. [1, 50, 22, 15, 51, 23],
  846. [2, 42, 14, 17, 43, 15],
  847. [5, 150, 120, 1, 151, 121],
  848. [9, 69, 43, 4, 70, 44],
  849. [17, 50, 22, 1, 51, 23],
  850. [2, 42, 14, 19, 43, 15],
  851. [3, 141, 113, 4, 142, 114],
  852. [3, 70, 44, 11, 71, 45],
  853. [17, 47, 21, 4, 48, 22],
  854. [9, 39, 13, 16, 40, 14],
  855. [3, 135, 107, 5, 136, 108],
  856. [3, 67, 41, 13, 68, 42],
  857. [15, 54, 24, 5, 55, 25],
  858. [15, 43, 15, 10, 44, 16],
  859. [4, 144, 116, 4, 145, 117],
  860. [17, 68, 42],
  861. [17, 50, 22, 6, 51, 23],
  862. [19, 46, 16, 6, 47, 17],
  863. [2, 139, 111, 7, 140, 112],
  864. [17, 74, 46],
  865. [7, 54, 24, 16, 55, 25],
  866. [34, 37, 13],
  867. [4, 151, 121, 5, 152, 122],
  868. [4, 75, 47, 14, 76, 48],
  869. [11, 54, 24, 14, 55, 25],
  870. [16, 45, 15, 14, 46, 16],
  871. [6, 147, 117, 4, 148, 118],
  872. [6, 73, 45, 14, 74, 46],
  873. [11, 54, 24, 16, 55, 25],
  874. [30, 46, 16, 2, 47, 17],
  875. [8, 132, 106, 4, 133, 107],
  876. [8, 75, 47, 13, 76, 48],
  877. [7, 54, 24, 22, 55, 25],
  878. [22, 45, 15, 13, 46, 16],
  879. [10, 142, 114, 2, 143, 115],
  880. [19, 74, 46, 4, 75, 47],
  881. [28, 50, 22, 6, 51, 23],
  882. [33, 46, 16, 4, 47, 17],
  883. [8, 152, 122, 4, 153, 123],
  884. [22, 73, 45, 3, 74, 46],
  885. [8, 53, 23, 26, 54, 24],
  886. [12, 45, 15, 28, 46, 16],
  887. [3, 147, 117, 10, 148, 118],
  888. [3, 73, 45, 23, 74, 46],
  889. [4, 54, 24, 31, 55, 25],
  890. [11, 45, 15, 31, 46, 16],
  891. [7, 146, 116, 7, 147, 117],
  892. [21, 73, 45, 7, 74, 46],
  893. [1, 53, 23, 37, 54, 24],
  894. [19, 45, 15, 26, 46, 16],
  895. [5, 145, 115, 10, 146, 116],
  896. [19, 75, 47, 10, 76, 48],
  897. [15, 54, 24, 25, 55, 25],
  898. [23, 45, 15, 25, 46, 16],
  899. [13, 145, 115, 3, 146, 116],
  900. [2, 74, 46, 29, 75, 47],
  901. [42, 54, 24, 1, 55, 25],
  902. [23, 45, 15, 28, 46, 16],
  903. [17, 145, 115],
  904. [10, 74, 46, 23, 75, 47],
  905. [10, 54, 24, 35, 55, 25],
  906. [19, 45, 15, 35, 46, 16],
  907. [17, 145, 115, 1, 146, 116],
  908. [14, 74, 46, 21, 75, 47],
  909. [29, 54, 24, 19, 55, 25],
  910. [11, 45, 15, 46, 46, 16],
  911. [13, 145, 115, 6, 146, 116],
  912. [14, 74, 46, 23, 75, 47],
  913. [44, 54, 24, 7, 55, 25],
  914. [59, 46, 16, 1, 47, 17],
  915. [12, 151, 121, 7, 152, 122],
  916. [12, 75, 47, 26, 76, 48],
  917. [39, 54, 24, 14, 55, 25],
  918. [22, 45, 15, 41, 46, 16],
  919. [6, 151, 121, 14, 152, 122],
  920. [6, 75, 47, 34, 76, 48],
  921. [46, 54, 24, 10, 55, 25],
  922. [2, 45, 15, 64, 46, 16],
  923. [17, 152, 122, 4, 153, 123],
  924. [29, 74, 46, 14, 75, 47],
  925. [49, 54, 24, 10, 55, 25],
  926. [24, 45, 15, 46, 46, 16],
  927. [4, 152, 122, 18, 153, 123],
  928. [13, 74, 46, 32, 75, 47],
  929. [48, 54, 24, 14, 55, 25],
  930. [42, 45, 15, 32, 46, 16],
  931. [20, 147, 117, 4, 148, 118],
  932. [40, 75, 47, 7, 76, 48],
  933. [43, 54, 24, 22, 55, 25],
  934. [10, 45, 15, 67, 46, 16],
  935. [19, 148, 118, 6, 149, 119],
  936. [18, 75, 47, 31, 76, 48],
  937. [34, 54, 24, 34, 55, 25],
  938. [20, 45, 15, 61, 46, 16]
  939. ];
  940. var qrRSBlock = function (totalCount, dataCount) {
  941. var _this = {};
  942. _this.totalCount = totalCount;
  943. _this.dataCount = dataCount;
  944. return _this;
  945. };
  946. var _this = {};
  947. var getRsBlockTable = function (typeNumber, errorCorrectLevel) {
  948. switch (errorCorrectLevel) {
  949. case QRErrorCorrectLevel.L:
  950. return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
  951. case QRErrorCorrectLevel.M:
  952. return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
  953. case QRErrorCorrectLevel.Q:
  954. return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
  955. case QRErrorCorrectLevel.H:
  956. return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
  957. default:
  958. return undefined;
  959. }
  960. };
  961. _this.getRSBlocks = function (typeNumber, errorCorrectLevel) {
  962. var rsBlock = getRsBlockTable(typeNumber, errorCorrectLevel);
  963. if (typeof rsBlock == 'undefined') {
  964. throw new Error('bad rs block @ typeNumber:' + typeNumber +
  965. '/errorCorrectLevel:' + errorCorrectLevel);
  966. }
  967. var length = rsBlock.length / 3;
  968. var list = new Array();
  969. for (var i = 0; i < length; i += 1) {
  970. var count = rsBlock[i * 3 + 0];
  971. var totalCount = rsBlock[i * 3 + 1];
  972. var dataCount = rsBlock[i * 3 + 2];
  973. for (var j = 0; j < count; j += 1) {
  974. list.push(qrRSBlock(totalCount, dataCount));
  975. }
  976. }
  977. return list;
  978. };
  979. return _this;
  980. }();
  981. //---------------------------------------------------------------------
  982. // qrBitBuffer
  983. //---------------------------------------------------------------------
  984. var qrBitBuffer = function () {
  985. var _buffer = new Array();
  986. var _length = 0;
  987. var _this = {};
  988. _this.getBuffer = function () {
  989. return _buffer;
  990. };
  991. _this.getAt = function (index) {
  992. var bufIndex = Math.floor(index / 8);
  993. return ((_buffer[bufIndex] >>> (7 - index % 8)) & 1) == 1;
  994. };
  995. _this.put = function (num, length) {
  996. for (var i = 0; i < length; i += 1) {
  997. _this.putBit(((num >>> (length - i - 1)) & 1) == 1);
  998. }
  999. };
  1000. _this.getLengthInBits = function () {
  1001. return _length;
  1002. };
  1003. _this.putBit = function (bit) {
  1004. var bufIndex = Math.floor(_length / 8);
  1005. if (_buffer.length <= bufIndex) {
  1006. _buffer.push(0);
  1007. }
  1008. if (bit) {
  1009. _buffer[bufIndex] |= (0x80 >>> (_length % 8));
  1010. }
  1011. _length += 1;
  1012. };
  1013. return _this;
  1014. };
  1015. //---------------------------------------------------------------------
  1016. // qr8BitByte
  1017. //---------------------------------------------------------------------
  1018. var qr8BitByte = function (data) {
  1019. var _mode = QRMode.MODE_8BIT_BYTE;
  1020. var _data = data;
  1021. var _parsedData = [];
  1022. var _this = {};
  1023. // Added to support UTF-8 Characters
  1024. for (var i = 0, l = _data.length; i < l; i++) {
  1025. var byteArray = [];
  1026. var code = _data.charCodeAt(i);
  1027. if (code > 0x10000) {
  1028. byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18);
  1029. byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12);
  1030. byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6);
  1031. byteArray[3] = 0x80 | (code & 0x3F);
  1032. } else if (code > 0x800) {
  1033. byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12);
  1034. byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6);
  1035. byteArray[2] = 0x80 | (code & 0x3F);
  1036. } else if (code > 0x80) {
  1037. byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6);
  1038. byteArray[1] = 0x80 | (code & 0x3F);
  1039. } else {
  1040. byteArray[0] = code;
  1041. }
  1042. // Fix Unicode corruption bug
  1043. _parsedData.push(byteArray);
  1044. }
  1045. _parsedData = Array.prototype.concat.apply([], _parsedData);
  1046. if (_parsedData.length != _data.length) {
  1047. _parsedData.unshift(191);
  1048. _parsedData.unshift(187);
  1049. _parsedData.unshift(239);
  1050. }
  1051. var _bytes = _parsedData;
  1052. _this.getMode = function () {
  1053. return _mode;
  1054. };
  1055. _this.getLength = function (buffer) {
  1056. return _bytes.length;
  1057. };
  1058. _this.write = function (buffer) {
  1059. for (var i = 0; i < _bytes.length; i += 1) {
  1060. buffer.put(_bytes[i], 8);
  1061. }
  1062. };
  1063. return _this;
  1064. };
  1065. //=====================================================================
  1066. // GIF Support etc.
  1067. //
  1068. //---------------------------------------------------------------------
  1069. // byteArrayOutputStream
  1070. //---------------------------------------------------------------------
  1071. var byteArrayOutputStream = function () {
  1072. var _bytes = new Array();
  1073. var _this = {};
  1074. _this.writeByte = function (b) {
  1075. _bytes.push(b & 0xff);
  1076. };
  1077. _this.writeShort = function (i) {
  1078. _this.writeByte(i);
  1079. _this.writeByte(i >>> 8);
  1080. };
  1081. _this.writeBytes = function (b, off, len) {
  1082. off = off || 0;
  1083. len = len || b.length;
  1084. for (var i = 0; i < len; i += 1) {
  1085. _this.writeByte(b[i + off]);
  1086. }
  1087. };
  1088. _this.writeString = function (s) {
  1089. for (var i = 0; i < s.length; i += 1) {
  1090. _this.writeByte(s.charCodeAt(i));
  1091. }
  1092. };
  1093. _this.toByteArray = function () {
  1094. return _bytes;
  1095. };
  1096. _this.toString = function () {
  1097. var s = '';
  1098. s += '[';
  1099. for (var i = 0; i < _bytes.length; i += 1) {
  1100. if (i > 0) {
  1101. s += ',';
  1102. }
  1103. s += _bytes[i];
  1104. }
  1105. s += ']';
  1106. return s;
  1107. };
  1108. return _this;
  1109. };
  1110. //---------------------------------------------------------------------
  1111. // base64EncodeOutputStream
  1112. //---------------------------------------------------------------------
  1113. var base64EncodeOutputStream = function () {
  1114. var _buffer = 0;
  1115. var _buflen = 0;
  1116. var _length = 0;
  1117. var _base64 = '';
  1118. var _this = {};
  1119. var writeEncoded = function (b) {
  1120. _base64 += String.fromCharCode(encode(b & 0x3f));
  1121. };
  1122. var encode = function (n) {
  1123. if (n < 0) {
  1124. // error.
  1125. } else if (n < 26) {
  1126. return 0x41 + n;
  1127. } else if (n < 52) {
  1128. return 0x61 + (n - 26);
  1129. } else if (n < 62) {
  1130. return 0x30 + (n - 52);
  1131. } else if (n == 62) {
  1132. return 0x2b;
  1133. } else if (n == 63) {
  1134. return 0x2f;
  1135. }
  1136. throw new Error('n:' + n);
  1137. };
  1138. _this.writeByte = function (n) {
  1139. _buffer = (_buffer << 8) | (n & 0xff);
  1140. _buflen += 8;
  1141. _length += 1;
  1142. while (_buflen >= 6) {
  1143. writeEncoded(_buffer >>> (_buflen - 6));
  1144. _buflen -= 6;
  1145. }
  1146. };
  1147. _this.flush = function () {
  1148. if (_buflen > 0) {
  1149. writeEncoded(_buffer << (6 - _buflen));
  1150. _buffer = 0;
  1151. _buflen = 0;
  1152. }
  1153. if (_length % 3 != 0) {
  1154. // padding
  1155. var padlen = 3 - _length % 3;
  1156. for (var i = 0; i < padlen; i += 1) {
  1157. _base64 += '=';
  1158. }
  1159. }
  1160. };
  1161. _this.toString = function () {
  1162. return _base64;
  1163. };
  1164. return _this;
  1165. };
  1166. //---------------------------------------------------------------------
  1167. // base64DecodeInputStream
  1168. //---------------------------------------------------------------------
  1169. var base64DecodeInputStream = function (str) {
  1170. var _str = str;
  1171. var _pos = 0;
  1172. var _buffer = 0;
  1173. var _buflen = 0;
  1174. var _this = {};
  1175. _this.read = function () {
  1176. while (_buflen < 8) {
  1177. if (_pos >= _str.length) {
  1178. if (_buflen == 0) {
  1179. return -1;
  1180. }
  1181. throw new Error('unexpected end of file./' + _buflen);
  1182. }
  1183. var c = _str.charAt(_pos);
  1184. _pos += 1;
  1185. if (c == '=') {
  1186. _buflen = 0;
  1187. return -1;
  1188. } else if (c.match(/^\s$/)) {
  1189. // ignore if whitespace.
  1190. continue;
  1191. }
  1192. _buffer = (_buffer << 6) | decode(c.charCodeAt(0));
  1193. _buflen += 6;
  1194. }
  1195. var n = (_buffer >>> (_buflen - 8)) & 0xff;
  1196. _buflen -= 8;
  1197. return n;
  1198. };
  1199. var decode = function (c) {
  1200. if (0x41 <= c && c <= 0x5a) {
  1201. return c - 0x41;
  1202. } else if (0x61 <= c && c <= 0x7a) {
  1203. return c - 0x61 + 26;
  1204. } else if (0x30 <= c && c <= 0x39) {
  1205. return c - 0x30 + 52;
  1206. } else if (c == 0x2b) {
  1207. return 62;
  1208. } else if (c == 0x2f) {
  1209. return 63;
  1210. } else {
  1211. throw new Error('c:' + c);
  1212. }
  1213. };
  1214. return _this;
  1215. };
  1216. //---------------------------------------------------------------------
  1217. // gifImage (B/W)
  1218. //---------------------------------------------------------------------
  1219. var gifImage = function (width, height) {
  1220. var _width = width;
  1221. var _height = height;
  1222. var _data = new Array(width * height);
  1223. var _this = {};
  1224. _this.setPixel = function (x, y, pixel) {
  1225. _data[y * _width + x] = pixel;
  1226. };
  1227. _this.write = function (out) {
  1228. //---------------------------------
  1229. // GIF Signature
  1230. out.writeString('GIF87a');
  1231. //---------------------------------
  1232. // Screen Descriptor
  1233. out.writeShort(_width);
  1234. out.writeShort(_height);
  1235. out.writeByte(0x80); // 2bit
  1236. out.writeByte(0);
  1237. out.writeByte(0);
  1238. //---------------------------------
  1239. // Global Color Map
  1240. // black
  1241. out.writeByte(0x00);
  1242. out.writeByte(0x00);
  1243. out.writeByte(0x00);
  1244. // white
  1245. out.writeByte(0xff);
  1246. out.writeByte(0xff);
  1247. out.writeByte(0xff);
  1248. //---------------------------------
  1249. // Image Descriptor
  1250. out.writeString(',');
  1251. out.writeShort(0);
  1252. out.writeShort(0);
  1253. out.writeShort(_width);
  1254. out.writeShort(_height);
  1255. out.writeByte(0);
  1256. //---------------------------------
  1257. // Local Color Map
  1258. //---------------------------------
  1259. // Raster Data
  1260. var lzwMinCodeSize = 2;
  1261. var raster = getLZWRaster(lzwMinCodeSize);
  1262. out.writeByte(lzwMinCodeSize);
  1263. var offset = 0;
  1264. while (raster.length - offset > 255) {
  1265. out.writeByte(255);
  1266. out.writeBytes(raster, offset, 255);
  1267. offset += 255;
  1268. }
  1269. out.writeByte(raster.length - offset);
  1270. out.writeBytes(raster, offset, raster.length - offset);
  1271. out.writeByte(0x00);
  1272. //---------------------------------
  1273. // GIF Terminator
  1274. out.writeString(';');
  1275. };
  1276. var bitOutputStream = function (out) {
  1277. var _out = out;
  1278. var _bitLength = 0;
  1279. var _bitBuffer = 0;
  1280. var _this = {};
  1281. _this.write = function (data, length) {
  1282. if ((data >>> length) != 0) {
  1283. throw new Error('length over');
  1284. }
  1285. while (_bitLength + length >= 8) {
  1286. _out.writeByte(0xff & ((data << _bitLength) | _bitBuffer));
  1287. length -= (8 - _bitLength);
  1288. data >>>= (8 - _bitLength);
  1289. _bitBuffer = 0;
  1290. _bitLength = 0;
  1291. }
  1292. _bitBuffer = (data << _bitLength) | _bitBuffer;
  1293. _bitLength = _bitLength + length;
  1294. };
  1295. _this.flush = function () {
  1296. if (_bitLength > 0) {
  1297. _out.writeByte(_bitBuffer);
  1298. }
  1299. };
  1300. return _this;
  1301. };
  1302. var getLZWRaster = function (lzwMinCodeSize) {
  1303. var clearCode = 1 << lzwMinCodeSize;
  1304. var endCode = (1 << lzwMinCodeSize) + 1;
  1305. var bitLength = lzwMinCodeSize + 1;
  1306. // Setup LZWTable
  1307. var table = lzwTable();
  1308. for (var i = 0; i < clearCode; i += 1) {
  1309. table.add(String.fromCharCode(i));
  1310. }
  1311. table.add(String.fromCharCode(clearCode));
  1312. table.add(String.fromCharCode(endCode));
  1313. var byteOut = byteArrayOutputStream();
  1314. var bitOut = bitOutputStream(byteOut);
  1315. // clear code
  1316. bitOut.write(clearCode, bitLength);
  1317. var dataIndex = 0;
  1318. var s = String.fromCharCode(_data[dataIndex]);
  1319. dataIndex += 1;
  1320. while (dataIndex < _data.length) {
  1321. var c = String.fromCharCode(_data[dataIndex]);
  1322. dataIndex += 1;
  1323. if (table.contains(s + c)) {
  1324. s = s + c;
  1325. } else {
  1326. bitOut.write(table.indexOf(s), bitLength);
  1327. if (table.size() < 0xfff) {
  1328. if (table.size() == (1 << bitLength)) {
  1329. bitLength += 1;
  1330. }
  1331. table.add(s + c);
  1332. }
  1333. s = c;
  1334. }
  1335. }
  1336. bitOut.write(table.indexOf(s), bitLength);
  1337. // end code
  1338. bitOut.write(endCode, bitLength);
  1339. bitOut.flush();
  1340. return byteOut.toByteArray();
  1341. };
  1342. var lzwTable = function () {
  1343. var _map = {};
  1344. var _size = 0;
  1345. var _this = {};
  1346. _this.add = function (key) {
  1347. if (_this.contains(key)) {
  1348. throw new Error('dup key:' + key);
  1349. }
  1350. _map[key] = _size;
  1351. _size += 1;
  1352. };
  1353. _this.size = function () {
  1354. return _size;
  1355. };
  1356. _this.indexOf = function (key) {
  1357. return _map[key];
  1358. };
  1359. _this.contains = function (key) {
  1360. return typeof _map[key] != 'undefined';
  1361. };
  1362. return _this;
  1363. };
  1364. return _this;
  1365. };
  1366. var createImgTag = function (width, height, getPixel, alt) {
  1367. var gif = gifImage(width, height);
  1368. for (var y = 0; y < height; y += 1) {
  1369. for (var x = 0; x < width; x += 1) {
  1370. gif.setPixel(x, y, getPixel(x, y));
  1371. }
  1372. }
  1373. var b = byteArrayOutputStream();
  1374. gif.write(b);
  1375. var base64 = base64EncodeOutputStream();
  1376. var bytes = b.toByteArray();
  1377. for (var i = 0; i < bytes.length; i += 1) {
  1378. base64.writeByte(bytes[i]);
  1379. }
  1380. base64.flush();
  1381. var img = '';
  1382. img += 'data:image/png;base64,';
  1383. img += base64;
  1384. return img;
  1385. };
  1386. //---------------------------------------------------------------------
  1387. // returns qrcode function.
  1388. var createQrCodeImg = function (text, options) {
  1389. options = options || {};
  1390. var typeNumber = options.typeNumber || 4;
  1391. var errorCorrectLevel = options.errorCorrectLevel || 'M';
  1392. var size = options.size || 500;
  1393. var qr;
  1394. try {
  1395. qr = qrcode(typeNumber, errorCorrectLevel || 'M');
  1396. qr.addData(text);
  1397. qr.make();
  1398. } catch (e) {
  1399. if (typeNumber >= 40) {
  1400. throw new Error('Text too long to encode');
  1401. } else {
  1402. return createQrCodeImg(text, {
  1403. size: size,
  1404. errorCorrectLevel: errorCorrectLevel,
  1405. typeNumber: typeNumber + 1
  1406. });
  1407. }
  1408. }
  1409. // calc cellsize and margin
  1410. var cellsize = parseInt(size / qr.getModuleCount());
  1411. var margin = parseInt((size - qr.getModuleCount() * cellsize) / 2);
  1412. return qr.createImgTag(cellsize, margin, size);
  1413. };
  1414. module.exports = {
  1415. createQrCodeImg: createQrCodeImg
  1416. };