mirror of
https://github.com/status-im/QR-Code-generator.git
synced 2025-02-24 10:28:20 +00:00
Changed and simplified C++ code by converting Ecc from class to enum, updated related code.
This commit is contained in:
parent
6b33f4fd9c
commit
3208954e81
@ -38,27 +38,17 @@ using std::vector;
|
|||||||
|
|
||||||
namespace qrcodegen {
|
namespace qrcodegen {
|
||||||
|
|
||||||
QrCode::Ecc::Ecc(int ord, int fb) :
|
int QrCode::getFormatBits(Ecc ecl) {
|
||||||
ordinal(ord),
|
switch (ecl) {
|
||||||
formatBits(fb) {}
|
case Ecc::LOW : return 1;
|
||||||
|
case Ecc::MEDIUM : return 0;
|
||||||
|
case Ecc::QUARTILE: return 3;
|
||||||
int QrCode::Ecc::getOrdinal() const {
|
case Ecc::HIGH : return 2;
|
||||||
return ordinal;
|
default: throw "Assertion error";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrCode::Ecc::getFormatBits() const {
|
|
||||||
return formatBits;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const QrCode::Ecc QrCode::Ecc::LOW (0, 1);
|
|
||||||
const QrCode::Ecc QrCode::Ecc::MEDIUM (1, 0);
|
|
||||||
const QrCode::Ecc QrCode::Ecc::QUARTILE(2, 3);
|
|
||||||
const QrCode::Ecc QrCode::Ecc::HIGH (3, 2);
|
|
||||||
|
|
||||||
|
|
||||||
QrCode QrCode::encodeText(const char *text, Ecc ecl) {
|
QrCode QrCode::encodeText(const char *text, Ecc ecl) {
|
||||||
vector<QrSegment> segs(QrSegment::makeSegments(text));
|
vector<QrSegment> segs(QrSegment::makeSegments(text));
|
||||||
return encodeSegments(segs, ecl);
|
return encodeSegments(segs, ecl);
|
||||||
@ -224,7 +214,7 @@ void QrCode::drawFunctionPatterns() {
|
|||||||
|
|
||||||
void QrCode::drawFormatBits(int mask) {
|
void QrCode::drawFormatBits(int mask) {
|
||||||
// Calculate error correction code and pack bits
|
// Calculate error correction code and pack bits
|
||||||
int data = errorCorrectionLevel.getFormatBits() << 3 | mask; // errCorrLvl is uint2, mask is uint3
|
int data = getFormatBits(errorCorrectionLevel) << 3 | mask; // errCorrLvl is uint2, mask is uint3
|
||||||
int rem = data;
|
int rem = data;
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
|
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
|
||||||
@ -309,8 +299,8 @@ vector<uint8_t> QrCode::appendErrorCorrection(const vector<uint8_t> &data) const
|
|||||||
throw "Invalid argument";
|
throw "Invalid argument";
|
||||||
|
|
||||||
// Calculate parameter numbers
|
// Calculate parameter numbers
|
||||||
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.getOrdinal()][version];
|
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(errorCorrectionLevel)][version];
|
||||||
int blockEccLen = ECC_CODEWORDS_PER_BLOCK[errorCorrectionLevel.getOrdinal()][version];
|
int blockEccLen = ECC_CODEWORDS_PER_BLOCK[static_cast<int>(errorCorrectionLevel)][version];
|
||||||
int rawCodewords = getNumRawDataModules(version) / 8;
|
int rawCodewords = getNumRawDataModules(version) / 8;
|
||||||
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
||||||
int shortBlockLen = rawCodewords / numBlocks;
|
int shortBlockLen = rawCodewords / numBlocks;
|
||||||
@ -537,8 +527,8 @@ int QrCode::getNumDataCodewords(int ver, Ecc ecl) {
|
|||||||
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
||||||
throw "Version number out of range";
|
throw "Version number out of range";
|
||||||
return getNumRawDataModules(ver) / 8
|
return getNumRawDataModules(ver) / 8
|
||||||
- ECC_CODEWORDS_PER_BLOCK[ecl.getOrdinal()][ver]
|
- ECC_CODEWORDS_PER_BLOCK[static_cast<int>(ecl)][ver]
|
||||||
* NUM_ERROR_CORRECTION_BLOCKS[ecl.getOrdinal()][ver];
|
* NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,25 +44,16 @@ class QrCode final {
|
|||||||
/*
|
/*
|
||||||
* Represents the error correction level used in a QR Code symbol.
|
* Represents the error correction level used in a QR Code symbol.
|
||||||
*/
|
*/
|
||||||
public: class Ecc final {
|
public: enum class Ecc {
|
||||||
// Constants declared in ascending order of error protection.
|
// Constants declared in ascending order of error protection.
|
||||||
public: const static Ecc LOW, MEDIUM, QUARTILE, HIGH;
|
LOW = 0, MEDIUM = 1, QUARTILE = 2, HIGH = 3
|
||||||
|
|
||||||
// Fields.
|
|
||||||
private: int ordinal;
|
|
||||||
private: int formatBits;
|
|
||||||
|
|
||||||
// Constructor.
|
|
||||||
private: Ecc(int ord, int fb);
|
|
||||||
|
|
||||||
// (Public) Returns a value in the range 0 to 3 (unsigned 2-bit integer).
|
|
||||||
public: int getOrdinal() const;
|
|
||||||
|
|
||||||
// (Package-private) Returns a value in the range 0 to 3 (unsigned 2-bit integer).
|
|
||||||
public: int getFormatBits() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Returns a value in the range 0 to 3 (unsigned 2-bit integer).
|
||||||
|
private: static int getFormatBits(Ecc ecl);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Public static factory functions ----*/
|
/*---- Public static factory functions ----*/
|
||||||
|
|
||||||
|
@ -37,11 +37,11 @@ using qrcodegen::QrCode;
|
|||||||
using qrcodegen::QrSegment;
|
using qrcodegen::QrSegment;
|
||||||
|
|
||||||
|
|
||||||
static const QrCode::Ecc *(ECC_LEVELS[]) = {
|
static const QrCode::Ecc ECC_LEVELS[] = {
|
||||||
&QrCode::Ecc::LOW,
|
QrCode::Ecc::LOW,
|
||||||
&QrCode::Ecc::MEDIUM,
|
QrCode::Ecc::MEDIUM,
|
||||||
&QrCode::Ecc::QUARTILE,
|
QrCode::Ecc::QUARTILE,
|
||||||
&QrCode::Ecc::HIGH,
|
QrCode::Ecc::HIGH,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ int main() {
|
|||||||
|
|
||||||
try { // Try to make QR Code symbol
|
try { // Try to make QR Code symbol
|
||||||
const QrCode qr = QrCode::encodeSegments(segs,
|
const QrCode qr = QrCode::encodeSegments(segs,
|
||||||
*ECC_LEVELS[errCorLvl], minVersion, maxVersion, mask, boostEcl == 1);
|
ECC_LEVELS[errCorLvl], minVersion, maxVersion, mask, boostEcl == 1);
|
||||||
// Print grid of modules
|
// Print grid of modules
|
||||||
std::cout << qr.getVersion() << std::endl;
|
std::cout << qr.getVersion() << std::endl;
|
||||||
for (int y = 0; y < qr.getSize(); y++) {
|
for (int y = 0; y < qr.getSize(); y++) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user