QRUtil.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import QRMaskPattern from "./QRMaskPattern.js";
  2. import QRPolynomial from "./QRPolynomial.js";
  3. import QRMode from "./QRMode.js";
  4. import QRMath from "./QRMath.js";
  5. export default class QRUtil {
  6. static PATTERN_POSITION_TABLE = [
  7. [],
  8. [6, 18],
  9. [6, 22],
  10. [6, 26],
  11. [6, 30],
  12. [6, 34],
  13. [6, 22, 38],
  14. [6, 24, 42],
  15. [6, 26, 46],
  16. [6, 28, 50],
  17. [6, 30, 54],
  18. [6, 32, 58],
  19. [6, 34, 62],
  20. [6, 26, 46, 66],
  21. [6, 26, 48, 70],
  22. [6, 26, 50, 74],
  23. [6, 30, 54, 78],
  24. [6, 30, 56, 82],
  25. [6, 30, 58, 86],
  26. [6, 34, 62, 90],
  27. [6, 28, 50, 72, 94],
  28. [6, 26, 50, 74, 98],
  29. [6, 30, 54, 78, 102],
  30. [6, 28, 54, 80, 106],
  31. [6, 32, 58, 84, 110],
  32. [6, 30, 58, 86, 114],
  33. [6, 34, 62, 90, 118],
  34. [6, 26, 50, 74, 98, 122],
  35. [6, 30, 54, 78, 102, 126],
  36. [6, 26, 52, 78, 104, 130],
  37. [6, 30, 56, 82, 108, 134],
  38. [6, 34, 60, 86, 112, 138],
  39. [6, 30, 58, 86, 114, 142],
  40. [6, 34, 62, 90, 118, 146],
  41. [6, 30, 54, 78, 102, 126, 150],
  42. [6, 24, 50, 76, 102, 128, 154],
  43. [6, 28, 54, 80, 106, 132, 158],
  44. [6, 32, 58, 84, 110, 136, 162],
  45. [6, 26, 54, 82, 110, 138, 166],
  46. [6, 30, 58, 86, 114, 142, 170]
  47. ];
  48. static G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
  49. static G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);
  50. static G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
  51. static getBCHTypeInfo(data) {
  52. var d = data << 10;
  53. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) {
  54. d ^= (QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15)));
  55. }
  56. return ((data << 10) | d) ^ QRUtil.G15_MASK;
  57. }
  58. static getBCHTypeNumber(data) {
  59. var d = data << 12;
  60. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) {
  61. d ^= (QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18)));
  62. }
  63. return (data << 12) | d;
  64. }
  65. static getBCHDigit(data) {
  66. var digit = 0;
  67. while (data != 0) {
  68. digit++;
  69. data >>>= 1;
  70. }
  71. return digit;
  72. }
  73. static getPatternPosition(typeNumber) {
  74. return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1];
  75. }
  76. static getMask(maskPattern, i, j) {
  77. switch (maskPattern) {
  78. case QRMaskPattern.PATTERN000:
  79. return (i + j) % 2 == 0;
  80. case QRMaskPattern.PATTERN001:
  81. return i % 2 == 0;
  82. case QRMaskPattern.PATTERN010:
  83. return j % 3 == 0;
  84. case QRMaskPattern.PATTERN011:
  85. return (i + j) % 3 == 0;
  86. case QRMaskPattern.PATTERN100:
  87. return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0;
  88. case QRMaskPattern.PATTERN101:
  89. return (i * j) % 2 + (i * j) % 3 == 0;
  90. case QRMaskPattern.PATTERN110:
  91. return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
  92. case QRMaskPattern.PATTERN111:
  93. return ((i * j) % 3 + (i + j) % 2) % 2 == 0;
  94. default:
  95. throw new Error("bad maskPattern:" + maskPattern);
  96. }
  97. }
  98. static getErrorCorrectPolynomial(errorCorrectLength) {
  99. var a = new QRPolynomial([1], 0);
  100. for (var i = 0; i < errorCorrectLength; i++) {
  101. a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0));
  102. }
  103. return a;
  104. }
  105. static getLengthInBits(mode, type) {
  106. if (1 <= type && type < 10) {
  107. switch (mode) {
  108. case QRMode.MODE_NUMBER:
  109. return 10;
  110. case QRMode.MODE_ALPHA_NUM:
  111. return 9;
  112. case QRMode.MODE_8BIT_BYTE:
  113. return 8;
  114. case QRMode.MODE_KANJI:
  115. return 8;
  116. default:
  117. throw new Error("mode:" + mode);
  118. }
  119. } else if (type < 27) {
  120. switch (mode) {
  121. case QRMode.MODE_NUMBER:
  122. return 12;
  123. case QRMode.MODE_ALPHA_NUM:
  124. return 11;
  125. case QRMode.MODE_8BIT_BYTE:
  126. return 16;
  127. case QRMode.MODE_KANJI:
  128. return 10;
  129. default:
  130. throw new Error("mode:" + mode);
  131. }
  132. } else if (type < 41) {
  133. switch (mode) {
  134. case QRMode.MODE_NUMBER:
  135. return 14;
  136. case QRMode.MODE_ALPHA_NUM:
  137. return 13;
  138. case QRMode.MODE_8BIT_BYTE:
  139. return 16;
  140. case QRMode.MODE_KANJI:
  141. return 12;
  142. default:
  143. throw new Error("mode:" + mode);
  144. }
  145. } else {
  146. throw new Error("type:" + type);
  147. }
  148. }
  149. static getLostPoint(qrCode) {
  150. var moduleCount = qrCode.getModuleCount();
  151. var lostPoint = 0;
  152. for (var row = 0; row < moduleCount; row++) {
  153. for (var col = 0; col < moduleCount; col++) {
  154. var sameCount = 0;
  155. var dark = qrCode.isDark(row, col);
  156. for (var r = -1; r <= 1; r++) {
  157. if (row + r < 0 || moduleCount <= row + r) {
  158. continue;
  159. }
  160. for (var c = -1; c <= 1; c++) {
  161. if (col + c < 0 || moduleCount <= col + c) {
  162. continue;
  163. }
  164. if (r == 0 && c == 0) {
  165. continue;
  166. }
  167. if (dark == qrCode.isDark(row + r, col + c)) {
  168. sameCount++;
  169. }
  170. }
  171. }
  172. if (sameCount > 5) {
  173. lostPoint += (3 + sameCount - 5);
  174. }
  175. }
  176. }
  177. for (var row = 0; row < moduleCount - 1; row++) {
  178. for (var col = 0; col < moduleCount - 1; col++) {
  179. var count = 0;
  180. if (qrCode.isDark(row, col)) count++;
  181. if (qrCode.isDark(row + 1, col)) count++;
  182. if (qrCode.isDark(row, col + 1)) count++;
  183. if (qrCode.isDark(row + 1, col + 1)) count++;
  184. if (count == 0 || count == 4) {
  185. lostPoint += 3;
  186. }
  187. }
  188. }
  189. for (var row = 0; row < moduleCount; row++) {
  190. for (var col = 0; col < moduleCount - 6; col++) {
  191. if (qrCode.isDark(row, col) && !qrCode.isDark(row, col + 1) && qrCode.isDark(row, col +
  192. 2) && qrCode.isDark(row, col + 3) && qrCode.isDark(row, col + 4) && !qrCode
  193. .isDark(row, col + 5) && qrCode.isDark(row, col + 6)) {
  194. lostPoint += 40;
  195. }
  196. }
  197. }
  198. for (var col = 0; col < moduleCount; col++) {
  199. for (var row = 0; row < moduleCount - 6; row++) {
  200. if (qrCode.isDark(row, col) && !qrCode.isDark(row + 1, col) && qrCode.isDark(row + 2,
  201. col) && qrCode.isDark(row + 3, col) && qrCode.isDark(row + 4, col) && !qrCode
  202. .isDark(row + 5, col) && qrCode.isDark(row + 6, col)) {
  203. lostPoint += 40;
  204. }
  205. }
  206. }
  207. var darkCount = 0;
  208. for (var col = 0; col < moduleCount; col++) {
  209. for (var row = 0; row < moduleCount; row++) {
  210. if (qrCode.isDark(row, col)) {
  211. darkCount++;
  212. }
  213. }
  214. }
  215. var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5;
  216. lostPoint += ratio * 10;
  217. return lostPoint;
  218. }
  219. };