UNPKG

react-payme-paycode

Version:

A React component for generating PayMe QR codes 🚀

1,359 lines (1,347 loc) • 68.1 kB
import React, { useRef, useEffect } from 'react'; var QRMode = { MODE_NUMBER: 1 << 0, MODE_ALPHA_NUM: 1 << 1, MODE_8BIT_BYTE: 1 << 2, MODE_KANJI: 1 << 3 }; var QRErrorCorrectionLevel = { L: 1, M: 0, Q: 3, H: 2 }; var QRMaskPattern = { PATTERN000: 0, PATTERN001: 1, PATTERN010: 2, PATTERN011: 3, PATTERN100: 4, PATTERN101: 5, PATTERN110: 6, PATTERN111: 7 }; var 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] ]; var QRMath = /** @class */ (function () { function QRMath() { } QRMath.glog = function (n) { if (n < 1) { throw new Error("glog(".concat(n, ")")); } return this.LOG_TABLE[n]; }; QRMath.gexp = function (n) { while (n < 0) { n += 255; } while (n >= 256) { n -= 255; } return this.EXP_TABLE[n]; }; var _a; _a = QRMath; QRMath.EXP_TABLE = new Array(256); QRMath.LOG_TABLE = new Array(256); (function () { // Initialize tables for (var i = 0; i < 8; i++) { _a.EXP_TABLE[i] = 1 << i; } for (var i = 8; i < 256; i++) { _a.EXP_TABLE[i] = _a.EXP_TABLE[i - 4] ^ _a.EXP_TABLE[i - 5] ^ _a.EXP_TABLE[i - 6] ^ _a.EXP_TABLE[i - 8]; } for (var i = 0; i < 255; i++) { _a.LOG_TABLE[_a.EXP_TABLE[i]] = i; } })(); return QRMath; }()); var QRPolynomial = /** @class */ (function () { function QRPolynomial(num, shift) { if (typeof num.length === 'undefined') { throw new Error("".concat(num.length, "/").concat(shift)); } var offset = 0; while (offset < num.length && num[offset] === 0) { offset++; } this._num = new Array(num.length - offset + shift); for (var i = 0; i < num.length - offset; i++) { this._num[i] = num[i + offset]; } } QRPolynomial.prototype.getAt = function (index) { return this._num[index]; }; QRPolynomial.prototype.getLength = function () { return this._num.length; }; QRPolynomial.prototype.multiply = function (e) { var num = new Array(this.getLength() + e.getLength() - 1); for (var i = 0; i < this.getLength(); i++) { for (var j = 0; j < e.getLength(); j++) { num[i + j] ^= QRMath.gexp(QRMath.glog(this.getAt(i)) + QRMath.glog(e.getAt(j))); } } return new QRPolynomial(num, 0); }; QRPolynomial.prototype.mod = function (e) { if (this.getLength() - e.getLength() < 0) { return this; } var ratio = QRMath.glog(this.getAt(0)) - QRMath.glog(e.getAt(0)); var num = new Array(this.getLength()); for (var i = 0; i < this.getLength(); i++) { num[i] = this.getAt(i); } for (var i = 0; i < e.getLength(); i++) { num[i] ^= QRMath.gexp(QRMath.glog(e.getAt(i)) + ratio); } return new QRPolynomial(num, 0).mod(e); }; return QRPolynomial; }()); var QRRSBlock = /** @class */ (function () { function QRRSBlock() { } QRRSBlock.getRSBlocks = function (typeNumber, errorCorrectionLevel) { var rsBlock = this.getRsBlockTable(typeNumber, errorCorrectionLevel); if (typeof rsBlock === 'undefined') { throw new Error("bad rs block @ typeNumber:".concat(typeNumber, "/errorCorrectionLevel:").concat(errorCorrectionLevel)); } var length = rsBlock.length / 3; var list = []; for (var i = 0; i < length; i++) { var count = rsBlock[i * 3 + 0]; var totalCount = rsBlock[i * 3 + 1]; var dataCount = rsBlock[i * 3 + 2]; for (var j = 0; j < count; j++) { list.push({ totalCount: totalCount, dataCount: dataCount }); } } return list; }; QRRSBlock.getRsBlockTable = function (typeNumber, errorCorrectionLevel) { switch (errorCorrectionLevel) { case QRErrorCorrectionLevel.L: return this.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0]; case QRErrorCorrectionLevel.M: return this.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1]; case QRErrorCorrectionLevel.Q: return this.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2]; case QRErrorCorrectionLevel.H: return this.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3]; default: throw new Error("Invalid error correction level: ".concat(errorCorrectionLevel)); } }; QRRSBlock.RS_BLOCK_TABLE = [ // L // M // Q // H // 1 [1, 26, 19], [1, 26, 16], [1, 26, 13], [1, 26, 9], // 2 [1, 44, 34], [1, 44, 28], [1, 44, 22], [1, 44, 16], // 3 [1, 70, 55], [1, 70, 44], [2, 35, 17], [2, 35, 13], // 4 [1, 100, 80], [2, 50, 32], [2, 50, 24], [4, 25, 9], // 5 [1, 134, 108], [2, 67, 43], [2, 33, 15, 2, 34, 16], [2, 33, 11, 2, 34, 12], // 6 [2, 86, 68], [4, 43, 27], [4, 43, 19], [4, 43, 15], // 7 [2, 98, 78], [4, 49, 31], [2, 32, 14, 4, 33, 15], [4, 39, 13, 1, 40, 14], // 8 [2, 121, 97], [2, 60, 38, 2, 61, 39], [4, 40, 18, 2, 41, 19], [4, 40, 14, 2, 41, 15], // 9 [2, 146, 116], [3, 58, 36, 2, 59, 37], [4, 36, 16, 4, 37, 17], [4, 36, 12, 4, 37, 13], // 10 [2, 86, 68, 2, 87, 69], [4, 69, 43, 1, 70, 44], [6, 43, 19, 2, 44, 20], [6, 43, 15, 2, 44, 16], // 11 [4, 101, 81], [1, 80, 50, 4, 81, 51], [4, 50, 22, 4, 51, 23], [3, 36, 12, 8, 37, 13], // 12 [2, 116, 92, 2, 117, 93], [6, 58, 36, 2, 59, 37], [4, 46, 20, 6, 47, 21], [7, 42, 14, 4, 43, 15], // 13 [4, 133, 107], [8, 59, 37, 1, 60, 38], [8, 44, 20, 4, 45, 21], [12, 33, 11, 4, 34, 12], // 14 [3, 145, 115, 1, 146, 116], [4, 64, 40, 5, 65, 41], [11, 36, 16, 5, 37, 17], [11, 36, 12, 5, 37, 13], // 15 [5, 109, 87, 1, 110, 88], [5, 65, 41, 5, 66, 42], [5, 54, 24, 7, 55, 25], [11, 36, 12, 7, 37, 13], // 16 [5, 122, 98, 1, 123, 99], [7, 73, 45, 3, 74, 46], [15, 43, 19, 2, 44, 20], [3, 45, 15, 13, 46, 16], // 17 [1, 135, 107, 5, 136, 108], [10, 74, 46, 1, 75, 47], [1, 50, 22, 15, 51, 23], [2, 42, 14, 17, 43, 15], // 18 [5, 150, 120, 1, 151, 121], [9, 69, 43, 4, 70, 44], [17, 50, 22, 1, 51, 23], [2, 42, 14, 19, 43, 15], // 19 [3, 141, 113, 4, 142, 114], [3, 70, 44, 11, 71, 45], [17, 47, 21, 4, 48, 22], [9, 39, 13, 16, 40, 14], // 20 [3, 135, 107, 5, 136, 108], [3, 67, 41, 13, 68, 42], [15, 54, 24, 5, 55, 25], [15, 43, 15, 10, 44, 16], // 21 [4, 144, 116, 4, 145, 117], [17, 68, 42], [17, 50, 22, 6, 51, 23], [19, 46, 16, 6, 47, 17], // 22 [2, 139, 111, 7, 140, 112], [17, 74, 46], [7, 54, 24, 16, 55, 25], [34, 37, 13], // 23 [4, 151, 121, 5, 152, 122], [4, 75, 47, 14, 76, 48], [11, 54, 24, 14, 55, 25], [16, 45, 15, 14, 46, 16], // 24 [6, 147, 117, 4, 148, 118], [6, 73, 45, 14, 74, 46], [11, 54, 24, 16, 55, 25], [30, 46, 16, 2, 47, 17], // 25 [8, 132, 106, 4, 133, 107], [8, 75, 47, 13, 76, 48], [7, 54, 24, 22, 55, 25], [22, 45, 15, 13, 46, 16], // 26 [10, 142, 114, 2, 143, 115], [19, 74, 46, 4, 75, 47], [28, 50, 22, 6, 51, 23], [33, 46, 16, 4, 47, 17], // 27 [8, 152, 122, 4, 153, 123], [22, 73, 45, 3, 74, 46], [8, 53, 23, 26, 54, 24], [12, 45, 15, 28, 46, 16], // 28 [3, 147, 117, 10, 148, 118], [3, 73, 45, 23, 74, 46], [4, 54, 24, 31, 55, 25], [11, 45, 15, 31, 46, 16], // 29 [7, 146, 116, 7, 147, 117], [21, 73, 45, 7, 74, 46], [1, 53, 23, 37, 54, 24], [19, 45, 15, 26, 46, 16], // 30 [5, 145, 115, 10, 146, 116], [19, 75, 47, 10, 76, 48], [15, 54, 24, 25, 55, 25], [23, 45, 15, 25, 46, 16], // 31 [13, 145, 115, 3, 146, 116], [2, 74, 46, 29, 75, 47], [42, 54, 24, 1, 55, 25], [23, 45, 15, 28, 46, 16], // 32 [17, 145, 115], [10, 74, 46, 23, 75, 47], [10, 54, 24, 35, 55, 25], [19, 45, 15, 35, 46, 16], // 33 [17, 145, 115, 1, 146, 116], [14, 74, 46, 21, 75, 47], [29, 54, 24, 19, 55, 25], [11, 45, 15, 46, 46, 16], // 34 [13, 145, 115, 6, 146, 116], [14, 74, 46, 23, 75, 47], [44, 54, 24, 7, 55, 25], [59, 46, 16, 1, 47, 17], // 35 [12, 151, 121, 7, 152, 122], [12, 75, 47, 26, 76, 48], [39, 54, 24, 14, 55, 25], [22, 45, 15, 41, 46, 16], // 36 [6, 151, 121, 14, 152, 122], [6, 75, 47, 34, 76, 48], [46, 54, 24, 10, 55, 25], [2, 45, 15, 64, 46, 16], // 37 [17, 152, 122, 4, 153, 123], [29, 74, 46, 14, 75, 47], [49, 54, 24, 10, 55, 25], [24, 45, 15, 46, 46, 16], // 38 [4, 152, 122, 18, 153, 123], [13, 74, 46, 32, 75, 47], [48, 54, 24, 14, 55, 25], [42, 45, 15, 32, 46, 16], // 39 [20, 147, 117, 4, 148, 118], [40, 75, 47, 7, 76, 48], [43, 54, 24, 22, 55, 25], [10, 45, 15, 67, 46, 16], // 40 [19, 148, 118, 6, 149, 119], [18, 75, 47, 31, 76, 48], [34, 54, 24, 34, 55, 25], [20, 45, 15, 61, 46, 16] ]; return QRRSBlock; }()); var QRUtil = /** @class */ (function () { function QRUtil() { } QRUtil.getBCHTypeInfo = function (data) { var d = data << 10; while (this.getBCHDigit(d) - this.getBCHDigit(this.G15) >= 0) { d ^= (this.G15 << (this.getBCHDigit(d) - this.getBCHDigit(this.G15))); } return ((data << 10) | d) ^ this.G15_MASK; }; QRUtil.getBCHTypeNumber = function (data) { var d = data << 12; while (this.getBCHDigit(d) - this.getBCHDigit(this.G18) >= 0) { d ^= (this.G18 << (this.getBCHDigit(d) - this.getBCHDigit(this.G18))); } return (data << 12) | d; }; QRUtil.getBCHDigit = function (data) { var digit = 0; while (data !== 0) { digit++; data >>>= 1; } return digit; }; QRUtil.getPatternPosition = function (typeNumber) { return PATTERN_POSITION_TABLE[typeNumber - 1]; }; QRUtil.getMaskFunction = function (maskPattern) { switch (maskPattern) { case QRMaskPattern.PATTERN000: return function (i, j) { return (i + j) % 2 === 0; }; case QRMaskPattern.PATTERN001: return function (i, j) { return i % 2 === 0; }; case QRMaskPattern.PATTERN010: return function (i, j) { return j % 3 === 0; }; case QRMaskPattern.PATTERN011: return function (i, j) { return (i + j) % 3 === 0; }; case QRMaskPattern.PATTERN100: return function (i, j) { return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0; }; case QRMaskPattern.PATTERN101: return function (i, j) { return (i * j) % 2 + (i * j) % 3 === 0; }; case QRMaskPattern.PATTERN110: return function (i, j) { return ((i * j) % 2 + (i * j) % 3) % 2 === 0; }; case QRMaskPattern.PATTERN111: return function (i, j) { return ((i * j) % 3 + (i + j) % 2) % 2 === 0; }; default: throw new Error("bad maskPattern:".concat(maskPattern)); } }; QRUtil.getErrorCorrectPolynomial = function (errorCorrectLength) { var a = new QRPolynomial([1], 0); for (var i = 0; i < errorCorrectLength; i++) { a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0)); } return a; }; QRUtil.getLengthInBits = function (mode, type) { if (1 <= type && type < 10) { switch (mode) { case QRMode.MODE_NUMBER: return 10; case QRMode.MODE_ALPHA_NUM: return 9; case QRMode.MODE_8BIT_BYTE: return 8; case QRMode.MODE_KANJI: return 8; default: throw new Error("mode:".concat(mode)); } } else if (type < 27) { switch (mode) { case QRMode.MODE_NUMBER: return 12; case QRMode.MODE_ALPHA_NUM: return 11; case QRMode.MODE_8BIT_BYTE: return 16; case QRMode.MODE_KANJI: return 10; default: throw new Error("mode:".concat(mode)); } } else if (type < 41) { switch (mode) { case QRMode.MODE_NUMBER: return 14; case QRMode.MODE_ALPHA_NUM: return 13; case QRMode.MODE_8BIT_BYTE: return 16; case QRMode.MODE_KANJI: return 12; default: throw new Error("mode:".concat(mode)); } } else { throw new Error("type:".concat(type)); } }; QRUtil.getLostPoint = function (qrcode) { var moduleCount = qrcode.getModuleCount(); var lostPoint = 0; // LEVEL1 for (var row = 0; row < moduleCount; row++) { for (var col = 0; col < moduleCount; col++) { var sameCount = 0; var dark = qrcode.isDark(row, col); for (var r = -1; r <= 1; r++) { if (row + r < 0 || moduleCount <= row + r) { continue; } for (var 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 (var row = 0; row < moduleCount - 1; row++) { for (var col = 0; col < moduleCount - 1; col++) { var 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 (var row = 0; row < moduleCount; row++) { for (var 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 (var col = 0; col < moduleCount; col++) { for (var 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 var darkCount = 0; for (var col = 0; col < moduleCount; col++) { for (var row = 0; row < moduleCount; row++) { if (qrcode.isDark(row, col)) { darkCount++; } } } var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5; lostPoint += ratio * 10; return lostPoint; }; QRUtil.G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0); QRUtil.G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0); QRUtil.G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1); return QRUtil; }()); var QR8bitByte = /** @class */ (function () { function QR8bitByte(data) { this.data = data; this.parsedData = []; // Add UTF-8 BOM this.parsedData.push(0xef, 0xbb, 0xbf); for (var i = 0; i < this.data.length; i++) { var byteArray = this.getUTF8Bytes(this.data.charAt(i)); for (var j = 0; j < byteArray.length; j++) { this.parsedData.push(byteArray[j]); } } } QR8bitByte.prototype.getUTF8Bytes = function (char) { var code = char.charCodeAt(0); if (code <= 0x7f) { return [code]; } else if (code <= 0x7ff) { return [ 0xc0 | ((code >> 6) & 0x1f), 0x80 | (code & 0x3f) ]; } else if (code <= 0xffff) { return [ 0xe0 | ((code >> 12) & 0x0f), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f) ]; } else if (code <= 0x1fffff) { return [ 0xf0 | ((code >> 18) & 0x07), 0x80 | ((code >> 12) & 0x3f), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f) ]; } else if (code <= 0x3ffffff) { return [ 0xf8 | ((code >> 24) & 0x03), 0x80 | ((code >> 18) & 0x3f), 0x80 | ((code >> 12) & 0x3f), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f) ]; } else { return [ 0xfc | ((code >> 30) & 0x01), 0x80 | ((code >> 24) & 0x3f), 0x80 | ((code >> 18) & 0x3f), 0x80 | ((code >> 12) & 0x3f), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f) ]; } }; QR8bitByte.prototype.getMode = function () { return QRMode.MODE_8BIT_BYTE; }; QR8bitByte.prototype.getLength = function () { return this.parsedData.length; }; QR8bitByte.prototype.write = function (buffer) { for (var i = 0; i < this.parsedData.length; i++) { buffer.put(this.parsedData[i], 8); } }; return QR8bitByte; }()); var QRAlphanumeric = /** @class */ (function () { function QRAlphanumeric(data) { this.data = data; this.parsedData = []; // Check if the data contains only valid alphanumeric characters for (var i = 0; i < this.data.length; i++) { var c = this.data.charAt(i); if (!this.isValidChar(c)) { throw new Error("Invalid alphanumeric character: ".concat(c)); } } // Parse the data into pairs of characters for (var i = 0; i < this.data.length; i += 2) { if (i + 1 < this.data.length) { this.parsedData.push(this.getCode(this.data.charAt(i)) * 45 + this.getCode(this.data.charAt(i + 1))); } else { this.parsedData.push(this.getCode(this.data.charAt(i))); } } } QRAlphanumeric.prototype.isValidChar = function (c) { return /^[0-9A-Z $%*+\-./:]*$/.test(c); }; QRAlphanumeric.prototype.getCode = function (c) { if (c >= '0' && c <= '9') { return c.charCodeAt(0) - '0'.charCodeAt(0); } else if (c >= 'A' && c <= 'Z') { return c.charCodeAt(0) - 'A'.charCodeAt(0) + 10; } else { switch (c) { case ' ': return 36; case '$': return 37; case '%': return 38; case '*': return 39; case '+': return 40; case '-': return 41; case '.': return 42; case '/': return 43; case ':': return 44; default: throw new Error("Invalid alphanumeric character: ".concat(c)); } } }; QRAlphanumeric.prototype.getMode = function () { return QRMode.MODE_ALPHA_NUM; }; QRAlphanumeric.prototype.getLength = function () { return this.parsedData.length; }; QRAlphanumeric.prototype.write = function (buffer) { for (var i = 0; i < this.parsedData.length; i++) { buffer.push(this.parsedData[i]); } }; return QRAlphanumeric; }()); var QRNumber = /** @class */ (function () { function QRNumber(data) { this.data = data; this.parsedData = []; // Check if the data contains only valid numeric characters if (!/^\d*$/.test(this.data)) { throw new Error('Invalid numeric data'); } // Parse the data into groups of 3 digits for (var i = 0; i < this.data.length; i += 3) { var group = this.data.substring(i, i + 3); this.parsedData.push(parseInt(group, 10)); } } QRNumber.prototype.getMode = function () { return QRMode.MODE_NUMBER; }; QRNumber.prototype.getLength = function () { return this.parsedData.length; }; QRNumber.prototype.write = function (buffer) { for (var i = 0; i < this.parsedData.length; i++) { buffer.push(this.parsedData[i]); } }; return QRNumber; }()); var QRKanji = /** @class */ (function () { function QRKanji(data) { this.data = data; this.parsedData = []; // Check if the data contains only valid Kanji characters for (var i = 0; i < this.data.length; i++) { var c = this.data.charCodeAt(i); if (!this.isValidKanji(c)) { throw new Error("Invalid Kanji character: ".concat(this.data.charAt(i))); } } // Parse the data into Kanji codes for (var i = 0; i < this.data.length; i++) { var c = this.data.charCodeAt(i); var code = this.getKanjiCode(c); this.parsedData.push(code); } } QRKanji.prototype.isValidKanji = function (code) { return (code >= 0x8140 && code <= 0x9ffc) || (code >= 0xe040 && code <= 0xebbf); }; QRKanji.prototype.getKanjiCode = function (code) { if (code >= 0x8140 && code <= 0x9ffc) { code -= 0x8140; } else if (code >= 0xe040 && code <= 0xebbf) { code -= 0xc140; } else { throw new Error("Invalid Kanji code: ".concat(code)); } var high = code >> 8; var low = code & 0xff; return high * 0xc0 + low; }; QRKanji.prototype.getMode = function () { return QRMode.MODE_KANJI; }; QRKanji.prototype.getLength = function () { return this.parsedData.length; }; QRKanji.prototype.write = function (buffer) { for (var i = 0; i < this.parsedData.length; i++) { buffer.push(this.parsedData[i]); } }; return QRKanji; }()); var QRBitBuffer = /** @class */ (function () { function QRBitBuffer() { this._buffer = []; this._length = 0; } QRBitBuffer.prototype.getBuffer = function () { return this._buffer; }; QRBitBuffer.prototype.getAt = function (index) { var bufIndex = Math.floor(index / 8); return ((this._buffer[bufIndex] >>> (7 - index % 8)) & 1) === 1; }; QRBitBuffer.prototype.put = function (num, length) { for (var i = 0; i < length; i++) { this.putBit(((num >>> (length - i - 1)) & 1) === 1); } }; QRBitBuffer.prototype.getLengthInBits = function () { return this._length; }; QRBitBuffer.prototype.putBit = function (bit) { var bufIndex = Math.floor(this._length / 8); if (this._buffer.length <= bufIndex) { this._buffer.push(0); } if (bit) { this._buffer[bufIndex] |= (0x80 >>> (this._length % 8)); } this._length++; }; return QRBitBuffer; }()); var QRCode = /** @class */ (function () { function QRCode(typeNumber, errorCorrectionLevel) { this._modules = null; this._moduleCount = 0; this._dataCache = null; this._dataList = []; this._typeNumber = typeNumber; this._errorCorrectionLevel = QRErrorCorrectionLevel[errorCorrectionLevel]; } QRCode.prototype.addData = function (data, mode) { if (mode === void 0) { mode = 'Byte'; } var newData; switch (mode) { case 'Numeric': newData = new QRNumber(data); break; case 'Alphanumeric': newData = new QRAlphanumeric(data); break; case 'Byte': newData = new QR8bitByte(data); break; case 'Kanji': newData = new QRKanji(data); break; default: throw new Error("Invalid mode: ".concat(mode)); } this._dataList.push(newData); this._dataCache = null; }; QRCode.prototype.make = function () { if (this._typeNumber < 1) { var typeNumber = 1; for (; typeNumber < 40; typeNumber++) { var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, this._errorCorrectionLevel); var buffer = new QRBitBuffer(); for (var i = 0; i < this._dataList.length; i++) { var data = this._dataList[i]; buffer.put(data.getMode(), 4); buffer.put(data.getLength(), QRUtil.getLengthInBits(data.getMode(), typeNumber)); data.write(buffer); } var totalDataCount = 0; for (var i = 0; i < rsBlocks.length; i++) { totalDataCount += rsBlocks[i].dataCount; } if (buffer.getLengthInBits() <= totalDataCount * 8) { break; } } this._typeNumber = typeNumber; } this.makeImpl(false, this.getBestMaskPattern()); }; QRCode.prototype.makeImpl = function (test, maskPattern) { var _this = this; this._moduleCount = this._typeNumber * 4 + 17; this._modules = Array(this._moduleCount).fill(null).map(function () { return Array(_this._moduleCount).fill(null); }); this.setupPositionProbePattern(0, 0); this.setupPositionProbePattern(this._moduleCount - 7, 0); this.setupPositionProbePattern(0, this._moduleCount - 7); this.setupPositionAdjustPattern(); this.setupTimingPattern(); this.setupTypeInfo(test, maskPattern); if (this._typeNumber >= 7) { this.setupTypeNumber(test); } if (this._dataCache == null) { this._dataCache = this.createData(this._typeNumber, this._errorCorrectionLevel, this._dataList); } this.mapData(this._dataCache, maskPattern); }; QRCode.prototype.setupPositionProbePattern = function (row, col) { for (var r = -1; r <= 7; r++) { if (row + r <= -1 || this._moduleCount <= row + r) continue; for (var c = -1; c <= 7; c++) { if (col + c <= -1 || this._moduleCount <= col + c) continue; if ((0 <= r && r <= 6 && (c === 0 || c === 6)) || (0 <= c && c <= 6 && (r === 0 || r === 6)) || (2 <= r && r <= 4 && 2 <= c && c <= 4)) { this._modules[row + r][col + c] = true; } else { this._modules[row + r][col + c] = false; } } } }; QRCode.prototype.setupPositionAdjustPattern = function () { var pos = QRUtil.getPatternPosition(this._typeNumber); for (var i = 0; i < pos.length; i++) { for (var j = 0; j < pos.length; j++) { var row = pos[i]; var col = pos[j]; if (this._modules[row][col] != null) { continue; } for (var r = -2; r <= 2; r++) { for (var c = -2; c <= 2; c++) { if (r === -2 || r === 2 || c === -2 || c === 2 || (r === 0 && c === 0)) { this._modules[row + r][col + c] = true; } else { this._modules[row + r][col + c] = false; } } } } } }; QRCode.prototype.setupTimingPattern = function () { for (var r = 8; r < this._moduleCount - 8; r++) { if (this._modules[r][6] != null) { continue; } this._modules[r][6] = (r % 2 === 0); } for (var c = 8; c < this._moduleCount - 8; c++) { if (this._modules[6][c] != null) { continue; } this._modules[6][c] = (c % 2 === 0); } }; QRCode.prototype.setupTypeInfo = function (test, maskPattern) { var data = (this._errorCorrectionLevel << 3) | maskPattern; var bits = QRUtil.getBCHTypeInfo(data); // vertical for (var i = 0; i < 15; i++) { var mod = (!test && ((bits >> i) & 1) === 1); if (i < 6) { this._modules[i][8] = mod; } else if (i < 8) { this._modules[i + 1][8] = mod; } else { this._modules[this._moduleCount - 15 + i][8] = mod; } } // horizontal for (var i = 0; i < 15; i++) { var mod = (!test && ((bits >> i) & 1) === 1); if (i < 8) { this._modules[8][this._moduleCount - i - 1] = mod; } else if (i < 9) { this._modules[8][15 - i - 1 + 1] = mod; } else { this._modules[8][15 - i - 1] = mod; } } // fixed module this._modules[this._moduleCount - 8][8] = (!test); }; QRCode.prototype.setupTypeNumber = function (test) { var bits = QRUtil.getBCHTypeNumber(this._typeNumber); for (var i = 0; i < 18; i++) { var mod = (!test && ((bits >> i) & 1) === 1); this._modules[Math.floor(i / 3)][i % 3 + this._moduleCount - 8 - 3] = mod; } for (var i = 0; i < 18; i++) { var mod = (!test && ((bits >> i) & 1) === 1); this._modules[i % 3 + this._moduleCount - 8 - 3][Math.floor(i / 3)] = mod; } }; QRCode.prototype.getBestMaskPattern = function () { var minLostPoint = 0; var pattern = 0; for (var i = 0; i < 8; i++) { this.makeImpl(true, i); var lostPoint = QRUtil.getLostPoint(this); if (i === 0 || minLostPoint > lostPoint) { minLostPoint = lostPoint; pattern = i; } } return pattern; }; QRCode.prototype.createData = function (typeNumber, errorCorrectionLevel, dataList) { var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, errorCorrectionLevel); var buffer = new QRBitBuffer(); for (var i = 0; i < dataList.length; i++) { var data = dataList[i]; buffer.put(data.getMode(), 4); buffer.put(data.getLength(), QRUtil.getLengthInBits(data.getMode(), typeNumber)); data.write(buffer); } var totalDataCount = 0; for (var i = 0; i < rsBlocks.length; i++) { totalDataCount += rsBlocks[i].dataCount; } if (buffer.getLengthInBits() > totalDataCount * 8) { throw new Error("Code length overflow. (".concat(buffer.getLengthInBits(), ">").concat(totalDataCount * 8, ")")); } // end code if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) { buffer.put(0, 4); } // padding while (buffer.getLengthInBits() % 8 !== 0) { buffer.putBit(false); } // padding while (true) { if (buffer.getLengthInBits() >= totalDataCount * 8) { break; } buffer.put(0xEC, 8); if (buffer.getLengthInBits() >= totalDataCount * 8) { break; } buffer.put(0x11, 8); } return this.createBytes(buffer, rsBlocks); }; QRCode.prototype.createBytes = function (buffer, rsBlocks) { var offset = 0; var maxDcCount = 0; var maxEcCount = 0; var dcdata = new Array(rsBlocks.length); var ecdata = new Array(rsBlocks.length); for (var r = 0; r < rsBlocks.length; r++) { var dcCount = rsBlocks[r].dataCount; var ecCount = rsBlocks[r].totalCount - dcCount; maxDcCount = Math.max(maxDcCount, dcCount); maxEcCount = Math.max(maxEcCount, ecCount); dcdata[r] = new Array(dcCount); for (var i = 0; i < dcdata[r].length; i++) { dcdata[r][i] = 0xff & buffer.getBuffer()[i + offset]; } offset += dcCount; var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount); var rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1); var modPoly = rawPoly.mod(rsPoly); ecdata[r] = new Array(rsPoly.getLength() - 1); for (var i = 0; i < ecdata[r].length; i++) { var modIndex = i + modPoly.getLength() - ecdata[r].length; ecdata[r][i] = (modIndex >= 0) ? modPoly.getAt(modIndex) : 0; } } var totalCodeCount = 0; for (var i = 0; i < rsBlocks.length; i++) { totalCodeCount += rsBlocks[i].totalCount; } var data = new Array(totalCodeCount); var index = 0; for (var i = 0; i < maxDcCount; i++) { for (var r = 0; r < rsBlocks.length; r++) { if (i < dcdata[r].length) { data[index] = dcdata[r][i]; index++; } } } for (var i = 0; i < maxEcCount; i++) { for (var r = 0; r < rsBlocks.length; r++) { if (i < ecdata[r].length) { data[index] = ecdata[r][i]; index++; } } } return data; }; QRCode.prototype.mapData = function (data, maskPattern) { var inc = -1; var row = this._moduleCount - 1; var bitIndex = 7; var byteIndex = 0; var maskFunc = QRUtil.getMaskFunction(maskPattern); for (var col = this._moduleCount - 1; col > 0; col -= 2) { if (col === 6) col--; while (true) { for (var c = 0; c < 2; c++) { if (this._modules[row][col - c] === null) { var dark = false; if (byteIndex < data.length) { dark = (((data[byteIndex] >>> bitIndex) & 1) === 1); } var mask = maskFunc(row, col - c); if (mask) { dark = !dark; } this._modules[row][col - c] = dark; bitIndex--; if (bitIndex === -1) { byteIndex++; bitIndex = 7; } } } row += inc; if (row < 0 || this._moduleCount <= row) { row -= inc; inc = -inc; break; } } } }; QRCode.prototype.isDark = function (row, col) { if (row < 0 || this._moduleCount <= row || col < 0 || this._moduleCount <= col) { throw new Error("".concat(row, ",").concat(col)); } return this._modules[row][col]; }; QRCode.prototype.getModuleCount = function () { return this._moduleCount; }; QRCode.prototype.renderTo2dContext = function (context, cellSize) { if (cellSize === void 0) { cellSize = 2; } var length = this.getModuleCount(); for (var row = 0; row < length; row++) { for (var col = 0; col < length; col++) { context.fillStyle = this.isDark(row, col) ? 'black' : 'white'; context.fillRect(row * cellSize, col * cellSize, cellSize, cellSize); } } }; QRCode.prototype.createTableTag = function (cellSize, margin) { if (cellSize === void 0) { cellSize = 2; } if (margin === void 0) { margin = cellSize * 4; } var qrHtml = ''; qrHtml += '<table style="'; qrHtml += ' border-width: 0px; border-style: none;'; qrHtml += ' border-collapse: collapse;'; qrHtml += ' padding: 0px; margin: ' + margin + 'px;'; qrHtml += '">'; qrHtml += '<tbody>'; for (var r = 0; r < this.getModuleCount(); r++) { qrHtml += '<tr>'; for (var c = 0; c < this.getModuleCount(); c++) { qrHtml += '<td style="'; qrHtml += ' border-width: 0px; border-style: none;'; qrHtml += ' border-collapse: collapse;'; qrHtml += ' padding: 0px; margin: 0px;'; qrHtml += ' width: ' + cellSize + 'px;'; qrHtml += ' height: ' + cellSize + 'px;'; qrHtml += ' background-color: '; qrHtml += this.isDark(r, c) ? '#000000' : '#ffffff'; qrHtml += ';'; qrHtml += '"/>'; } qrHtml += '</tr>'; } qrHtml += '</tbody>'; qrHtml += '</table>'; return qrHtml; }; QRCode.prototype.createSvgTag = function (cellSize, margin) { if (cellSize === void 0) { cellSize = 2; } if (margin === void 0) { margin = cellSize * 4; } var size = this.getModuleCount() * cellSize + margin * 2; var qrSvg = ''; qrSvg += '<svg version="1.1" xmlns="http://www.w3.org/2000/svg"'; qrSvg += ' width="' + size + 'px"'; qrSvg += ' height="' + size + 'px"'; qrSvg += ' viewBox="0 0 ' + size + ' ' + size + '" '; qrSvg += ' preserveAspectRatio="xMinYMin meet">'; qrSvg += '<rect width="100%" height="100%" fill="white" cx="0" cy="0"/>'; qrSvg += '<path d="'; for (var r = 0; r < this.getModuleCount(); r++) { var mr = r * cellSize + margin; for (var c = 0; c < this.getModuleCount(); c++) { if (this.isDark(r, c)) { var mc = c * cellSize + margin; qrSvg += 'M' + mc + ',' + mr + 'l' + cellSize + ',0 0,' + cellSize + ' -' + cellSize + ',0 0,-' + cellSize + 'z '; } } } qrSvg += '" stroke="transparent" fill="black"/>'; qrSvg += '</svg>'; return qrSvg; }; QRCode.prototype.createDataURL = function (cellSize, margin) { if (cellSize === void 0) { cellSize = 2; } if (margin === void 0) { margin = cellSize * 4; } var size = this.getModuleCount() * cellSize + margin * 2; var min = margin; var canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; var context = canvas.getContext('2d'); context.fillStyle = 'white'; context.fillRect(0, 0, size, size); for (var row = 0; row < this.getModuleCount(); row++) { for (var col = 0; col < this.getModuleCount(); col++) { var w = (Math.ceil((col + 1) * cellSize) - Math.floor(col * cellSize)); var h = (Math.ceil((row + 1) * cellSize) - Math.floor(row * cellSize)); context.fillStyle = this.isDark(row, col) ? 'black' : 'white'; context.fillRect(Math.round(col * cellSize) + min, Math.round(row * cellSize) + min, w, h); } } return canvas.toDataURL('image/png'); }; QRCode.prototype.createImgTag = function (cellSize, margin, alt) { if (cellSize === void 0) { cellSize = 2; } if (margin === void 0) { margin = cellSize * 4; } var size = this.getModuleCount() * cellSize + margin * 2; var img = ''; img += '<img'; img += '\u0020src="'; img += this.createDataURL(cellSize, margin); img += '"'; img += '\u0020width="'; img += size; img += '"'; img += '\u0020height="'; img += size; img += '"'; if (alt) { img += '\u0020alt="'; img += alt; img += '"'; } img += '/>'; return img; }; QRCode.prototype.createASCII = function (cellSize, margin) { if (cellSize === void 0) { cellSize = 1; } if (margin === void 0) { margin = cellSize * 2; } if (cellSize < 2) { return this._createHalfASCII(margin); } cellSize -= 1; margin = (typeof margin === 'undefined') ? cellSize * 2 : margin; var size = this.getModuleCount() * cellSize + margin * 2; var min = margin; var max = size - margin; var ascii = ''; var line = ''; for (var y = 0; y < size; y++) { var r = Math.floor((y - min) / cellSize); line = ''; for (var x = 0; x < size; x++) { var p = 1; if (min <= x && x < max && min <= y && y < max && this.isDark(r, Math.floor((x - min) / cellSize))) { p = 0; } line += p ? '██' : ' '; } for (var r_1 = 0; r_1 < cellSize; r_1++) { ascii += line + '\n'; } } return ascii.substring(0, ascii.length - 1); }; QRCode.prototype._createHalfASCII = function (margin) { var cellSize = 1; margin = (typeof margin === 'undefined') ? cellSize * 2 : margin; var size = this.getModuleCount() * cellSize + margin * 2; var min = margin; var max = size - margin; var blocks = { '██': '█', '█ ': '▀', ' █': '▄', ' ': ' ' }; var ascii = ''; for (var y = 0; y < size; y += 2) { var r1 = Math.floor((y - min) / cellSize); var r2 = Math.floor((y + 1 - min) / cellSize); for (var x = 0; x < size; x++) { var p = '█'; if (min <= x && x < max && min <= y && y < max && this.isDark(r1, Math.floor((x - min) / cellSize))) { p = ' '; } if (min <= x && x < max && min <= y + 1 && y + 1 < max && this.isDark(r2, Math.floor((x - min) / cellSize))) { p += ' '; } else { p += '█'; } ascii += blocks[p]; } ascii += '\n'; } if (size % 2) { return ascii.substring(0, ascii.length - size - 1) + Array(size + 1).join('▀'); } return ascii.substring(0, ascii.length - 1); }; return QRCode; }()); function drawEye(_a) { var ctx = _a.ctx; _a.width; var margin = _a.margin, cellRadius = _a.cellRadius, xOffset = _a.xOffset, yOffset = _a.yOffset; _a.offset; // outer edge ctx.beginPath(); ctx.moveTo(xOffset + 11 * cellRadius + margin, yOffset + 0 * cellRadius + margin); ctx.arcTo(xOffset + 14 * cellRadius + margin, yOffset + 0 * cellRadius + margin, xOffset + 14 * cellRadius + margin, yOffset + 3 * cellRadius + margin, 3 * cellRadius); ctx.arcTo(xOffset + 14 * cellRadius + margin, yOffset + 14 * cellRadius + margin, xOffset + 12 * cellRadius + margin, yOffset + 14 * cellRadius + margin, 3 * cellRadius); ctx.arcTo(xOffset + 0 * cellRadius + margin, yOffset + 14 * cellRadius + margin, xOffset + 0 * cellRadius + margin, yOffset + 11 * cellRadius + margin, 3 * cellRadius); ctx.arcTo(xOffset + 0 * cellRadius + margin, yOffset + 0 * cellRadius + margin, xOffset + 3 * cellRadius + margin, yOffset + 0 * cellRadius + margin, 3 * cellRadius); ctx.lineTo(xOffset + 11 * cellRadius + margin, yOffset + 0 * cellRadius + margin); // inner edge ctx.moveTo(xOffset + 4 * cellRadius + margin, yOffset + 2 * cellRadius + margin); ctx.arcTo(xOffset + 2 * cellRadius + margin, yOffset + 2 * cellRadius + margin, xOffset + 2 * cellRadius + margin, yOffset + 4 * cellRadius + margin, 2 * cellRadius); ctx.arcTo(xOffset + 2 * cellRadius + margin, yOffset + 12 * cellRadius + margin, xOffset + 4 * cellRadius + margin, yOffset + 12 * cellRadius + margin, 2 * cellRadius); ctx.arcTo(xOffset + 12 * cellRadius + margin, yOffset + 12 * cellRadius + margin, xOffset + 12 * cellRadius + margin, yOffset + 10 * cellRadius + margin, 2 * cellRadius); ctx.arcTo(xOffset + 12 * cellRadius + margin, yOffset + 2 * cellRadius + margin, xOffset + 10 * cellRadius + margin, yOffset + 2 * cellRadius + margin, 2 * cellRadius); ctx.lineTo(xOffset + 4 * cellRadius + margin, yOffset + 2 * cellRadius + margin); ctx.closePath(); ctx.fill(); // central rect ctx.beginPath(); ctx.moveTo(xOffset + 8 * cellRadius + margin, yOffset + 4 * cellRadius + margin); ctx.arcTo(xOffset + 10 * cellRadius + margin, yOffset + 4 * cellRadius + margin, xOffset + 10 * cellRadius + margin, yOffset + 6 * cellRadius + margin, cellRadius); ctx.arcTo(xOffset + 10 * cellRadius + margin, yOffset + 10 * cellRadius + margin, xOffset + 8 * cellRadius + margin, yOffset + 10 * cellRadius + margin, cellRadius); ctx.arcTo(xOffset + 4 * cellRadius + margin, yOffset + 10 * cellRadius + margin, xOffset + 4 * cellRadius + margin, yOffset + 8 * cellRadius + margin, cellRadius); ctx.arcTo(xOffset + 4 * cellRadius + margin, yOffset + 4 * cellRadius + margin, xOffset + 6 * cellRadius + margin, yOffset + 4 * cellRadius + margin, cellRadius); ctx.lineTo(xOffset + 8 * cellRadius + margin, yOffset + 4 * cellRadius + margin); ctx.closePath(); ctx.fill(); } var CONSUMER_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAYAAABV7bNHAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAA5pSURBVHhe7ZsJdFTVGccnCwkBAiQRZEcWOezIohVBBURcyiYeWxbRtgdQFHBjEykIQkEjAiKFVuqOx8opdQOUyg4uCIICQiaTSSaZTCaZSSb7OsnX7//d9yYJTA0hL4B1cs7/zHv33Xfn3t+797vf972JKd4URQH9bwUA1aAAoBoUAFSDAoBqUABQDQoAqkEBQDUoAKgGXRWAzKZmrEhWE004bua37uXWFQLUjOJM4XTOZBLFmSIoPvQasjRpx2rPxy0YUESV62FXDNhlBBQts0MNuCHZ+g4m1/wllP/Z51Ry1kxlznTyZnlEZc4MKomLp/ydu8m9aBnZBtzK9zaSe9EG2vL/HcbrsgDC08fg4iNaUMacuVTy0zkqLyzkz7OU88a7XDaPHOMmkf3231IKK3XsRMqYNZeyt7xFxafPqLpmC7nmLiJLZCsNVFNuu/5B1TOgaFkecaZQcs6YRd70DCpLTSP38ysosWtfuYbBKgVLPQjHenmcqQFZO/Ui16KlVGpLIW9mlkDGEsW1+oZUj4CiZYAJbbtS4cEjVJ5XQ