135 lines
3.3 KiB
JavaScript
Executable file
135 lines
3.3 KiB
JavaScript
Executable file
const ECLevel = require('./error-correction-level')
|
|
|
|
const EC_BLOCKS_TABLE = [
|
|
// L M Q H
|
|
1, 1, 1, 1,
|
|
1, 1, 1, 1,
|
|
1, 1, 2, 2,
|
|
1, 2, 2, 4,
|
|
1, 2, 4, 4,
|
|
2, 4, 4, 4,
|
|
2, 4, 6, 5,
|
|
2, 4, 6, 6,
|
|
2, 5, 8, 8,
|
|
4, 5, 8, 8,
|
|
4, 5, 8, 11,
|
|
4, 8, 10, 11,
|
|
4, 9, 12, 16,
|
|
4, 9, 16, 16,
|
|
6, 10, 12, 18,
|
|
6, 10, 17, 16,
|
|
6, 11, 16, 19,
|
|
6, 13, 18, 21,
|
|
7, 14, 21, 25,
|
|
8, 16, 20, 25,
|
|
8, 17, 23, 25,
|
|
9, 17, 23, 34,
|
|
9, 18, 25, 30,
|
|
10, 20, 27, 32,
|
|
12, 21, 29, 35,
|
|
12, 23, 34, 37,
|
|
12, 25, 34, 40,
|
|
13, 26, 35, 42,
|
|
14, 28, 38, 45,
|
|
15, 29, 40, 48,
|
|
16, 31, 43, 51,
|
|
17, 33, 45, 54,
|
|
18, 35, 48, 57,
|
|
19, 37, 51, 60,
|
|
19, 38, 53, 63,
|
|
20, 40, 56, 66,
|
|
21, 43, 59, 70,
|
|
22, 45, 62, 74,
|
|
24, 47, 65, 77,
|
|
25, 49, 68, 81
|
|
]
|
|
|
|
const EC_CODEWORDS_TABLE = [
|
|
// L M Q H
|
|
7, 10, 13, 17,
|
|
10, 16, 22, 28,
|
|
15, 26, 36, 44,
|
|
20, 36, 52, 64,
|
|
26, 48, 72, 88,
|
|
36, 64, 96, 112,
|
|
40, 72, 108, 130,
|
|
48, 88, 132, 156,
|
|
60, 110, 160, 192,
|
|
72, 130, 192, 224,
|
|
80, 150, 224, 264,
|
|
96, 176, 260, 308,
|
|
104, 198, 288, 352,
|
|
120, 216, 320, 384,
|
|
132, 240, 360, 432,
|
|
144, 280, 408, 480,
|
|
168, 308, 448, 532,
|
|
180, 338, 504, 588,
|
|
196, 364, 546, 650,
|
|
224, 416, 600, 700,
|
|
224, 442, 644, 750,
|
|
252, 476, 690, 816,
|
|
270, 504, 750, 900,
|
|
300, 560, 810, 960,
|
|
312, 588, 870, 1050,
|
|
336, 644, 952, 1110,
|
|
360, 700, 1020, 1200,
|
|
390, 728, 1050, 1260,
|
|
420, 784, 1140, 1350,
|
|
450, 812, 1200, 1440,
|
|
480, 868, 1290, 1530,
|
|
510, 924, 1350, 1620,
|
|
540, 980, 1440, 1710,
|
|
570, 1036, 1530, 1800,
|
|
570, 1064, 1590, 1890,
|
|
600, 1120, 1680, 1980,
|
|
630, 1204, 1770, 2100,
|
|
660, 1260, 1860, 2220,
|
|
720, 1316, 1950, 2310,
|
|
750, 1372, 2040, 2430
|
|
]
|
|
|
|
/**
|
|
* Returns the number of error correction block that the QR Code should contain
|
|
* for the specified version and error correction level.
|
|
*
|
|
* @param {Number} version QR Code version
|
|
* @param {Number} errorCorrectionLevel Error correction level
|
|
* @return {Number} Number of error correction blocks
|
|
*/
|
|
exports.getBlocksCount = function getBlocksCount (version, errorCorrectionLevel) {
|
|
switch (errorCorrectionLevel) {
|
|
case ECLevel.L:
|
|
return EC_BLOCKS_TABLE[(version - 1) * 4 + 0]
|
|
case ECLevel.M:
|
|
return EC_BLOCKS_TABLE[(version - 1) * 4 + 1]
|
|
case ECLevel.Q:
|
|
return EC_BLOCKS_TABLE[(version - 1) * 4 + 2]
|
|
case ECLevel.H:
|
|
return EC_BLOCKS_TABLE[(version - 1) * 4 + 3]
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the number of error correction codewords to use for the specified
|
|
* version and error correction level.
|
|
*
|
|
* @param {Number} version QR Code version
|
|
* @param {Number} errorCorrectionLevel Error correction level
|
|
* @return {Number} Number of error correction codewords
|
|
*/
|
|
exports.getTotalCodewordsCount = function getTotalCodewordsCount (version, errorCorrectionLevel) {
|
|
switch (errorCorrectionLevel) {
|
|
case ECLevel.L:
|
|
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 0]
|
|
case ECLevel.M:
|
|
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 1]
|
|
case ECLevel.Q:
|
|
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 2]
|
|
case ECLevel.H:
|
|
return EC_CODEWORDS_TABLE[(version - 1) * 4 + 3]
|
|
default:
|
|
return undefined
|
|
}
|
|
}
|