QRCode.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. var BitByte = require('./8BitByte');
  2. var RSBlock = require('./RSBlock');
  3. var BitBuffer = require('./BitBuffer');
  4. var util = require('./util');
  5. var Polynomial = require('./Polynomial');
  6. function QRCode(typeNumber, errorCorrectLevel) {
  7. this.typeNumber = typeNumber;
  8. this.errorCorrectLevel = errorCorrectLevel;
  9. this.modules = null;
  10. this.moduleCount = 0;
  11. this.dataCache = null;
  12. this.dataList = [];
  13. }
  14. // for client side minification
  15. var proto = QRCode.prototype;
  16. proto.addData = function(data) {
  17. var newData = new BitByte(data);
  18. this.dataList.push(newData);
  19. this.dataCache = null;
  20. };
  21. proto.isDark = function(row, col) {
  22. if (row < 0 || this.moduleCount <= row || col < 0 || this.moduleCount <= col) {
  23. throw new Error(row + "," + col);
  24. }
  25. return this.modules[row][col];
  26. };
  27. proto.getModuleCount = function() {
  28. return this.moduleCount;
  29. };
  30. proto.make = function() {
  31. // Calculate automatically typeNumber if provided is < 1
  32. if (this.typeNumber < 1 ){
  33. var typeNumber = 1;
  34. for (typeNumber = 1; typeNumber < 40; typeNumber++) {
  35. var rsBlocks = RSBlock.getRSBlocks(typeNumber, this.errorCorrectLevel);
  36. var buffer = new BitBuffer();
  37. var totalDataCount = 0;
  38. for (var i = 0; i < rsBlocks.length; i++) {
  39. totalDataCount += rsBlocks[i].dataCount;
  40. }
  41. for (var i = 0; i < this.dataList.length; i++) {
  42. var data = this.dataList[i];
  43. buffer.put(data.mode, 4);
  44. buffer.put(data.getLength(), util.getLengthInBits(data.mode, typeNumber) );
  45. data.write(buffer);
  46. }
  47. if (buffer.getLengthInBits() <= totalDataCount * 8)
  48. break;
  49. }
  50. this.typeNumber = typeNumber;
  51. }
  52. this.makeImpl(false, this.getBestMaskPattern() );
  53. };
  54. proto.makeImpl = function(test, maskPattern) {
  55. this.moduleCount = this.typeNumber * 4 + 17;
  56. this.modules = new Array(this.moduleCount);
  57. for (var row = 0; row < this.moduleCount; row++) {
  58. this.modules[row] = new Array(this.moduleCount);
  59. for (var col = 0; col < this.moduleCount; col++) {
  60. this.modules[row][col] = null;//(col + row) % 3;
  61. }
  62. }
  63. this.setupPositionProbePattern(0, 0);
  64. this.setupPositionProbePattern(this.moduleCount - 7, 0);
  65. this.setupPositionProbePattern(0, this.moduleCount - 7);
  66. this.setupPositionAdjustPattern();
  67. this.setupTimingPattern();
  68. this.setupTypeInfo(test, maskPattern);
  69. if (this.typeNumber >= 7) {
  70. this.setupTypeNumber(test);
  71. }
  72. if (this.dataCache == null) {
  73. this.dataCache = QRCode.createData(this.typeNumber, this.errorCorrectLevel, this.dataList);
  74. }
  75. this.mapData(this.dataCache, maskPattern);
  76. };
  77. proto.setupPositionProbePattern = function(row, col) {
  78. for (var r = -1; r <= 7; r++) {
  79. if (row + r <= -1 || this.moduleCount <= row + r) continue;
  80. for (var c = -1; c <= 7; c++) {
  81. if (col + c <= -1 || this.moduleCount <= col + c) continue;
  82. if ( (0 <= r && r <= 6 && (c == 0 || c == 6) )
  83. || (0 <= c && c <= 6 && (r == 0 || r == 6) )
  84. || (2 <= r && r <= 4 && 2 <= c && c <= 4) ) {
  85. this.modules[row + r][col + c] = true;
  86. } else {
  87. this.modules[row + r][col + c] = false;
  88. }
  89. }
  90. }
  91. };
  92. proto.getBestMaskPattern = function() {
  93. var minLostPoint = 0;
  94. var pattern = 0;
  95. for (var i = 0; i < 8; i++) {
  96. this.makeImpl(true, i);
  97. var lostPoint = util.getLostPoint(this);
  98. if (i == 0 || minLostPoint > lostPoint) {
  99. minLostPoint = lostPoint;
  100. pattern = i;
  101. }
  102. }
  103. return pattern;
  104. };
  105. proto.createMovieClip = function(target_mc, instance_name, depth) {
  106. var qr_mc = target_mc.createEmptyMovieClip(instance_name, depth);
  107. var cs = 1;
  108. this.make();
  109. for (var row = 0; row < this.modules.length; row++) {
  110. var y = row * cs;
  111. for (var col = 0; col < this.modules[row].length; col++) {
  112. var x = col * cs;
  113. var dark = this.modules[row][col];
  114. if (dark) {
  115. qr_mc.beginFill(0, 100);
  116. qr_mc.moveTo(x, y);
  117. qr_mc.lineTo(x + cs, y);
  118. qr_mc.lineTo(x + cs, y + cs);
  119. qr_mc.lineTo(x, y + cs);
  120. qr_mc.endFill();
  121. }
  122. }
  123. }
  124. return qr_mc;
  125. };
  126. proto.setupTimingPattern = function() {
  127. for (var r = 8; r < this.moduleCount - 8; r++) {
  128. if (this.modules[r][6] != null) {
  129. continue;
  130. }
  131. this.modules[r][6] = (r % 2 == 0);
  132. }
  133. for (var c = 8; c < this.moduleCount - 8; c++) {
  134. if (this.modules[6][c] != null) {
  135. continue;
  136. }
  137. this.modules[6][c] = (c % 2 == 0);
  138. }
  139. };
  140. proto.setupPositionAdjustPattern = function() {
  141. var pos = util.getPatternPosition(this.typeNumber);
  142. for (var i = 0; i < pos.length; i++) {
  143. for (var j = 0; j < pos.length; j++) {
  144. var row = pos[i];
  145. var col = pos[j];
  146. if (this.modules[row][col] != null) {
  147. continue;
  148. }
  149. for (var r = -2; r <= 2; r++) {
  150. for (var c = -2; c <= 2; c++) {
  151. if (r == -2 || r == 2 || c == -2 || c == 2
  152. || (r == 0 && c == 0) ) {
  153. this.modules[row + r][col + c] = true;
  154. } else {
  155. this.modules[row + r][col + c] = false;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. };
  162. proto.setupTypeNumber = function(test) {
  163. var bits = util.getBCHTypeNumber(this.typeNumber);
  164. for (var i = 0; i < 18; i++) {
  165. var mod = (!test && ( (bits >> i) & 1) == 1);
  166. this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod;
  167. }
  168. for (var i = 0; i < 18; i++) {
  169. var mod = (!test && ( (bits >> i) & 1) == 1);
  170. this.modules[i % 3 + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod;
  171. }
  172. };
  173. proto.setupTypeInfo = function(test, maskPattern) {
  174. var data = (this.errorCorrectLevel << 3) | maskPattern;
  175. var bits = util.getBCHTypeInfo(data);
  176. // vertical
  177. for (var i = 0; i < 15; i++) {
  178. var mod = (!test && ( (bits >> i) & 1) == 1);
  179. if (i < 6) {
  180. this.modules[i][8] = mod;
  181. } else if (i < 8) {
  182. this.modules[i + 1][8] = mod;
  183. } else {
  184. this.modules[this.moduleCount - 15 + i][8] = mod;
  185. }
  186. }
  187. // horizontal
  188. for (var i = 0; i < 15; i++) {
  189. var mod = (!test && ( (bits >> i) & 1) == 1);
  190. if (i < 8) {
  191. this.modules[8][this.moduleCount - i - 1] = mod;
  192. } else if (i < 9) {
  193. this.modules[8][15 - i - 1 + 1] = mod;
  194. } else {
  195. this.modules[8][15 - i - 1] = mod;
  196. }
  197. }
  198. // fixed module
  199. this.modules[this.moduleCount - 8][8] = (!test);
  200. };
  201. proto.mapData = function(data, maskPattern) {
  202. var inc = -1;
  203. var row = this.moduleCount - 1;
  204. var bitIndex = 7;
  205. var byteIndex = 0;
  206. for (var col = this.moduleCount - 1; col > 0; col -= 2) {
  207. if (col == 6) col--;
  208. while (true) {
  209. for (var c = 0; c < 2; c++) {
  210. if (this.modules[row][col - c] == null) {
  211. var dark = false;
  212. if (byteIndex < data.length) {
  213. dark = ( ( (data[byteIndex] >>> bitIndex) & 1) == 1);
  214. }
  215. var mask = util.getMask(maskPattern, row, col - c);
  216. if (mask) {
  217. dark = !dark;
  218. }
  219. this.modules[row][col - c] = dark;
  220. bitIndex--;
  221. if (bitIndex == -1) {
  222. byteIndex++;
  223. bitIndex = 7;
  224. }
  225. }
  226. }
  227. row += inc;
  228. if (row < 0 || this.moduleCount <= row) {
  229. row -= inc;
  230. inc = -inc;
  231. break;
  232. }
  233. }
  234. }
  235. };
  236. QRCode.PAD0 = 0xEC;
  237. QRCode.PAD1 = 0x11;
  238. QRCode.createData = function(typeNumber, errorCorrectLevel, dataList) {
  239. var rsBlocks = RSBlock.getRSBlocks(typeNumber, errorCorrectLevel);
  240. var buffer = new BitBuffer();
  241. for (var i = 0; i < dataList.length; i++) {
  242. var data = dataList[i];
  243. buffer.put(data.mode, 4);
  244. buffer.put(data.getLength(), util.getLengthInBits(data.mode, typeNumber) );
  245. data.write(buffer);
  246. }
  247. // calc num max data.
  248. var totalDataCount = 0;
  249. for (var i = 0; i < rsBlocks.length; i++) {
  250. totalDataCount += rsBlocks[i].dataCount;
  251. }
  252. if (buffer.getLengthInBits() > totalDataCount * 8) {
  253. throw new Error("code length overflow. ("
  254. + buffer.getLengthInBits()
  255. + ">"
  256. + totalDataCount * 8
  257. + ")");
  258. }
  259. // end code
  260. if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
  261. buffer.put(0, 4);
  262. }
  263. // padding
  264. while (buffer.getLengthInBits() % 8 != 0) {
  265. buffer.putBit(false);
  266. }
  267. // padding
  268. while (true) {
  269. if (buffer.getLengthInBits() >= totalDataCount * 8) {
  270. break;
  271. }
  272. buffer.put(QRCode.PAD0, 8);
  273. if (buffer.getLengthInBits() >= totalDataCount * 8) {
  274. break;
  275. }
  276. buffer.put(QRCode.PAD1, 8);
  277. }
  278. return QRCode.createBytes(buffer, rsBlocks);
  279. };
  280. QRCode.createBytes = function(buffer, rsBlocks) {
  281. var offset = 0;
  282. var maxDcCount = 0;
  283. var maxEcCount = 0;
  284. var dcdata = new Array(rsBlocks.length);
  285. var ecdata = new Array(rsBlocks.length);
  286. for (var r = 0; r < rsBlocks.length; r++) {
  287. var dcCount = rsBlocks[r].dataCount;
  288. var ecCount = rsBlocks[r].totalCount - dcCount;
  289. maxDcCount = Math.max(maxDcCount, dcCount);
  290. maxEcCount = Math.max(maxEcCount, ecCount);
  291. dcdata[r] = new Array(dcCount);
  292. for (var i = 0; i < dcdata[r].length; i++) {
  293. dcdata[r][i] = 0xff & buffer.buffer[i + offset];
  294. }
  295. offset += dcCount;
  296. var rsPoly = util.getErrorCorrectPolynomial(ecCount);
  297. var rawPoly = new Polynomial(dcdata[r], rsPoly.getLength() - 1);
  298. var modPoly = rawPoly.mod(rsPoly);
  299. ecdata[r] = new Array(rsPoly.getLength() - 1);
  300. for (var i = 0; i < ecdata[r].length; i++) {
  301. var modIndex = i + modPoly.getLength() - ecdata[r].length;
  302. ecdata[r][i] = (modIndex >= 0)? modPoly.get(modIndex) : 0;
  303. }
  304. }
  305. var totalCodeCount = 0;
  306. for (var i = 0; i < rsBlocks.length; i++) {
  307. totalCodeCount += rsBlocks[i].totalCount;
  308. }
  309. var data = new Array(totalCodeCount);
  310. var index = 0;
  311. for (var i = 0; i < maxDcCount; i++) {
  312. for (var r = 0; r < rsBlocks.length; r++) {
  313. if (i < dcdata[r].length) {
  314. data[index++] = dcdata[r][i];
  315. }
  316. }
  317. }
  318. for (var i = 0; i < maxEcCount; i++) {
  319. for (var r = 0; r < rsBlocks.length; r++) {
  320. if (i < ecdata[r].length) {
  321. data[index++] = ecdata[r][i];
  322. }
  323. }
  324. }
  325. return data;
  326. };
  327. module.exports = QRCode;