-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqr-util.ts
301 lines (263 loc) · 7.07 KB
/
qr-util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { QRPolynomial } from './qr-polynomial'
import { QRMath } from './qr-math'
import { qrModeEnum } from './qr-mode-enum'
const QRMaskPattern = {
PATTERN000: 0,
PATTERN001: 1,
PATTERN010: 2,
PATTERN011: 3,
PATTERN100: 4,
PATTERN101: 5,
PATTERN110: 6,
PATTERN111: 7,
}
const 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],
]
const G15 =
(1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0)
const G18 =
(1 << 12) |
(1 << 11) |
(1 << 10) |
(1 << 9) |
(1 << 8) |
(1 << 5) |
(1 << 2) |
(1 << 0)
const G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1)
const QRUtil = {
getBCHTypeInfo: function (data: number) {
let d = data << 10
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(G15) >= 0) {
d ^= G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(G15))
}
return ((data << 10) | d) ^ G15_MASK
},
getBCHTypeNumber: function (data: number) {
let d = data << 12
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(G18) >= 0) {
d ^= G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(G18))
}
return (data << 12) | d
},
getBCHDigit: function (data: number) {
let digit = 0
while (data != 0) {
digit++
data >>>= 1
}
return digit
},
getPatternPosition: function (typeNumber: number) {
return PATTERN_POSITION_TABLE[typeNumber - 1]
},
getMask: function (maskPattern: number, i: number, j: number) {
switch (maskPattern) {
case QRMaskPattern.PATTERN000:
return (i + j) % 2 == 0
case QRMaskPattern.PATTERN001:
return i % 2 == 0
case QRMaskPattern.PATTERN010:
return j % 3 == 0
case QRMaskPattern.PATTERN011:
return (i + j) % 3 == 0
case QRMaskPattern.PATTERN100:
return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0
case QRMaskPattern.PATTERN101:
return ((i * j) % 2) + ((i * j) % 3) == 0
case QRMaskPattern.PATTERN110:
return (((i * j) % 2) + ((i * j) % 3)) % 2 == 0
case QRMaskPattern.PATTERN111:
return (((i * j) % 3) + ((i + j) % 2)) % 2 == 0
default:
throw new Error('bad maskPattern:' + maskPattern)
}
},
getErrorCorrectPolynomial: function (errorCorrectLength: number) {
let a = new QRPolynomial([1], 0)
for (let i = 0; i < errorCorrectLength; i++) {
a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0))
}
return a
},
getLengthInBits: function (mode: number, type: string | number) {
if (1 <= type && type < 10) {
// 1 - 9
switch (mode) {
case qrModeEnum.MODE_NUMBER:
return 10
case qrModeEnum.MODE_ALPHA_NUM:
return 9
case qrModeEnum.MODE_8BIT_BYTE:
return 8
case qrModeEnum.MODE_KANJI:
return 8
default:
throw new Error('mode:' + mode)
}
} else if (type < 27) {
// 10 - 26
switch (mode) {
case qrModeEnum.MODE_NUMBER:
return 12
case qrModeEnum.MODE_ALPHA_NUM:
return 11
case qrModeEnum.MODE_8BIT_BYTE:
return 16
case qrModeEnum.MODE_KANJI:
return 10
default:
throw new Error('mode:' + mode)
}
} else if (type < 41) {
// 27 - 40
switch (mode) {
case qrModeEnum.MODE_NUMBER:
return 14
case qrModeEnum.MODE_ALPHA_NUM:
return 13
case qrModeEnum.MODE_8BIT_BYTE:
return 16
case qrModeEnum.MODE_KANJI:
return 12
default:
throw new Error('mode:' + mode)
}
} else {
throw new Error('type:' + type)
}
},
getLostPoint: function (qrCode: {
getModuleCount: () => any
isDark: (arg0: number, arg1: number) => any
}) {
let row
let col
const moduleCount = qrCode.getModuleCount()
let lostPoint = 0
// LEVEL1
for (row = 0; row < moduleCount; row++) {
for (col = 0; col < moduleCount; col++) {
let sameCount = 0
const dark = qrCode.isDark(row, col)
for (let r = -1; r <= 1; r++) {
if (row + r < 0 || moduleCount <= row + r) {
continue
}
for (let c = -1; c <= 1; c++) {
if (col + c < 0 || moduleCount <= col + c) {
continue
}
if (r == 0 && c == 0) {
continue
}
if (dark == qrCode.isDark(row + r, col + c)) {
sameCount++
}
}
}
if (sameCount > 5) {
lostPoint += 3 + sameCount - 5
}
}
}
// LEVEL2
for (row = 0; row < moduleCount - 1; row++) {
for (col = 0; col < moduleCount - 1; col++) {
let count = 0
if (qrCode.isDark(row, col)) count++
if (qrCode.isDark(row + 1, col)) count++
if (qrCode.isDark(row, col + 1)) count++
if (qrCode.isDark(row + 1, col + 1)) count++
if (count == 0 || count == 4) {
lostPoint += 3
}
}
}
// LEVEL3
for (row = 0; row < moduleCount; row++) {
for (col = 0; col < moduleCount - 6; col++) {
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)
) {
lostPoint += 40
}
}
}
for (col = 0; col < moduleCount; col++) {
for (row = 0; row < moduleCount - 6; row++) {
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)
) {
lostPoint += 40
}
}
}
// LEVEL4
let darkCount = 0
for (col = 0; col < moduleCount; col++) {
for (row = 0; row < moduleCount; row++) {
if (qrCode.isDark(row, col)) {
darkCount++
}
}
}
const ratio =
Math.abs((100 * darkCount) / moduleCount / moduleCount - 50) / 5
lostPoint += ratio * 10
return lostPoint
},
}
export { QRUtil }