1
mirror of https://github.com/monero-project/monero-gui synced 2024-12-29 23:26:24 +01:00

Merge pull request #192

96c7dde Add QR code display on the receive page (moneromooo.monero)
This commit is contained in:
Riccardo Spagni 2016-11-24 12:37:53 +02:00
commit cb3285e553
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
12 changed files with 1627 additions and 2 deletions

View File

@ -37,6 +37,7 @@
#include "oshelper.h" #include "oshelper.h"
#include "WalletManager.h" #include "WalletManager.h"
#include "Wallet.h" #include "Wallet.h"
#include "QRCodeImageProvider.h"
#include "PendingTransaction.h" #include "PendingTransaction.h"
#include "TranslationManager.h" #include "TranslationManager.h"
#include "TransactionInfo.h" #include "TransactionInfo.h"
@ -102,6 +103,7 @@ int main(int argc, char *argv[])
engine.rootContext()->setContextProperty("translationManager", TranslationManager::instance()); engine.rootContext()->setContextProperty("translationManager", TranslationManager::instance());
engine.addImageProvider(QLatin1String("qrcode"), new QRCodeImageProvider());
// export to QML monero accounts root directory // export to QML monero accounts root directory
// wizard is talking about where // wizard is talking about where

View File

@ -11,6 +11,7 @@ QMAKE_DISTCLEAN += -r $$WALLET_ROOT
INCLUDEPATH += $$WALLET_ROOT/include \ INCLUDEPATH += $$WALLET_ROOT/include \
$$PWD/src/libwalletqt \ $$PWD/src/libwalletqt \
$$PWD/src/QR-Code-generator \
$$PWD/src $$PWD/src
HEADERS += \ HEADERS += \
@ -22,10 +23,14 @@ HEADERS += \
src/libwalletqt/PendingTransaction.h \ src/libwalletqt/PendingTransaction.h \
src/libwalletqt/TransactionHistory.h \ src/libwalletqt/TransactionHistory.h \
src/libwalletqt/TransactionInfo.h \ src/libwalletqt/TransactionInfo.h \
src/libwalletqt/QRCodeImageProvider.h \
oshelper.h \ oshelper.h \
TranslationManager.h \ TranslationManager.h \
src/model/TransactionHistoryModel.h \ src/model/TransactionHistoryModel.h \
src/model/TransactionHistorySortFilterModel.h src/model/TransactionHistorySortFilterModel.h \
src/QR-Code-generator/BitBuffer.hpp \
src/QR-Code-generator/QrCode.hpp \
src/QR-Code-generator/QrSegment.hpp
SOURCES += main.cpp \ SOURCES += main.cpp \
@ -37,10 +42,14 @@ SOURCES += main.cpp \
src/libwalletqt/PendingTransaction.cpp \ src/libwalletqt/PendingTransaction.cpp \
src/libwalletqt/TransactionHistory.cpp \ src/libwalletqt/TransactionHistory.cpp \
src/libwalletqt/TransactionInfo.cpp \ src/libwalletqt/TransactionInfo.cpp \
src/libwalletqt/QRCodeImageProvider.cpp \
oshelper.cpp \ oshelper.cpp \
TranslationManager.cpp \ TranslationManager.cpp \
src/model/TransactionHistoryModel.cpp \ src/model/TransactionHistoryModel.cpp \
src/model/TransactionHistorySortFilterModel.cpp src/model/TransactionHistorySortFilterModel.cpp \
src/QR-Code-generator/BitBuffer.cpp \
src/QR-Code-generator/QrCode.cpp \
src/QR-Code-generator/QrSegment.cpp
lupdate_only { lupdate_only {
SOURCES = *.qml \ SOURCES = *.qml \

View File

@ -56,6 +56,23 @@ Rectangle {
integratedAddressLine.text = qsTr("Invalid payment ID") integratedAddressLine.text = qsTr("Invalid payment ID")
} }
function makeQRCodeString() {
var s = "monero:"
var nfields = 0
s += addressLine.text
var amount = amountLine.text.trim()
if (amount !== "") {
s += (nfields++ ? "&" : "?")
s += "tx_amount=" + amount
}
var pid = paymentIdLine.text.trim()
if (pid !== "") {
s += (nfields++ ? "&" : "?")
s += "tx_payment_id=" + pid
}
return s
}
Clipboard { id: clipboard } Clipboard { id: clipboard }
@ -71,6 +88,7 @@ Rectangle {
property int labelWidth: 120 property int labelWidth: 120
property int editWidth: 400 property int editWidth: 400
property int lineEditFontSize: 12 property int lineEditFontSize: 12
property int qrCodeSize: 240
RowLayout { RowLayout {
@ -183,6 +201,43 @@ Rectangle {
} }
} }
RowLayout {
id: amountRow
Label {
id: amountLabel
fontSize: 14
text: qsTr("Amount") + translationManager.emptyString
width: mainLayout.labelWidth
}
LineEdit {
id: amountLine
fontSize: mainLayout.lineEditFontSize
placeholderText: qsTr("Amount") + translationManager.emptyString
readOnly: false
width: mainLayout.editWidth
Layout.fillWidth: true
validator: DoubleValidator {
bottom: 0.0
top: 18446744.073709551615
decimals: 12
notation: DoubleValidator.StandardNotation
locale: "C"
}
}
}
Image {
id: qrCode
anchors.margins: 50
anchors.top: amountRow.bottom
Layout.fillWidth: true
Layout.minimumHeight: mainLayout.qrCodeSize
smooth: false
fillMode: Image.PreserveAspectFit
source: "image://qrcode/" + makeQRCodeString()
}
} }
function onPageCompleted() { function onPageCompleted() {

View File

@ -0,0 +1,63 @@
/*
* QR Code generator library (C++)
*
* Copyright (c) 2016 Project Nayuki
* https://www.nayuki.io/page/qr-code-generator-library
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#include <cstddef>
#include "BitBuffer.hpp"
qrcodegen::BitBuffer::BitBuffer() :
data(),
bitLength(0) {}
int qrcodegen::BitBuffer::getBitLength() const {
return bitLength;
}
std::vector<uint8_t> qrcodegen::BitBuffer::getBytes() const {
return data;
}
void qrcodegen::BitBuffer::appendBits(uint32_t val, int len) {
if (len < 0 || len > 32 || (len < 32 && (val >> len) != 0))
throw "Value out of range";
size_t newBitLen = bitLength + len;
while (data.size() * 8 < newBitLen)
data.push_back(0);
for (int i = len - 1; i >= 0; i--, bitLength++) // Append bit by bit
data.at(bitLength >> 3) |= ((val >> i) & 1) << (7 - (bitLength & 7));
}
void qrcodegen::BitBuffer::appendData(const QrSegment &seg) {
size_t newBitLen = bitLength + seg.bitLength;
while (data.size() * 8 < newBitLen)
data.push_back(0);
for (int i = 0; i < seg.bitLength; i++, bitLength++) { // Append bit by bit
int bit = (seg.data.at(i >> 3) >> (7 - (i & 7))) & 1;
data.at(bitLength >> 3) |= bit << (7 - (bitLength & 7));
}
}

View File

@ -0,0 +1,76 @@
/*
* QR Code generator library (C++)
*
* Copyright (c) 2016 Project Nayuki
* https://www.nayuki.io/page/qr-code-generator-library
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#pragma once
#include <cstdint>
#include <vector>
#include "QrSegment.hpp"
namespace qrcodegen {
/*
* An appendable sequence of bits. Bits are packed in big endian within a byte.
*/
class BitBuffer final {
/*---- Fields ----*/
private:
std::vector<uint8_t> data;
int bitLength;
/*---- Constructor ----*/
public:
// Creates an empty bit buffer (length 0).
BitBuffer();
/*---- Methods ----*/
public:
// Returns the number of bits in the buffer, which is a non-negative value.
int getBitLength() const;
// Returns a copy of all bytes, padding up to the nearest byte.
std::vector<uint8_t> getBytes() const;
// Appends the given number of bits of the given value to this sequence.
// If 0 <= len <= 31, then this requires 0 <= val < 2^len.
void appendBits(uint32_t val, int len);
// Appends the data of the given segment to this bit buffer.
void appendData(const QrSegment &seg);
};
}

View File

@ -0,0 +1,616 @@
/*
* QR Code generator library (C++)
*
* Copyright (c) 2016 Project Nayuki
* https://www.nayuki.io/page/qr-code-generator-library
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstddef>
#include <sstream>
#include "BitBuffer.hpp"
#include "QrCode.hpp"
qrcodegen::QrCode::Ecc::Ecc(int ord, int fb) :
ordinal(ord),
formatBits(fb) {}
const qrcodegen::QrCode::Ecc qrcodegen::QrCode::Ecc::LOW (0, 1);
const qrcodegen::QrCode::Ecc qrcodegen::QrCode::Ecc::MEDIUM (1, 0);
const qrcodegen::QrCode::Ecc qrcodegen::QrCode::Ecc::QUARTILE(2, 3);
const qrcodegen::QrCode::Ecc qrcodegen::QrCode::Ecc::HIGH (3, 2);
qrcodegen::QrCode qrcodegen::QrCode::encodeText(const char *text, const Ecc &ecl) {
std::vector<QrSegment> segs(QrSegment::makeSegments(text));
return encodeSegments(segs, ecl);
}
qrcodegen::QrCode qrcodegen::QrCode::encodeBinary(const std::vector<uint8_t> &data, const Ecc &ecl) {
std::vector<QrSegment> segs;
segs.push_back(QrSegment::makeBytes(data));
return encodeSegments(segs, ecl);
}
qrcodegen::QrCode qrcodegen::QrCode::encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl,
int minVersion, int maxVersion, int mask, bool boostEcl) {
if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7)
throw "Invalid value";
// Find the minimal version number to use
int version, dataUsedBits;
for (version = minVersion; ; version++) {
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
dataUsedBits = QrSegment::getTotalBits(segs, version);
if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
break; // This version number is found to be suitable
if (version >= maxVersion) // All versions in the range could not fit the given data
throw "Data too long";
}
if (dataUsedBits == -1)
throw "Assertion error";
// Increase the error correction level while the data still fits in the current version number
const Ecc *newEcl = &ecl;
if (boostEcl) {
if (dataUsedBits <= getNumDataCodewords(version, Ecc::MEDIUM ) * 8) newEcl = &Ecc::MEDIUM ;
if (dataUsedBits <= getNumDataCodewords(version, Ecc::QUARTILE) * 8) newEcl = &Ecc::QUARTILE;
if (dataUsedBits <= getNumDataCodewords(version, Ecc::HIGH ) * 8) newEcl = &Ecc::HIGH ;
}
// Create the data bit string by concatenating all segments
int dataCapacityBits = getNumDataCodewords(version, *newEcl) * 8;
BitBuffer bb;
for (size_t i = 0; i < segs.size(); i++) {
const QrSegment &seg(segs.at(i));
bb.appendBits(seg.mode.modeBits, 4);
bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));
bb.appendData(seg);
}
// Add terminator and pad up to a byte if applicable
bb.appendBits(0, std::min(4, dataCapacityBits - bb.getBitLength()));
bb.appendBits(0, (8 - bb.getBitLength() % 8) % 8);
// Pad with alternate bytes until data capacity is reached
for (uint8_t padByte = 0xEC; bb.getBitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
bb.appendBits(padByte, 8);
if (bb.getBitLength() % 8 != 0)
throw "Assertion error";
// Create the QR Code symbol
return QrCode(version, *newEcl, bb.getBytes(), mask);
}
qrcodegen::QrCode::QrCode(int ver, const Ecc &ecl, const std::vector<uint8_t> &dataCodewords, int mask) :
// Initialize scalar fields
version(ver),
size(1 <= ver && ver <= 40 ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior
errorCorrectionLevel(ecl) {
// Check arguments
if (ver < 1 || ver > 40 || mask < -1 || mask > 7)
throw "Value out of range";
std::vector<bool> row(size);
for (int i = 0; i < size; i++) {
modules.push_back(row);
isFunction.push_back(row);
}
// Draw function patterns, draw all codewords, do masking
drawFunctionPatterns();
const std::vector<uint8_t> allCodewords(appendErrorCorrection(dataCodewords));
drawCodewords(allCodewords);
this->mask = handleConstructorMasking(mask);
}
qrcodegen::QrCode::QrCode(const QrCode &qr, int mask) :
// Copy scalar fields
version(qr.version),
size(qr.size),
errorCorrectionLevel(qr.errorCorrectionLevel) {
// Check arguments
if (mask < -1 || mask > 7)
throw "Mask value out of range";
// Handle grid fields
modules = qr.modules;
isFunction = qr.isFunction;
// Handle masking
applyMask(qr.mask); // Undo old mask
this->mask = handleConstructorMasking(mask);
}
int qrcodegen::QrCode::getMask() const {
return mask;
}
int qrcodegen::QrCode::getModule(int x, int y) const {
if (0 <= x && x < size && 0 <= y && y < size)
return modules.at(y).at(x) ? 1 : 0;
else
return 0; // Infinite white border
}
std::string qrcodegen::QrCode::toSvgString(int border) const {
if (border < 0)
throw "Border must be non-negative";
std::ostringstream sb;
sb << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
sb << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
sb << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ";
sb << (size + border * 2) << " " << (size + border * 2) << "\">\n";
sb << "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\" stroke-width=\"0\"/>\n";
sb << "\t<path d=\"";
bool head = true;
for (int y = -border; y < size + border; y++) {
for (int x = -border; x < size + border; x++) {
if (getModule(x, y) == 1) {
if (head)
head = false;
else
sb << " ";
sb << "M" << (x + border) << "," << (y + border) << "h1v1h-1z";
}
}
}
sb << "\" fill=\"#000000\" stroke-width=\"0\"/>\n";
sb << "</svg>\n";
return sb.str();
}
void qrcodegen::QrCode::drawFunctionPatterns() {
// Draw the horizontal and vertical timing patterns
for (int i = 0; i < size; i++) {
setFunctionModule(6, i, i % 2 == 0);
setFunctionModule(i, 6, i % 2 == 0);
}
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
drawFinderPattern(3, 3);
drawFinderPattern(size - 4, 3);
drawFinderPattern(3, size - 4);
// Draw the numerous alignment patterns
const std::vector<int> alignPatPos(getAlignmentPatternPositions(version));
int numAlign = alignPatPos.size();
for (int i = 0; i < numAlign; i++) {
for (int j = 0; j < numAlign; j++) {
if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))
continue; // Skip the three finder corners
else
drawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j));
}
}
// Draw configuration data
drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
drawVersion();
}
void qrcodegen::QrCode::drawFormatBits(int mask) {
// Calculate error correction code and pack bits
int data = errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3
int rem = data;
for (int i = 0; i < 10; i++)
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
data = data << 10 | rem;
data ^= 0x5412; // uint15
if (data >> 15 != 0)
throw "Assertion error";
// Draw first copy
for (int i = 0; i <= 5; i++)
setFunctionModule(8, i, ((data >> i) & 1) != 0);
setFunctionModule(8, 7, ((data >> 6) & 1) != 0);
setFunctionModule(8, 8, ((data >> 7) & 1) != 0);
setFunctionModule(7, 8, ((data >> 8) & 1) != 0);
for (int i = 9; i < 15; i++)
setFunctionModule(14 - i, 8, ((data >> i) & 1) != 0);
// Draw second copy
for (int i = 0; i <= 7; i++)
setFunctionModule(size - 1 - i, 8, ((data >> i) & 1) != 0);
for (int i = 8; i < 15; i++)
setFunctionModule(8, size - 15 + i, ((data >> i) & 1) != 0);
setFunctionModule(8, size - 8, true);
}
void qrcodegen::QrCode::drawVersion() {
if (version < 7)
return;
// Calculate error correction code and pack bits
int rem = version; // version is uint6, in the range [7, 40]
for (int i = 0; i < 12; i++)
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
int data = version << 12 | rem; // uint18
if (data >> 18 != 0)
throw "Assertion error";
// Draw two copies
for (int i = 0; i < 18; i++) {
bool bit = ((data >> i) & 1) != 0;
int a = size - 11 + i % 3, b = i / 3;
setFunctionModule(a, b, bit);
setFunctionModule(b, a, bit);
}
}
void qrcodegen::QrCode::drawFinderPattern(int x, int y) {
for (int i = -4; i <= 4; i++) {
for (int j = -4; j <= 4; j++) {
int dist = std::max(std::abs(i), std::abs(j)); // Chebyshev/infinity norm
int xx = x + j, yy = y + i;
if (0 <= xx && xx < size && 0 <= yy && yy < size)
setFunctionModule(xx, yy, dist != 2 && dist != 4);
}
}
}
void qrcodegen::QrCode::drawAlignmentPattern(int x, int y) {
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++)
setFunctionModule(x + j, y + i, std::max(std::abs(i), std::abs(j)) != 1);
}
}
void qrcodegen::QrCode::setFunctionModule(int x, int y, bool isBlack) {
modules.at(y).at(x) = isBlack;
isFunction.at(y).at(x) = true;
}
std::vector<uint8_t> qrcodegen::QrCode::appendErrorCorrection(const std::vector<uint8_t> &data) const {
if (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))
throw "Invalid argument";
// Calculate parameter numbers
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal][version];
int totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[errorCorrectionLevel.ordinal][version];
if (totalEcc % numBlocks != 0)
throw "Assertion error";
int blockEccLen = totalEcc / numBlocks;
int numShortBlocks = numBlocks - getNumRawDataModules(version) / 8 % numBlocks;
int shortBlockLen = getNumRawDataModules(version) / 8 / numBlocks;
// Split data into blocks and append ECC to each block
std::vector<std::vector<uint8_t>> blocks;
const ReedSolomonGenerator rs(blockEccLen);
for (int i = 0, k = 0; i < numBlocks; i++) {
std::vector<uint8_t> dat;
dat.insert(dat.begin(), data.begin() + k, data.begin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));
k += dat.size();
const std::vector<uint8_t> ecc(rs.getRemainder(dat));
if (i < numShortBlocks)
dat.push_back(0);
dat.insert(dat.end(), ecc.begin(), ecc.end());
blocks.push_back(dat);
}
// Interleave (not concatenate) the bytes from every block into a single sequence
std::vector<uint8_t> result;
for (int i = 0; static_cast<unsigned int>(i) < blocks.at(0).size(); i++) {
for (int j = 0; static_cast<unsigned int>(j) < blocks.size(); j++) {
// Skip the padding byte in short blocks
if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
result.push_back(blocks.at(j).at(i));
}
}
if (result.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))
throw "Assertion error";
return result;
}
void qrcodegen::QrCode::drawCodewords(const std::vector<uint8_t> &data) {
if (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))
throw "Invalid argument";
size_t i = 0; // Bit index into the data
// Do the funny zigzag scan
for (int right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair
if (right == 6)
right = 5;
for (int vert = 0; vert < size; vert++) { // Vertical counter
for (int j = 0; j < 2; j++) {
int x = right - j; // Actual x coordinate
bool upwards = ((right & 2) == 0) ^ (x < 6);
int y = upwards ? size - 1 - vert : vert; // Actual y coordinate
if (!isFunction.at(y).at(x) && i < data.size() * 8) {
modules.at(y).at(x) = ((data.at(i >> 3) >> (7 - (i & 7))) & 1) != 0;
i++;
}
// If there are any remainder bits (0 to 7), they are already
// set to 0/false/white when the grid of modules was initialized
}
}
}
if (static_cast<unsigned int>(i) != data.size() * 8)
throw "Assertion error";
}
void qrcodegen::QrCode::applyMask(int mask) {
if (mask < 0 || mask > 7)
throw "Mask value out of range";
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
bool invert;
switch (mask) {
case 0: invert = (x + y) % 2 == 0; break;
case 1: invert = y % 2 == 0; break;
case 2: invert = x % 3 == 0; break;
case 3: invert = (x + y) % 3 == 0; break;
case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
case 5: invert = x * y % 2 + x * y % 3 == 0; break;
case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
default: throw "Assertion error";
}
modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));
}
}
}
int qrcodegen::QrCode::handleConstructorMasking(int mask) {
if (mask == -1) { // Automatically choose best mask
int32_t minPenalty = INT32_MAX;
for (int i = 0; i < 8; i++) {
drawFormatBits(i);
applyMask(i);
int penalty = getPenaltyScore();
if (penalty < minPenalty) {
mask = i;
minPenalty = penalty;
}
applyMask(i); // Undoes the mask due to XOR
}
}
if (mask < 0 || mask > 7)
throw "Assertion error";
drawFormatBits(mask); // Overwrite old format bits
applyMask(mask); // Apply the final choice of mask
return mask; // The caller shall assign this value to the final-declared field
}
int qrcodegen::QrCode::getPenaltyScore() const {
int result = 0;
// Adjacent modules in row having same color
for (int y = 0; y < size; y++) {
bool colorX = modules.at(y).at(0);
for (int x = 1, runX = 1; x < size; x++) {
if (modules.at(y).at(x) != colorX) {
colorX = modules.at(y).at(x);
runX = 1;
} else {
runX++;
if (runX == 5)
result += PENALTY_N1;
else if (runX > 5)
result++;
}
}
}
// Adjacent modules in column having same color
for (int x = 0; x < size; x++) {
bool colorY = modules.at(0).at(x);
for (int y = 1, runY = 1; y < size; y++) {
if (modules.at(y).at(x) != colorY) {
colorY = modules.at(y).at(x);
runY = 1;
} else {
runY++;
if (runY == 5)
result += PENALTY_N1;
else if (runY > 5)
result++;
}
}
}
// 2*2 blocks of modules having same color
for (int y = 0; y < size - 1; y++) {
for (int x = 0; x < size - 1; x++) {
bool color = modules.at(y).at(x);
if ( color == modules.at(y).at(x + 1) &&
color == modules.at(y + 1).at(x) &&
color == modules.at(y + 1).at(x + 1))
result += PENALTY_N2;
}
}
// Finder-like pattern in rows
for (int y = 0; y < size; y++) {
for (int x = 0, bits = 0; x < size; x++) {
bits = ((bits << 1) & 0x7FF) | (modules.at(y).at(x) ? 1 : 0);
if (x >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
result += PENALTY_N3;
}
}
// Finder-like pattern in columns
for (int x = 0; x < size; x++) {
for (int y = 0, bits = 0; y < size; y++) {
bits = ((bits << 1) & 0x7FF) | (modules.at(y).at(x) ? 1 : 0);
if (y >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
result += PENALTY_N3;
}
}
// Balance of black and white modules
int black = 0;
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (modules.at(y).at(x))
black++;
}
}
int total = size * size;
// Find smallest k such that (45-5k)% <= dark/total <= (55+5k)%
for (int k = 0; black*20 < (9-k)*total || black*20 > (11+k)*total; k++)
result += PENALTY_N4;
return result;
}
std::vector<int> qrcodegen::QrCode::getAlignmentPatternPositions(int ver) {
if (ver < 1 || ver > 40)
throw "Version number out of range";
else if (ver == 1)
return std::vector<int>();
else {
int numAlign = ver / 7 + 2;
int step;
if (ver != 32)
step = (ver * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2; // ceil((size - 13) / (2*numAlign - 2)) * 2
else // C-C-C-Combo breaker!
step = 26;
std::vector<int> result;
int size = ver * 4 + 17;
for (int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)
result.insert(result.begin(), pos);
result.insert(result.begin(), 6);
return result;
}
}
int qrcodegen::QrCode::getNumRawDataModules(int ver) {
if (ver < 1 || ver > 40)
throw "Version number out of range";
int result = (16 * ver + 128) * ver + 64;
if (ver >= 2) {
int numAlign = ver / 7 + 2;
result -= (25 * numAlign - 10) * numAlign - 55;
if (ver >= 7)
result -= 18 * 2; // Subtract version information
}
return result;
}
int qrcodegen::QrCode::getNumDataCodewords(int ver, const Ecc &ecl) {
if (ver < 1 || ver > 40)
throw "Version number out of range";
return getNumRawDataModules(ver) / 8 - NUM_ERROR_CORRECTION_CODEWORDS[ecl.ordinal][ver];
}
/*---- Tables of constants ----*/
const int qrcodegen::QrCode::PENALTY_N1 = 3;
const int qrcodegen::QrCode::PENALTY_N2 = 3;
const int qrcodegen::QrCode::PENALTY_N3 = 40;
const int qrcodegen::QrCode::PENALTY_N4 = 10;
const int16_t qrcodegen::QrCode::NUM_ERROR_CORRECTION_CODEWORDS[4][41] = {
// Version: (note that index 0 is for padding, and is set to an illegal value)
//0, 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 Error correction level
{-1, 7, 10, 15, 20, 26, 36, 40, 48, 60, 72, 80, 96, 104, 120, 132, 144, 168, 180, 196, 224, 224, 252, 270, 300, 312, 336, 360, 390, 420, 450, 480, 510, 540, 570, 570, 600, 630, 660, 720, 750}, // Low
{-1, 10, 16, 26, 36, 48, 64, 72, 88, 110, 130, 150, 176, 198, 216, 240, 280, 308, 338, 364, 416, 442, 476, 504, 560, 588, 644, 700, 728, 784, 812, 868, 924, 980, 1036, 1064, 1120, 1204, 1260, 1316, 1372}, // Medium
{-1, 13, 22, 36, 52, 72, 96, 108, 132, 160, 192, 224, 260, 288, 320, 360, 408, 448, 504, 546, 600, 644, 690, 750, 810, 870, 952, 1020, 1050, 1140, 1200, 1290, 1350, 1440, 1530, 1590, 1680, 1770, 1860, 1950, 2040}, // Quartile
{-1, 17, 28, 44, 64, 88, 112, 130, 156, 192, 224, 264, 308, 352, 384, 432, 480, 532, 588, 650, 700, 750, 816, 900, 960, 1050, 1110, 1200, 1260, 1350, 1440, 1530, 1620, 1710, 1800, 1890, 1980, 2100, 2220, 2310, 2430}, // High
};
const int8_t qrcodegen::QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
// Version: (note that index 0 is for padding, and is set to an illegal value)
//0, 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 Error correction level
{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low
{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium
{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile
{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High
};
qrcodegen::QrCode::ReedSolomonGenerator::ReedSolomonGenerator(int degree) :
coefficients() {
if (degree < 1 || degree > 255)
throw "Degree out of range";
// Start with the monomial x^0
coefficients.resize(degree);
coefficients.at(degree - 1) = 1;
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
// drop the highest term, and store the rest of the coefficients in order of descending powers.
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
int root = 1;
for (int i = 0; i < degree; i++) {
// Multiply the current product by (x - r^i)
for (size_t j = 0; j < coefficients.size(); j++) {
coefficients.at(j) = multiply(coefficients.at(j), static_cast<uint8_t>(root));
if (j + 1 < coefficients.size())
coefficients.at(j) ^= coefficients.at(j + 1);
}
root = (root << 1) ^ ((root >> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D)
}
}
std::vector<uint8_t> qrcodegen::QrCode::ReedSolomonGenerator::getRemainder(const std::vector<uint8_t> &data) const {
// Compute the remainder by performing polynomial division
std::vector<uint8_t> result(coefficients.size());
for (size_t i = 0; i < data.size(); i++) {
uint8_t factor = data.at(i) ^ result.at(0);
result.erase(result.begin());
result.push_back(0);
for (size_t j = 0; j < result.size(); j++)
result.at(j) ^= multiply(coefficients.at(j), factor);
}
return result;
}
uint8_t qrcodegen::QrCode::ReedSolomonGenerator::multiply(uint8_t x, uint8_t y) {
// Russian peasant multiplication
int z = 0;
for (int i = 7; i >= 0; i--) {
z = (z << 1) ^ ((z >> 7) * 0x11D);
z ^= ((y >> i) & 1) * x;
}
if (z >> 8 != 0)
throw "Assertion error";
return static_cast<uint8_t>(z);
}

View File

@ -0,0 +1,314 @@
/*
* QR Code generator library (C++)
*
* Copyright (c) 2016 Project Nayuki
* https://www.nayuki.io/page/qr-code-generator-library
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include "QrSegment.hpp"
namespace qrcodegen {
/*
* Represents an immutable square grid of black and white cells for a QR Code symbol, and
* provides static functions to create a QR Code from user-supplied textual or binary data.
* This class covers the QR Code model 2 specification, supporting all versions (sizes)
* from 1 to 40, all 4 error correction levels, and only 3 character encoding modes.
*/
class QrCode final {
/*---- Public helper enumeration ----*/
public:
/*
* Represents the error correction level used in a QR Code symbol.
*/
class Ecc final {
// Constants declared in ascending order of error protection.
public:
const static Ecc LOW, MEDIUM, QUARTILE, HIGH;
// Fields.
public:
const int ordinal; // (Public) In the range 0 to 3 (unsigned 2-bit integer).
const int formatBits; // (Package-private) In the range 0 to 3 (unsigned 2-bit integer).
// Constructor.
private:
Ecc(int ord, int fb);
};
/*---- Public static factory functions ----*/
public:
/*
* Returns a QR Code symbol representing the given Unicode text string at the given error correction level.
* As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer Unicode
* code points (not UTF-16 code units). The smallest possible QR Code version is automatically chosen for the output.
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
*/
static QrCode encodeText(const char *text, const Ecc &ecl);
/*
* Returns a QR Code symbol representing the given binary data string at the given error correction level.
* This function always encodes using the binary segment mode, not any text mode. The maximum number of
* bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
*/
static QrCode encodeBinary(const std::vector<uint8_t> &data, const Ecc &ecl);
/*
* Returns a QR Code symbol representing the specified data segments with the specified encoding parameters.
* The smallest possible QR Code version within the specified range is automatically chosen for the output.
* This function allows the user to create a custom sequence of segments that switches
* between modes (such as alphanumeric and binary) to encode text more efficiently.
* This function is considered to be lower level than simply encoding text or binary data.
*/
static QrCode encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl,
int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true); // All optional parameters
/*---- Instance fields ----*/
// Public immutable scalar parameters
public:
/* This QR Code symbol's version number, which is always between 1 and 40 (inclusive). */
const int version;
/* The width and height of this QR Code symbol, measured in modules.
* Always equal to version &times; 4 + 17, in the range 21 to 177. */
const int size;
/* The error correction level used in this QR Code symbol. */
const Ecc &errorCorrectionLevel;
/* The mask pattern used in this QR Code symbol, in the range 0 to 7 (i.e. unsigned 3-bit integer).
* Note that even if a constructor was called with automatic masking requested
* (mask = -1), the resulting object will still have a mask value between 0 and 7. */
private:
int mask;
// Private grids of modules/pixels (conceptually immutable)
private:
std::vector<std::vector<bool>> modules; // The modules of this QR Code symbol (false = white, true = black)
std::vector<std::vector<bool>> isFunction; // Indicates function modules that are not subjected to masking
/*---- Constructors ----*/
public:
/*
* Creates a new QR Code symbol with the given version number, error correction level, binary data array,
* and mask number. This is a cumbersome low-level constructor that should not be invoked directly by the user.
* To go one level up, see the encodeSegments() function.
*/
QrCode(int ver, const Ecc &ecl, const std::vector<uint8_t> &dataCodewords, int mask);
/*
* Creates a new QR Code symbol based on the given existing object, but with a potentially
* different mask pattern. The version, error correction level, codewords, etc. of the newly
* created object are all identical to the argument object; only the mask may differ.
*/
QrCode(const QrCode &qr, int mask);
/*---- Public instance methods ----*/
public:
int getMask() const;
/*
* Returns the color of the module (pixel) at the given coordinates, which is either 0 for white or 1 for black. The top
* left corner has the coordinates (x=0, y=0). If the given coordinates are out of bounds, then 0 (white) is returned.
*/
int getModule(int x, int y) const;
/*
* Based on the given number of border modules to add as padding, this returns a
* string whose contents represents an SVG XML file that depicts this QR Code symbol.
* Note that Unix newlines (\n) are always used, regardless of the platform.
*/
std::string toSvgString(int border) const;
/*---- Private helper methods for constructor: Drawing function modules ----*/
private:
void drawFunctionPatterns();
// Draws two copies of the format bits (with its own error correction code)
// based on the given mask and this object's error correction level field.
void drawFormatBits(int mask);
// Draws two copies of the version bits (with its own error correction code),
// based on this object's version field (which only has an effect for 7 <= version <= 40).
void drawVersion();
// Draws a 9*9 finder pattern including the border separator, with the center module at (x, y).
void drawFinderPattern(int x, int y);
// Draws a 5*5 alignment pattern, with the center module at (x, y).
void drawAlignmentPattern(int x, int y);
// Sets the color of a module and marks it as a function module.
// Only used by the constructor. Coordinates must be in range.
void setFunctionModule(int x, int y, bool isBlack);
/*---- Private helper methods for constructor: Codewords and masking ----*/
private:
// Returns a new byte string representing the given data with the appropriate error correction
// codewords appended to it, based on this object's version and error correction level.
std::vector<uint8_t> appendErrorCorrection(const std::vector<uint8_t> &data) const;
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
// data area of this QR Code symbol. Function modules need to be marked off before this is called.
void drawCodewords(const std::vector<uint8_t> &data);
// XORs the data modules in this QR Code with the given mask pattern. Due to XOR's mathematical
// properties, calling applyMask(m) twice with the same value is equivalent to no change at all.
// This means it is possible to apply a mask, undo it, and try another mask. Note that a final
// well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.).
void applyMask(int mask);
// A messy helper function for the constructors. This QR Code must be in an unmasked state when this
// method is called. The given argument is the requested mask, which is -1 for auto or 0 to 7 for fixed.
// This method applies and returns the actual mask chosen, from 0 to 7.
int handleConstructorMasking(int mask);
// Calculates and returns the penalty score based on state of this QR Code's current modules.
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
int getPenaltyScore() const;
/*---- Private static helper functions ----*/
private:
// Returns a set of positions of the alignment patterns in ascending order. These positions are
// used on both the x and y axes. Each value in the resulting array is in the range [0, 177).
// This stateless pure function could be implemented as table of 40 variable-length lists of unsigned bytes.
static std::vector<int> getAlignmentPatternPositions(int ver);
// Returns the number of raw data modules (bits) available at the given version number.
// These data modules are used for both user data codewords and error correction codewords.
// This stateless pure function could be implemented as a 40-entry lookup table.
static int getNumRawDataModules(int ver);
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
// QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
static int getNumDataCodewords(int ver, const Ecc &ecl);
/*---- Private tables of constants ----*/
private:
// For use in getPenaltyScore(), when evaluating which mask is best.
static const int PENALTY_N1;
static const int PENALTY_N2;
static const int PENALTY_N3;
static const int PENALTY_N4;
static const int16_t NUM_ERROR_CORRECTION_CODEWORDS[4][41];
static const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];
/*---- Private helper class ----*/
private:
/*
* Computes the Reed-Solomon error correction codewords for a sequence of data codewords
* at a given degree. Objects are immutable, and the state only depends on the degree.
* This class exists because the divisor polynomial does not need to be recalculated for every input.
*/
class ReedSolomonGenerator final {
/*-- Immutable field --*/
private:
// Coefficients of the divisor polynomial, stored from highest to lowest power, excluding the leading term which
// is always 1. For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
std::vector<uint8_t> coefficients;
/*-- Constructor --*/
public:
/*
* Creates a Reed-Solomon ECC generator for the given degree. This could be implemented
* as a lookup table over all possible parameter values, instead of as an algorithm.
*/
ReedSolomonGenerator(int degree);
/*-- Method --*/
public:
/*
* Computes and returns the Reed-Solomon error correction codewords for the given sequence of data codewords.
* The returned object is always a new byte array. This method does not alter this object's state (because it is immutable).
*/
std::vector<uint8_t> getRemainder(const std::vector<uint8_t> &data) const;
/*-- Static function --*/
private:
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
static uint8_t multiply(uint8_t x, uint8_t y);
};
};
}

View File

@ -0,0 +1,173 @@
/*
* QR Code generator library (C++)
*
* Copyright (c) 2016 Project Nayuki
* https://www.nayuki.io/page/qr-code-generator-library
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#include <cstddef>
#include "BitBuffer.hpp"
#include "QrSegment.hpp"
qrcodegen::QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
modeBits(mode) {
numBitsCharCount[0] = cc0;
numBitsCharCount[1] = cc1;
numBitsCharCount[2] = cc2;
}
int qrcodegen::QrSegment::Mode::numCharCountBits(int ver) const {
if ( 1 <= ver && ver <= 9) return numBitsCharCount[0];
else if (10 <= ver && ver <= 26) return numBitsCharCount[1];
else if (27 <= ver && ver <= 40) return numBitsCharCount[2];
else throw "Version number out of range";
}
const qrcodegen::QrSegment::Mode qrcodegen::QrSegment::Mode::NUMERIC (0x1, 10, 12, 14);
const qrcodegen::QrSegment::Mode qrcodegen::QrSegment::Mode::ALPHANUMERIC(0x2, 9, 11, 13);
const qrcodegen::QrSegment::Mode qrcodegen::QrSegment::Mode::BYTE (0x4, 8, 16, 16);
const qrcodegen::QrSegment::Mode qrcodegen::QrSegment::Mode::KANJI (0x8, 8, 10, 12);
qrcodegen::QrSegment qrcodegen::QrSegment::makeBytes(const std::vector<uint8_t> &data) {
return QrSegment(Mode::BYTE, data.size(), data, data.size() * 8);
}
qrcodegen::QrSegment qrcodegen::QrSegment::makeNumeric(const char *digits) {
BitBuffer bb;
int accumData = 0;
int accumCount = 0;
int charCount = 0;
for (; *digits != '\0'; digits++, charCount++) {
char c = *digits;
if (c < '0' || c > '9')
throw "String contains non-numeric characters";
accumData = accumData * 10 + (c - '0');
accumCount++;
if (accumCount == 3) {
bb.appendBits(accumData, 10);
accumData = 0;
accumCount = 0;
}
}
if (accumCount > 0) // 1 or 2 digits remaining
bb.appendBits(accumData, accumCount * 3 + 1);
return QrSegment(Mode::NUMERIC, charCount, bb.getBytes(), bb.getBitLength());
}
qrcodegen::QrSegment qrcodegen::QrSegment::makeAlphanumeric(const char *text) {
BitBuffer bb;
int accumData = 0;
int accumCount = 0;
int charCount = 0;
for (; *text != '\0'; text++, charCount++) {
char c = *text;
if (c < ' ' || c > 'Z')
throw "String contains unencodable characters in alphanumeric mode";
accumData = accumData * 45 + ALPHANUMERIC_ENCODING_TABLE[c - ' '];
accumCount++;
if (accumCount == 2) {
bb.appendBits(accumData, 11);
accumData = 0;
accumCount = 0;
}
}
if (accumCount > 0) // 1 character remaining
bb.appendBits(accumData, 6);
return QrSegment(Mode::ALPHANUMERIC, charCount, bb.getBytes(), bb.getBitLength());
}
std::vector<qrcodegen::QrSegment> qrcodegen::QrSegment::makeSegments(const char *text) {
// Select the most efficient segment encoding automatically
std::vector<QrSegment> result;
if (*text == '\0'); // Leave the vector empty
else if (QrSegment::isNumeric(text))
result.push_back(QrSegment::makeNumeric(text));
else if (QrSegment::isAlphanumeric(text))
result.push_back(QrSegment::makeAlphanumeric(text));
else {
std::vector<uint8_t> bytes;
for (; *text != '\0'; text++)
bytes.push_back(static_cast<uint8_t>(*text));
result.push_back(QrSegment::makeBytes(bytes));
}
return result;
}
qrcodegen::QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<uint8_t> &b, int bitLen) :
mode(md),
numChars(numCh),
data(b),
bitLength(bitLen) {
if (numCh < 0 || bitLen < 0 || b.size() != static_cast<unsigned int>((bitLen + 7) / 8))
throw "Invalid value";
}
int qrcodegen::QrSegment::getTotalBits(const std::vector<QrSegment> &segs, int version) {
if (version < 1 || version > 40)
throw "Version number out of range";
int result = 0;
for (size_t i = 0; i < segs.size(); i++) {
const QrSegment &seg(segs.at(i));
int ccbits = seg.mode.numCharCountBits(version);
// Fail if segment length value doesn't fit in the length field's bit-width
if (seg.numChars >= (1 << ccbits))
return -1;
result += 4 + ccbits + seg.bitLength;
}
return result;
}
bool qrcodegen::QrSegment::isAlphanumeric(const char *text) {
for (; *text != '\0'; text++) {
char c = *text;
if (c < ' ' || c > 'Z' || ALPHANUMERIC_ENCODING_TABLE[c - ' '] == -1)
return false;
}
return true;
}
bool qrcodegen::QrSegment::isNumeric(const char *text) {
for (; *text != '\0'; text++) {
char c = *text;
if (c < '0' || c > '9')
return false;
}
return true;
}
const int8_t qrcodegen::QrSegment::ALPHANUMERIC_ENCODING_TABLE[59] = {
// SP, !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, // ASCII codes 32 to 64
36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, -1, // Array indices 0 to 32
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, // Array indices 33 to 58
// A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, // ASCII codes 65 to 90
};

View File

@ -0,0 +1,170 @@
/*
* QR Code generator library (C++)
*
* Copyright (c) 2016 Project Nayuki
* https://www.nayuki.io/page/qr-code-generator-library
*
* (MIT License)
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#pragma once
#include <cstdint>
#include <vector>
namespace qrcodegen {
/*
* Represents a character string to be encoded in a QR Code symbol. Each segment has
* a mode, and a sequence of characters that is already encoded as a sequence of bits.
* Instances of this class are immutable.
* This segment class imposes no length restrictions, but QR Codes have restrictions.
* Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
* Any segment longer than this is meaningless for the purpose of generating QR Codes.
*/
class QrSegment final {
/*---- Public helper enumeration ----*/
/*
* The mode field of a segment. Immutable. Provides methods to retrieve closely related values.
*/
public:
class Mode final {
/*-- Constants --*/
public:
static const Mode NUMERIC;
static const Mode ALPHANUMERIC;
static const Mode BYTE;
static const Mode KANJI;
/*-- Fields --*/
/* (Package-private) An unsigned 4-bit integer value (range 0 to 15) representing the mode indicator bits for this mode object. */
public:
const int modeBits;
private:
int numBitsCharCount[3];
/*-- Constructor --*/
private:
Mode(int mode, int cc0, int cc1, int cc2);
/*-- Method --*/
/*
* (Package-private) Returns the bit width of the segment character count field for this mode object at the given version number.
*/
public:
int numCharCountBits(int ver) const;
};
/*---- Public static factory functions ----*/
public:
/*
* Returns a segment representing the given binary data encoded in byte mode.
*/
static QrSegment makeBytes(const std::vector<uint8_t> &data);
/*
* Returns a segment representing the given string of decimal digits encoded in numeric mode.
*/
static QrSegment makeNumeric(const char *digits);
/*
* Returns a segment representing the given text string encoded in alphanumeric mode. The characters allowed are:
* 0 to 9, A to Z (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
*/
static QrSegment makeAlphanumeric(const char *text);
/*
* Returns a list of zero or more segments to represent the given text string.
* The result may use various segment modes and switch modes to optimize the length of the bit stream.
*/
static std::vector<QrSegment> makeSegments(const char *text);
/*---- Public static helper functions ----*/
public:
/*
* Tests whether the given string can be encoded as a segment in alphanumeric mode.
*/
static bool isAlphanumeric(const char *text);
/*
* Tests whether the given string can be encoded as a segment in numeric mode.
*/
static bool isNumeric(const char *text);
/*---- Instance fields ----*/
public:
/* The mode indicator for this segment. */
const Mode mode;
/* The length of this segment's unencoded data, measured in characters. Always zero or positive. */
const int numChars;
/* The bits of this segment packed into a byte array in big endian. */
const std::vector<uint8_t> data;
/* The length of this segment's encoded data, measured in bits. Satisfies ceil(bitLength / 8) = data.size(). */
const int bitLength;
/*---- Constructor ----*/
public:
/*
* Creates a new QR Code data segment with the given parameters and data.
*/
QrSegment(const Mode &md, int numCh, const std::vector<uint8_t> &b, int bitLen);
// Package-private helper function.
static int getTotalBits(const std::vector<QrSegment> &segs, int version);
/*---- Private constant ----*/
private:
/* Maps shifted ASCII codes to alphanumeric mode character codes. */
static const int8_t ALPHANUMERIC_ENCODING_TABLE[59];
};
}

View File

@ -0,0 +1,119 @@
QR Code generator library
=========================
Introduction
------------
This project aims to provide the best and clearest QR Code generator library. The primary goals are flexible options and absolute correctness. The secondary goals are compact implementation size and good documentation comments.
Home page with live JavaScript demo and extensive description: [https://www.nayuki.io/page/qr-code-generator-library](https://www.nayuki.io/page/qr-code-generator-library)
Features
--------
Core features:
* Available in 4 programming languages, all with nearly equal functionality: Java, JavaScript, Python, C++
* Significantly shorter code but more documentation comments compared to competing libraries
* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard
* Output formats: Raw modules/pixels of the QR symbol (all languages), SVG XML string (all languages), BufferedImage raster bitmap (Java only)
* Encodes numeric and special-alphanumeric text in less space than general text
* Open source code under the permissive MIT License
Manual parameters:
* User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data
* User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one
* User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number
Optional advanced features (Java only):
* Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes
* Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general parts
Examples
--------
Java language:
import io.nayuki.qrcodegen.*;
// Simple operation
QrCode qr0 = QrCode.encodeText("Hello, world!", QrCode.Ecc.MEDIUM);
BufferedImage img = qr0.toImage(4, 10);
ImageIO.write(img, "png", new File("qr-code.png"));
// Manual operation
List<QrSegment> segs = QrSegment.makeSegments("3141592653589793238462643383");
QrCode qr1 = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, 5, 5, 2, false);
JavaScript language:
// Name abbreviated for the sake of these examples here
var QRC = qrcodegen.QrCode;
// Simple operation
var qr0 = QRC.encodeText("Hello, world!", QRC.Ecc.MEDIUM);
var svg = qr0.toSvgString(4);
// Manual operation
var segs = qrcodegen.QrSegment.makeSegments("3141592653589793238462643383");
var qr1 = QRC.encodeSegments(segs, QRC.Ecc.HIGH, 5, 5, 2, false);
Python language:
from qrcodegen import *
# Simple operation
qr0 = QrCode.encode_text("Hello, world!", QrCode.Ecc.MEDIUM)
svg = qr0.to_svg_str(4)
# Manual operation
segs = QrSegment.make_segments("3141592653589793238462643383")
qr1 = QrCode.encode_segments(segs, QrCode.Ecc.HIGH, 5, 5, 2, False)
C++ language:
#include <string>
#include <vector>
#include "QrCode.hpp"
using namespace qrcodegen;
// Simple operation
QrCode qr0 = QrCode::encodeText("Hello, world!", QrCode::Ecc::MEDIUM);
std::string svg = qr0.toSvgString(4);
// Manual operation
std::vector<QrSegment> segs =
QrSegment::makeSegments("3141592653589793238462643383");
QrCode qr1 = QrCode::encodeSegments(segs, QrCode::Ecc::HIGH, 5, 5, 2, false);
License
-------
Copyright © 2016 Project Nayuki
[https://www.nayuki.io/page/qr-code-generator-library](https://www.nayuki.io/page/qr-code-generator-library)
(MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
* The Software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the Software or the use or other dealings in the
Software.

View File

@ -0,0 +1,17 @@
#include "QrCode.hpp"
#include "QRCodeImageProvider.h"
QImage QRCodeImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
using namespace qrcodegen;
QrCode qrcode = QrCode::encodeText(id.toStdString().c_str(), QrCode::Ecc::MEDIUM);
QImage img = QImage(qrcode.size, qrcode.size, QImage::Format_Mono);
for (int y = 0; y < qrcode.size; ++y)
for (int x = 0; x < qrcode.size; ++x)
img.setPixel(x, y, qrcode.getModule(x, y));
if (size)
*size = QSize(qrcode.size, qrcode.size);
return img;
}

View File

@ -0,0 +1,11 @@
#include <QImage>
#include <QQuickImageProvider>
class QRCodeImageProvider: public QQuickImageProvider
{
public:
QRCodeImageProvider(): QQuickImageProvider(QQuickImageProvider::Image) {}
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
};