QRCodeModel.js 8.4 KB

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