weapp-qrcode.js 40 KB

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