qrcode-matrix 0.2.1 → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -36
- package/dist/qrcodeMatrix.umd.js +1298 -1297
- package/dist/qrcodeMatrix.umd.min.js +6 -0
- package/lib/encode/sjis.d.ts +2 -2
- package/lib/encode/sjisData.d.ts +2 -2
- package/lib/encode/utf-8.d.ts +2 -2
- package/lib/index.d.ts +5 -5
- package/lib/lib/BitBuffer.d.ts +9 -9
- package/lib/lib/QRMatrix.d.ts +10 -10
- package/lib/lib/bch-encode.d.ts +31 -31
- package/lib/lib/draw.d.ts +4 -4
- package/lib/modes/Alphanumeric.d.ts +3 -3
- package/lib/modes/Byte.d.ts +3 -3
- package/lib/modes/Kanji.d.ts +3 -3
- package/lib/modes/Numeric.d.ts +3 -3
- package/lib/modes/mode.d.ts +6 -6
- package/lib/qrcode.d.ts +19 -19
- package/{dist → lib}/qrcodeMatrix.es.js +1296 -1295
- package/{dist → lib}/qrcodeMatrix.js +1301 -1294
- package/lib/utils.d.ts +26 -26
- package/package.json +7 -8
- package/LICENSE +0 -21
- package/lib/encode/sjis.js +0 -113
- package/lib/encode/sjisData.js +0 -4
- package/lib/encode/utf-8.js +0 -23
- package/lib/index.js +0 -37
- package/lib/lib/BitBuffer.js +0 -39
- package/lib/lib/QRMatrix.js +0 -31
- package/lib/lib/bch-encode.js +0 -466
- package/lib/lib/draw.js +0 -49
- package/lib/modes/Alphanumeric.js +0 -49
- package/lib/modes/Byte.js +0 -25
- package/lib/modes/Kanji.js +0 -46
- package/lib/modes/Numeric.js +0 -43
- package/lib/modes/mode.js +0 -2
- package/lib/qrcode.js +0 -124
- package/lib/utils.js +0 -379
- package/src/.DS_Store +0 -0
- package/src/encode/.DS_Store +0 -0
- package/src/encode/sjis.ts +0 -121
- package/src/encode/sjisData.ts +0 -2
- package/src/encode/utf-8.ts +0 -30
- package/src/index.ts +0 -33
- package/src/lib/BitBuffer.ts +0 -70
- package/src/lib/QRMatrix.ts +0 -39
- package/src/lib/bch-encode.ts +0 -639
- package/src/lib/draw.ts +0 -77
- package/src/modes/Alphanumeric.ts +0 -55
- package/src/modes/Byte.ts +0 -30
- package/src/modes/Kanji.ts +0 -56
- package/src/modes/Numeric.ts +0 -47
- package/src/modes/mode.ts +0 -6
- package/src/qrcode.ts +0 -237
- package/src/utils.ts +0 -459
package/src/lib/bch-encode.ts
DELETED
|
@@ -1,639 +0,0 @@
|
|
|
1
|
-
import BitBuffer from './BitBuffer';
|
|
2
|
-
|
|
3
|
-
const QRErrorCorrectionLevel = {
|
|
4
|
-
L: 1,
|
|
5
|
-
M: 0,
|
|
6
|
-
Q: 3,
|
|
7
|
-
H: 2
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const QRMode = {
|
|
11
|
-
MODE_NUMBER: 1 << 0,
|
|
12
|
-
MODE_ALPHA_NUM: 1 << 1,
|
|
13
|
-
MODE_8BIT_BYTE: 1 << 2,
|
|
14
|
-
MODE_KANJI: 1 << 3
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const G15: number = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
|
|
18
|
-
const G18: number = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);
|
|
19
|
-
const G15_MASK: number = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
|
|
20
|
-
const G15_BCH_DIGIT = getBCHDigit(G15);
|
|
21
|
-
const G18_BCH_DIGIT = getBCHDigit(G18);
|
|
22
|
-
|
|
23
|
-
const RS_BLOCK_TABLE: Uint8Array[] = [
|
|
24
|
-
// L
|
|
25
|
-
// M
|
|
26
|
-
// Q
|
|
27
|
-
// H
|
|
28
|
-
|
|
29
|
-
// 1
|
|
30
|
-
[1, 26, 19],
|
|
31
|
-
[1, 26, 16],
|
|
32
|
-
[1, 26, 13],
|
|
33
|
-
[1, 26, 9],
|
|
34
|
-
|
|
35
|
-
// 2
|
|
36
|
-
[1, 44, 34],
|
|
37
|
-
[1, 44, 28],
|
|
38
|
-
[1, 44, 22],
|
|
39
|
-
[1, 44, 16],
|
|
40
|
-
|
|
41
|
-
// 3
|
|
42
|
-
[1, 70, 55],
|
|
43
|
-
[1, 70, 44],
|
|
44
|
-
[2, 35, 17],
|
|
45
|
-
[2, 35, 13],
|
|
46
|
-
|
|
47
|
-
// 4
|
|
48
|
-
[1, 100, 80],
|
|
49
|
-
[2, 50, 32],
|
|
50
|
-
[2, 50, 24],
|
|
51
|
-
[4, 25, 9],
|
|
52
|
-
|
|
53
|
-
// 5
|
|
54
|
-
[1, 134, 108],
|
|
55
|
-
[2, 67, 43],
|
|
56
|
-
[2, 33, 15, 2, 34, 16],
|
|
57
|
-
[2, 33, 11, 2, 34, 12],
|
|
58
|
-
|
|
59
|
-
// 6
|
|
60
|
-
[2, 86, 68],
|
|
61
|
-
[4, 43, 27],
|
|
62
|
-
[4, 43, 19],
|
|
63
|
-
[4, 43, 15],
|
|
64
|
-
|
|
65
|
-
// 7
|
|
66
|
-
[2, 98, 78],
|
|
67
|
-
[4, 49, 31],
|
|
68
|
-
[2, 32, 14, 4, 33, 15],
|
|
69
|
-
[4, 39, 13, 1, 40, 14],
|
|
70
|
-
|
|
71
|
-
// 8
|
|
72
|
-
[2, 121, 97],
|
|
73
|
-
[2, 60, 38, 2, 61, 39],
|
|
74
|
-
[4, 40, 18, 2, 41, 19],
|
|
75
|
-
[4, 40, 14, 2, 41, 15],
|
|
76
|
-
|
|
77
|
-
// 9
|
|
78
|
-
[2, 146, 116],
|
|
79
|
-
[3, 58, 36, 2, 59, 37],
|
|
80
|
-
[4, 36, 16, 4, 37, 17],
|
|
81
|
-
[4, 36, 12, 4, 37, 13],
|
|
82
|
-
|
|
83
|
-
// 10
|
|
84
|
-
[2, 86, 68, 2, 87, 69],
|
|
85
|
-
[4, 69, 43, 1, 70, 44],
|
|
86
|
-
[6, 43, 19, 2, 44, 20],
|
|
87
|
-
[6, 43, 15, 2, 44, 16],
|
|
88
|
-
|
|
89
|
-
// 11
|
|
90
|
-
[4, 101, 81],
|
|
91
|
-
[1, 80, 50, 4, 81, 51],
|
|
92
|
-
[4, 50, 22, 4, 51, 23],
|
|
93
|
-
[3, 36, 12, 8, 37, 13],
|
|
94
|
-
|
|
95
|
-
// 12
|
|
96
|
-
[2, 116, 92, 2, 117, 93],
|
|
97
|
-
[6, 58, 36, 2, 59, 37],
|
|
98
|
-
[4, 46, 20, 6, 47, 21],
|
|
99
|
-
[7, 42, 14, 4, 43, 15],
|
|
100
|
-
|
|
101
|
-
// 13
|
|
102
|
-
[4, 133, 107],
|
|
103
|
-
[8, 59, 37, 1, 60, 38],
|
|
104
|
-
[8, 44, 20, 4, 45, 21],
|
|
105
|
-
[12, 33, 11, 4, 34, 12],
|
|
106
|
-
|
|
107
|
-
// 14
|
|
108
|
-
[3, 145, 115, 1, 146, 116],
|
|
109
|
-
[4, 64, 40, 5, 65, 41],
|
|
110
|
-
[11, 36, 16, 5, 37, 17],
|
|
111
|
-
[11, 36, 12, 5, 37, 13],
|
|
112
|
-
|
|
113
|
-
// 15
|
|
114
|
-
[5, 109, 87, 1, 110, 88],
|
|
115
|
-
[5, 65, 41, 5, 66, 42],
|
|
116
|
-
[5, 54, 24, 7, 55, 25],
|
|
117
|
-
[11, 36, 12, 7, 37, 13],
|
|
118
|
-
|
|
119
|
-
// 16
|
|
120
|
-
[5, 122, 98, 1, 123, 99],
|
|
121
|
-
[7, 73, 45, 3, 74, 46],
|
|
122
|
-
[15, 43, 19, 2, 44, 20],
|
|
123
|
-
[3, 45, 15, 13, 46, 16],
|
|
124
|
-
|
|
125
|
-
// 17
|
|
126
|
-
[1, 135, 107, 5, 136, 108],
|
|
127
|
-
[10, 74, 46, 1, 75, 47],
|
|
128
|
-
[1, 50, 22, 15, 51, 23],
|
|
129
|
-
[2, 42, 14, 17, 43, 15],
|
|
130
|
-
|
|
131
|
-
// 18
|
|
132
|
-
[5, 150, 120, 1, 151, 121],
|
|
133
|
-
[9, 69, 43, 4, 70, 44],
|
|
134
|
-
[17, 50, 22, 1, 51, 23],
|
|
135
|
-
[2, 42, 14, 19, 43, 15],
|
|
136
|
-
|
|
137
|
-
// 19
|
|
138
|
-
[3, 141, 113, 4, 142, 114],
|
|
139
|
-
[3, 70, 44, 11, 71, 45],
|
|
140
|
-
[17, 47, 21, 4, 48, 22],
|
|
141
|
-
[9, 39, 13, 16, 40, 14],
|
|
142
|
-
|
|
143
|
-
// 20
|
|
144
|
-
[3, 135, 107, 5, 136, 108],
|
|
145
|
-
[3, 67, 41, 13, 68, 42],
|
|
146
|
-
[15, 54, 24, 5, 55, 25],
|
|
147
|
-
[15, 43, 15, 10, 44, 16],
|
|
148
|
-
|
|
149
|
-
// 21
|
|
150
|
-
[4, 144, 116, 4, 145, 117],
|
|
151
|
-
[17, 68, 42],
|
|
152
|
-
[17, 50, 22, 6, 51, 23],
|
|
153
|
-
[19, 46, 16, 6, 47, 17],
|
|
154
|
-
|
|
155
|
-
// 22
|
|
156
|
-
[2, 139, 111, 7, 140, 112],
|
|
157
|
-
[17, 74, 46],
|
|
158
|
-
[7, 54, 24, 16, 55, 25],
|
|
159
|
-
[34, 37, 13],
|
|
160
|
-
|
|
161
|
-
// 23
|
|
162
|
-
[4, 151, 121, 5, 152, 122],
|
|
163
|
-
[4, 75, 47, 14, 76, 48],
|
|
164
|
-
[11, 54, 24, 14, 55, 25],
|
|
165
|
-
[16, 45, 15, 14, 46, 16],
|
|
166
|
-
|
|
167
|
-
// 24
|
|
168
|
-
[6, 147, 117, 4, 148, 118],
|
|
169
|
-
[6, 73, 45, 14, 74, 46],
|
|
170
|
-
[11, 54, 24, 16, 55, 25],
|
|
171
|
-
[30, 46, 16, 2, 47, 17],
|
|
172
|
-
|
|
173
|
-
// 25
|
|
174
|
-
[8, 132, 106, 4, 133, 107],
|
|
175
|
-
[8, 75, 47, 13, 76, 48],
|
|
176
|
-
[7, 54, 24, 22, 55, 25],
|
|
177
|
-
[22, 45, 15, 13, 46, 16],
|
|
178
|
-
|
|
179
|
-
// 26
|
|
180
|
-
[10, 142, 114, 2, 143, 115],
|
|
181
|
-
[19, 74, 46, 4, 75, 47],
|
|
182
|
-
[28, 50, 22, 6, 51, 23],
|
|
183
|
-
[33, 46, 16, 4, 47, 17],
|
|
184
|
-
|
|
185
|
-
// 27
|
|
186
|
-
[8, 152, 122, 4, 153, 123],
|
|
187
|
-
[22, 73, 45, 3, 74, 46],
|
|
188
|
-
[8, 53, 23, 26, 54, 24],
|
|
189
|
-
[12, 45, 15, 28, 46, 16],
|
|
190
|
-
|
|
191
|
-
// 28
|
|
192
|
-
[3, 147, 117, 10, 148, 118],
|
|
193
|
-
[3, 73, 45, 23, 74, 46],
|
|
194
|
-
[4, 54, 24, 31, 55, 25],
|
|
195
|
-
[11, 45, 15, 31, 46, 16],
|
|
196
|
-
|
|
197
|
-
// 29
|
|
198
|
-
[7, 146, 116, 7, 147, 117],
|
|
199
|
-
[21, 73, 45, 7, 74, 46],
|
|
200
|
-
[1, 53, 23, 37, 54, 24],
|
|
201
|
-
[19, 45, 15, 26, 46, 16],
|
|
202
|
-
|
|
203
|
-
// 30
|
|
204
|
-
[5, 145, 115, 10, 146, 116],
|
|
205
|
-
[19, 75, 47, 10, 76, 48],
|
|
206
|
-
[15, 54, 24, 25, 55, 25],
|
|
207
|
-
[23, 45, 15, 25, 46, 16],
|
|
208
|
-
|
|
209
|
-
// 31
|
|
210
|
-
[13, 145, 115, 3, 146, 116],
|
|
211
|
-
[2, 74, 46, 29, 75, 47],
|
|
212
|
-
[42, 54, 24, 1, 55, 25],
|
|
213
|
-
[23, 45, 15, 28, 46, 16],
|
|
214
|
-
|
|
215
|
-
// 32
|
|
216
|
-
[17, 145, 115],
|
|
217
|
-
[10, 74, 46, 23, 75, 47],
|
|
218
|
-
[10, 54, 24, 35, 55, 25],
|
|
219
|
-
[19, 45, 15, 35, 46, 16],
|
|
220
|
-
|
|
221
|
-
// 33
|
|
222
|
-
[17, 145, 115, 1, 146, 116],
|
|
223
|
-
[14, 74, 46, 21, 75, 47],
|
|
224
|
-
[29, 54, 24, 19, 55, 25],
|
|
225
|
-
[11, 45, 15, 46, 46, 16],
|
|
226
|
-
|
|
227
|
-
// 34
|
|
228
|
-
[13, 145, 115, 6, 146, 116],
|
|
229
|
-
[14, 74, 46, 23, 75, 47],
|
|
230
|
-
[44, 54, 24, 7, 55, 25],
|
|
231
|
-
[59, 46, 16, 1, 47, 17],
|
|
232
|
-
|
|
233
|
-
// 35
|
|
234
|
-
[12, 151, 121, 7, 152, 122],
|
|
235
|
-
[12, 75, 47, 26, 76, 48],
|
|
236
|
-
[39, 54, 24, 14, 55, 25],
|
|
237
|
-
[22, 45, 15, 41, 46, 16],
|
|
238
|
-
|
|
239
|
-
// 36
|
|
240
|
-
[6, 151, 121, 14, 152, 122],
|
|
241
|
-
[6, 75, 47, 34, 76, 48],
|
|
242
|
-
[46, 54, 24, 10, 55, 25],
|
|
243
|
-
[2, 45, 15, 64, 46, 16],
|
|
244
|
-
|
|
245
|
-
// 37
|
|
246
|
-
[17, 152, 122, 4, 153, 123],
|
|
247
|
-
[29, 74, 46, 14, 75, 47],
|
|
248
|
-
[49, 54, 24, 10, 55, 25],
|
|
249
|
-
[24, 45, 15, 46, 46, 16],
|
|
250
|
-
|
|
251
|
-
// 38
|
|
252
|
-
[4, 152, 122, 18, 153, 123],
|
|
253
|
-
[13, 74, 46, 32, 75, 47],
|
|
254
|
-
[48, 54, 24, 14, 55, 25],
|
|
255
|
-
[42, 45, 15, 32, 46, 16],
|
|
256
|
-
|
|
257
|
-
// 39
|
|
258
|
-
[20, 147, 117, 4, 148, 118],
|
|
259
|
-
[40, 75, 47, 7, 76, 48],
|
|
260
|
-
[43, 54, 24, 22, 55, 25],
|
|
261
|
-
[10, 45, 15, 67, 46, 16],
|
|
262
|
-
|
|
263
|
-
// 40
|
|
264
|
-
[19, 148, 118, 6, 149, 119],
|
|
265
|
-
[18, 75, 47, 31, 76, 48],
|
|
266
|
-
[34, 54, 24, 34, 55, 25],
|
|
267
|
-
[20, 45, 15, 61, 46, 16]
|
|
268
|
-
].map(arr => new Uint8Array(arr));
|
|
269
|
-
|
|
270
|
-
//内存能省一点是一点
|
|
271
|
-
// if(typeof Uint8Array == 'function'){
|
|
272
|
-
// RS_BLOCK_TABLE = RS_BLOCK_TABLE.map(arr=>new Uint8Array(arr));
|
|
273
|
-
// }
|
|
274
|
-
|
|
275
|
-
interface RSBlock {
|
|
276
|
-
totalCount: number;
|
|
277
|
-
dataCount: number;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
interface mode {
|
|
281
|
-
getMode(): number;
|
|
282
|
-
getLength(): number;
|
|
283
|
-
write(b: BitBuffer): mode;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// 获取数据块长度
|
|
287
|
-
const getLengthInBits: (mode: number | string, typeNumber: number) => number = (function() {
|
|
288
|
-
// 各模式数据块长度
|
|
289
|
-
const BITS_LENGTH_LIST = {
|
|
290
|
-
[QRMode.MODE_NUMBER]: [10, 12, 14],
|
|
291
|
-
[QRMode.MODE_ALPHA_NUM]: [9, 11, 13],
|
|
292
|
-
[QRMode.MODE_8BIT_BYTE]: [8, 16, 16],
|
|
293
|
-
[QRMode.MODE_KANJI]: [8, 10, 12]
|
|
294
|
-
};
|
|
295
|
-
return function(mode: number | string, type: number): number {
|
|
296
|
-
if (1 <= type && type <= 40) {
|
|
297
|
-
var lengths = BITS_LENGTH_LIST[mode];
|
|
298
|
-
if (!lengths) throw 'mode:' + mode;
|
|
299
|
-
var index = type <= 9 ? 0 : type <= 26 ? 1 : 2;
|
|
300
|
-
return lengths[index];
|
|
301
|
-
} else {
|
|
302
|
-
throw new Error('type:' + type);
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
})();
|
|
306
|
-
|
|
307
|
-
// 获取最高位
|
|
308
|
-
function getBCHDigit(data: number): number {
|
|
309
|
-
let digit = 0;
|
|
310
|
-
while (data != 0) {
|
|
311
|
-
digit++;
|
|
312
|
-
data >>>= 1;
|
|
313
|
-
}
|
|
314
|
-
return digit;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
function getBCHTypeInfo(data: number): number {
|
|
318
|
-
let d = data << 10;
|
|
319
|
-
while (getBCHDigit(d) >= G15_BCH_DIGIT) {
|
|
320
|
-
d ^= G15 << (getBCHDigit(d) - G15_BCH_DIGIT);
|
|
321
|
-
}
|
|
322
|
-
return ((data << 10) | d) ^ G15_MASK;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
function getBCHTypeNumber(data: number): number {
|
|
326
|
-
var d = data << 12;
|
|
327
|
-
while (getBCHDigit(d) - G18_BCH_DIGIT >= 0) {
|
|
328
|
-
d ^= G18 << (getBCHDigit(d) - G18_BCH_DIGIT);
|
|
329
|
-
}
|
|
330
|
-
return (data << 12) | d;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
function getRsBlockTable(typeNumber: number, errorCorrectionLevel: number): Uint8Array {
|
|
334
|
-
switch (errorCorrectionLevel) {
|
|
335
|
-
case QRErrorCorrectionLevel.L:
|
|
336
|
-
return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
|
|
337
|
-
case QRErrorCorrectionLevel.M:
|
|
338
|
-
return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
|
|
339
|
-
case QRErrorCorrectionLevel.Q:
|
|
340
|
-
return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
|
|
341
|
-
case QRErrorCorrectionLevel.H:
|
|
342
|
-
return RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
|
|
343
|
-
default:
|
|
344
|
-
throw 'bad rs block @ typeNumber:' + typeNumber + '/errorCorrectionLevel:' + errorCorrectionLevel;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
function getRSBlocks(typeNumber: number, errorCorrectionLevel: number): RSBlock[] {
|
|
349
|
-
let rsBlock: Uint8Array = getRsBlockTable(typeNumber, errorCorrectionLevel);
|
|
350
|
-
let length: number = rsBlock.length / 3;
|
|
351
|
-
let list: RSBlock[] = [];
|
|
352
|
-
for (let i = 0; i < length; i += 1) {
|
|
353
|
-
let count = rsBlock[i * 3 + 0];
|
|
354
|
-
let totalCount = rsBlock[i * 3 + 1];
|
|
355
|
-
let dataCount = rsBlock[i * 3 + 2];
|
|
356
|
-
|
|
357
|
-
for (let j = 0; j < count; j += 1) {
|
|
358
|
-
list.push({ totalCount, dataCount });
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
return list;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
const QRMath = (function() {
|
|
365
|
-
const EXP_TABLE = new Uint8Array(256);
|
|
366
|
-
const LOG_TABLE = new Uint8Array(256);
|
|
367
|
-
|
|
368
|
-
// initialize tables
|
|
369
|
-
for (let i = 0; i < 8; i += 1) {
|
|
370
|
-
EXP_TABLE[i] = 1 << i;
|
|
371
|
-
}
|
|
372
|
-
for (let i = 8; i < 256; i += 1) {
|
|
373
|
-
EXP_TABLE[i] = EXP_TABLE[i - 4] ^ EXP_TABLE[i - 5] ^ EXP_TABLE[i - 6] ^ EXP_TABLE[i - 8];
|
|
374
|
-
}
|
|
375
|
-
for (let i = 0; i < 255; i += 1) {
|
|
376
|
-
LOG_TABLE[EXP_TABLE[i]] = i;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
let _this = {
|
|
380
|
-
glog: function(n) {
|
|
381
|
-
if (n < 1) {
|
|
382
|
-
throw 'glog(' + n + ')';
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
return LOG_TABLE[n];
|
|
386
|
-
},
|
|
387
|
-
gexp: function(n) {
|
|
388
|
-
while (n < 0) {
|
|
389
|
-
n += 255;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
while (n >= 256) {
|
|
393
|
-
n -= 255;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
return EXP_TABLE[n];
|
|
397
|
-
}
|
|
398
|
-
};
|
|
399
|
-
|
|
400
|
-
return _this;
|
|
401
|
-
})();
|
|
402
|
-
|
|
403
|
-
const qrPolynomial = function(num, shift) {
|
|
404
|
-
if (typeof num.length == 'undefined') {
|
|
405
|
-
throw num.length + '/' + shift;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
let _num = (function() {
|
|
409
|
-
let offset = 0;
|
|
410
|
-
while (offset < num.length && num[offset] == 0) {
|
|
411
|
-
offset += 1;
|
|
412
|
-
}
|
|
413
|
-
let _num = new Array(num.length - offset + shift);
|
|
414
|
-
for (let i = 0; i < num.length - offset; i += 1) {
|
|
415
|
-
_num[i] = num[i + offset];
|
|
416
|
-
}
|
|
417
|
-
return _num;
|
|
418
|
-
})();
|
|
419
|
-
|
|
420
|
-
let _this = {
|
|
421
|
-
getAt: function(index) {
|
|
422
|
-
return _num[index];
|
|
423
|
-
},
|
|
424
|
-
getLength: function() {
|
|
425
|
-
return _num.length;
|
|
426
|
-
},
|
|
427
|
-
multiply: function(e) {
|
|
428
|
-
let num = new Array(_this.getLength() + e.getLength() - 1);
|
|
429
|
-
for (let i = 0; i < _this.getLength(); i += 1) {
|
|
430
|
-
for (let j = 0; j < e.getLength(); j += 1) {
|
|
431
|
-
num[i + j] ^= QRMath.gexp(QRMath.glog(_this.getAt(i)) + QRMath.glog(e.getAt(j)));
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
return qrPolynomial(num, 0);
|
|
435
|
-
},
|
|
436
|
-
mod: function(e) {
|
|
437
|
-
if (_this.getLength() - e.getLength() < 0) {
|
|
438
|
-
return _this;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
let ratio = QRMath.glog(_this.getAt(0)) - QRMath.glog(e.getAt(0));
|
|
442
|
-
|
|
443
|
-
let num = new Array(_this.getLength());
|
|
444
|
-
for (let i = 0; i < _this.getLength(); i += 1) {
|
|
445
|
-
num[i] = _this.getAt(i);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
for (let i = 0; i < e.getLength(); i += 1) {
|
|
449
|
-
num[i] ^= QRMath.gexp(QRMath.glog(e.getAt(i)) + ratio);
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
// recursive call
|
|
453
|
-
return qrPolynomial(num, 0).mod(e);
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
return _this;
|
|
457
|
-
};
|
|
458
|
-
|
|
459
|
-
//纠错相关
|
|
460
|
-
const getErrorCorrectPolynomial = function(errorCorrectLength) {
|
|
461
|
-
let a = qrPolynomial([1], 0);
|
|
462
|
-
for (let i = 0; i < errorCorrectLength; i += 1) {
|
|
463
|
-
a = a.multiply(qrPolynomial([1, QRMath.gexp(i)], 0));
|
|
464
|
-
}
|
|
465
|
-
return a;
|
|
466
|
-
};
|
|
467
|
-
|
|
468
|
-
//RS编码
|
|
469
|
-
const createBytes = function(buffer: BitBuffer, rsBlocks: RSBlock[]) {
|
|
470
|
-
let offset = 0;
|
|
471
|
-
let maxDcCount = 0;
|
|
472
|
-
let maxEcCount = 0;
|
|
473
|
-
|
|
474
|
-
let dcdata = new Array(rsBlocks.length);
|
|
475
|
-
let ecdata = new Array(rsBlocks.length);
|
|
476
|
-
|
|
477
|
-
for (let r = 0; r < rsBlocks.length; r += 1) {
|
|
478
|
-
let dcCount = rsBlocks[r].dataCount;
|
|
479
|
-
let ecCount = rsBlocks[r].totalCount - dcCount;
|
|
480
|
-
|
|
481
|
-
maxDcCount = Math.max(maxDcCount, dcCount);
|
|
482
|
-
maxEcCount = Math.max(maxEcCount, ecCount);
|
|
483
|
-
|
|
484
|
-
dcdata[r] = new Array(dcCount);
|
|
485
|
-
for (let i = 0; i < dcdata[r].length; i += 1) {
|
|
486
|
-
dcdata[r][i] = 0xff & buffer.getBuffer()[i + offset];
|
|
487
|
-
}
|
|
488
|
-
offset += dcCount;
|
|
489
|
-
|
|
490
|
-
let rsPoly = getErrorCorrectPolynomial(ecCount);
|
|
491
|
-
let rawPoly = qrPolynomial(dcdata[r], rsPoly.getLength() - 1);
|
|
492
|
-
|
|
493
|
-
let modPoly = rawPoly.mod(rsPoly);
|
|
494
|
-
ecdata[r] = new Array(rsPoly.getLength() - 1);
|
|
495
|
-
for (let i = 0; i < ecdata[r].length; i += 1) {
|
|
496
|
-
let modIndex = i + modPoly.getLength() - ecdata[r].length;
|
|
497
|
-
ecdata[r][i] = modIndex >= 0 ? modPoly.getAt(modIndex) : 0;
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
let totalCodeCount = 0;
|
|
502
|
-
for (let i = 0; i < rsBlocks.length; i += 1) {
|
|
503
|
-
totalCodeCount += rsBlocks[i].totalCount;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
let data = new Array(totalCodeCount);
|
|
507
|
-
let index = 0;
|
|
508
|
-
|
|
509
|
-
for (let i = 0; i < maxDcCount; i += 1) {
|
|
510
|
-
for (let r = 0; r < rsBlocks.length; r += 1) {
|
|
511
|
-
if (i < dcdata[r].length) {
|
|
512
|
-
data[index] = dcdata[r][i];
|
|
513
|
-
index += 1;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
for (let i = 0; i < maxEcCount; i += 1) {
|
|
519
|
-
for (let r = 0; r < rsBlocks.length; r += 1) {
|
|
520
|
-
if (i < ecdata[r].length) {
|
|
521
|
-
data[index] = ecdata[r][i];
|
|
522
|
-
index += 1;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
return data;
|
|
528
|
-
};
|
|
529
|
-
|
|
530
|
-
function getTotal(rsBlocks):number{
|
|
531
|
-
let totalDataCount = 0;
|
|
532
|
-
for (var i = 0; i < rsBlocks.length; i += 1) {
|
|
533
|
-
totalDataCount += rsBlocks[i].dataCount;
|
|
534
|
-
}
|
|
535
|
-
return totalDataCount;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
const getMaxDataCount = function(typeNumber:number, errorCorrectionLevel:number):number{
|
|
539
|
-
let rsBlocks = getRSBlocks(typeNumber, errorCorrectionLevel);
|
|
540
|
-
return getTotal(rsBlocks);
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
// 通过buffer 创建 QR 数据块
|
|
544
|
-
const paddingBuffer = function(buffer: BitBuffer, rsBlocks: RSBlock[]) {
|
|
545
|
-
const PAD0 = 0xec;
|
|
546
|
-
const PAD1 = 0x11;
|
|
547
|
-
|
|
548
|
-
// calc num max data.
|
|
549
|
-
let totalDataCount = getTotal(rsBlocks);
|
|
550
|
-
|
|
551
|
-
if (buffer.getLengthInBits() > totalDataCount * 8) {
|
|
552
|
-
throw 'code length overflow. (' + buffer.getLengthInBits() + '>' + totalDataCount * 8 + ')';
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
// end code
|
|
556
|
-
if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
|
|
557
|
-
buffer.put(0, 4);
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
// padding
|
|
561
|
-
while (buffer.getLengthInBits() % 8 != 0) {
|
|
562
|
-
buffer.putBit(false);
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
// padding
|
|
566
|
-
while (true) {
|
|
567
|
-
if (buffer.getLengthInBits() >= totalDataCount * 8) {
|
|
568
|
-
break;
|
|
569
|
-
}
|
|
570
|
-
buffer.put(PAD0, 8);
|
|
571
|
-
|
|
572
|
-
if (buffer.getLengthInBits() >= totalDataCount * 8) {
|
|
573
|
-
break;
|
|
574
|
-
}
|
|
575
|
-
buffer.put(PAD1, 8);
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
// return createBytes(buffer, rsBlocks);
|
|
579
|
-
};
|
|
580
|
-
|
|
581
|
-
const getBufferForBytes = function(typeNumber, byteArray, mode) {
|
|
582
|
-
let buffer = BitBuffer();
|
|
583
|
-
let data = byteArray;
|
|
584
|
-
buffer.put(mode, 4);
|
|
585
|
-
buffer.put(data.length, getLengthInBits(mode, typeNumber));
|
|
586
|
-
for (let i = 0; i < data.length; i++) {
|
|
587
|
-
buffer.put(data[i], 8);
|
|
588
|
-
}
|
|
589
|
-
return buffer;
|
|
590
|
-
// return _createData(buffer, rsBlocks);
|
|
591
|
-
};
|
|
592
|
-
|
|
593
|
-
const getBufferForModes = function(typeNumber, dataList) {
|
|
594
|
-
let buffer = BitBuffer();
|
|
595
|
-
for (let i = 0; i < dataList.length; i += 1) {
|
|
596
|
-
let data = dataList[i];
|
|
597
|
-
buffer.put(data.getMode(), 4);
|
|
598
|
-
buffer.put(data.getLength(), getLengthInBits(data.getMode(), typeNumber));
|
|
599
|
-
data.write(buffer);
|
|
600
|
-
}
|
|
601
|
-
return buffer;
|
|
602
|
-
// return _createData(buffer, rsBlocks);
|
|
603
|
-
};
|
|
604
|
-
|
|
605
|
-
function createData(typeNumber: number, errorCorrectionLevel: number, byteArray: mode[]);
|
|
606
|
-
function createData(typeNumber: number, errorCorrectionLevel: number, byteArray: number[], mode?: mode);
|
|
607
|
-
function createData(typeNumber: number, errorCorrectionLevel: number, byteArray: any, mode?: mode) {
|
|
608
|
-
let rsBlocks = getRSBlocks(typeNumber, errorCorrectionLevel); //RS 算法矩阵
|
|
609
|
-
//1. 数据通过 mode 编码为 buffer;
|
|
610
|
-
let buffer: BitBuffer;
|
|
611
|
-
if (mode) {
|
|
612
|
-
buffer = getBufferForBytes(typeNumber, byteArray, mode);
|
|
613
|
-
} else {
|
|
614
|
-
buffer = getBufferForModes(typeNumber, byteArray);
|
|
615
|
-
}
|
|
616
|
-
//修整 buffer
|
|
617
|
-
paddingBuffer(buffer, rsBlocks);
|
|
618
|
-
return createBytes(buffer, rsBlocks);
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
export {
|
|
622
|
-
QRErrorCorrectionLevel,
|
|
623
|
-
QRMode,
|
|
624
|
-
getLengthInBits,
|
|
625
|
-
getBCHTypeInfo,
|
|
626
|
-
getBCHTypeNumber,
|
|
627
|
-
getMaxDataCount,
|
|
628
|
-
getRSBlocks,
|
|
629
|
-
RSBlock,
|
|
630
|
-
getBufferForModes,
|
|
631
|
-
createData
|
|
632
|
-
};
|
|
633
|
-
|
|
634
|
-
// module.exports = {
|
|
635
|
-
// QRErrorCorrectionLevel,
|
|
636
|
-
// getBCHTypeInfo,
|
|
637
|
-
// getBCHTypeNumber,
|
|
638
|
-
// getRSBlocks
|
|
639
|
-
// };
|
package/src/lib/draw.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import BitMatrix from 'bitmatrix';
|
|
2
|
-
// import BitMatrix = require('bitmatrix');
|
|
3
|
-
|
|
4
|
-
const drawLoop = function(
|
|
5
|
-
matrix: BitMatrix,
|
|
6
|
-
value: number,
|
|
7
|
-
x: number,
|
|
8
|
-
y: number,
|
|
9
|
-
offsetX: number,
|
|
10
|
-
offsetY: number,
|
|
11
|
-
length: number
|
|
12
|
-
): void {
|
|
13
|
-
let { width, height } = matrix;
|
|
14
|
-
let v = value ? 1 : 0;
|
|
15
|
-
let _x = x;
|
|
16
|
-
let _y = y;
|
|
17
|
-
if (length <= 0) return;
|
|
18
|
-
while (length--) {
|
|
19
|
-
if (_x >= 0 && _x < width && _y >= 0 && _y < height) {
|
|
20
|
-
matrix.set(_x, _y, v);
|
|
21
|
-
}
|
|
22
|
-
_x += offsetX;
|
|
23
|
-
_y += offsetY;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
//bit矩阵中 绘一个矩形
|
|
28
|
-
const rectBorder = function(
|
|
29
|
-
matrix: BitMatrix,
|
|
30
|
-
val: number,
|
|
31
|
-
row: number,
|
|
32
|
-
col: number,
|
|
33
|
-
width: number,
|
|
34
|
-
height: number
|
|
35
|
-
) {
|
|
36
|
-
if (width < 0 || height < 0) return;
|
|
37
|
-
let set = drawLoop.bind(null, matrix, val ? 1 : 0);
|
|
38
|
-
if (width === 1) {
|
|
39
|
-
return set(row, col, 0, 1, height);
|
|
40
|
-
}
|
|
41
|
-
if (height === 1) {
|
|
42
|
-
return set(row, col, 1, 0, width);
|
|
43
|
-
}
|
|
44
|
-
let _w = width - 1;
|
|
45
|
-
let _h = height - 1;
|
|
46
|
-
set(row, col, 1, 0, _w);
|
|
47
|
-
set((row += width - 1), col, 0, 1, _h);
|
|
48
|
-
set(row, (col += height - 1), -1, 0, _w);
|
|
49
|
-
set((row -= width - 1), col, 0, -1, _h);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
//bit矩阵中 填充一个矩形
|
|
53
|
-
const fillRect = function(
|
|
54
|
-
matrix: BitMatrix,
|
|
55
|
-
val: number,
|
|
56
|
-
row: number,
|
|
57
|
-
col: number,
|
|
58
|
-
width: number,
|
|
59
|
-
height: number
|
|
60
|
-
) {
|
|
61
|
-
let { width: _width, height: _height } = matrix;
|
|
62
|
-
let v = val ? 1 : 0;
|
|
63
|
-
let _x = row > 0 ? row : 0;
|
|
64
|
-
let _y = col > 0 ? col : 0;
|
|
65
|
-
let maxX = Math.min(row + width, _width);
|
|
66
|
-
let maxY = Math.min(col + height, _height);
|
|
67
|
-
for (let y = _y; y < maxY; y++) {
|
|
68
|
-
for (let x = _x; x < maxX; x++) {
|
|
69
|
-
matrix.set(x, y, v);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export {
|
|
75
|
-
rectBorder,
|
|
76
|
-
fillRect
|
|
77
|
-
}
|