Format code & update example
This commit is contained in:
+33
-118
@@ -1,123 +1,38 @@
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: LLVM
|
||||
AccessModifierOffset: -2
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveBitFields: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: true
|
||||
AlignTrailingComments: true
|
||||
AllowAllArgumentsOnNextLine: false
|
||||
AllowAllConstructorInitializersOnNextLine: false
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: Empty
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: false
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
BinPackArguments: true
|
||||
BinPackParameters: true
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: false
|
||||
AfterControlStatement: false
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: false
|
||||
SplitEmptyRecord: false
|
||||
SplitEmptyNamespace: false
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
Standard: Cpp11
|
||||
CommentPragmas: "^!|^:"
|
||||
PointerBindsToType: false
|
||||
SpaceAfterTemplateKeyword: false
|
||||
BreakBeforeBinaryOperators: All
|
||||
BreakBeforeBraces: Custom
|
||||
BreakBeforeInheritanceComma: false
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
ColumnLimit: 100
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
BreakConstructorInitializers: BeforeComma
|
||||
ConstructorInitializerIndentWidth: 2
|
||||
ContinuationIndentWidth: 2
|
||||
Cpp11BracedListStyle: true
|
||||
DeriveLineEnding: true
|
||||
DerivePointerAlignment: false
|
||||
DisableFormat: false
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
IncludeBlocks: Preserve
|
||||
IncludeCategories:
|
||||
- Regex: '^<config.h>$'
|
||||
Priority: -1
|
||||
- Regex: '^<.*\.h>$'
|
||||
Priority: 1
|
||||
- Regex: '^<.*>'
|
||||
Priority: 2
|
||||
- Regex: '.*'
|
||||
Priority: 3
|
||||
IncludeIsMainRegex: '(Test)?$'
|
||||
IndentCaseBlocks: true
|
||||
IndentCaseLabels: false
|
||||
IndentGotoLabels: true
|
||||
IndentPPDirectives: AfterHash
|
||||
IndentWidth: 2
|
||||
IndentWrappedFunctionNames: false
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBinPackProtocolList: Auto
|
||||
ObjCBlockIndentWidth: 2
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PenaltyBreakAssignment: 2000
|
||||
PenaltyBreakBeforeFirstCallParameter: 19
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyBreakTemplateDeclaration: 10
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 60
|
||||
PointerAlignment: Right
|
||||
ReflowComments: true
|
||||
SortIncludes: true
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 2
|
||||
SpacesInAngles: false
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
Standard: Latest
|
||||
TabWidth: 2
|
||||
UseTab: Never
|
||||
...
|
||||
AlignAfterOpenBracket: true
|
||||
AlwaysBreakTemplateDeclarations: true
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
SortIncludes: false
|
||||
IndentCaseLabels: true
|
||||
IndentPPDirectives: AfterHash
|
||||
AccessModifierOffset: -2
|
||||
IndentWidth: 2
|
||||
ColumnLimit: 80
|
||||
|
||||
BraceWrapping:
|
||||
AfterClass: true
|
||||
AfterControlStatement: true
|
||||
AfterEnum: true
|
||||
AfterFunction: true
|
||||
AfterNamespace: true
|
||||
AfterObjCDeclaration: true
|
||||
AfterStruct: true
|
||||
AfterUnion: true
|
||||
BeforeCatch: true
|
||||
BeforeElse: true
|
||||
IndentBraces: false
|
||||
|
||||
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH, forever, Q_FOREVER, QBENCHMARK, QBENCHMARK_ONCE ]
|
||||
#StatementMacros ['Q_OBJECT', 'Q_UNUSED']
|
||||
|
||||
|
||||
|
||||
+18
@@ -3,6 +3,19 @@
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
.cache
|
||||
|
||||
# Binary builds
|
||||
udp_function_generator
|
||||
udp_function_generator.exe
|
||||
|
||||
# Build & IDEs
|
||||
*.kdev*
|
||||
build/*
|
||||
example/build/*
|
||||
*.vscode
|
||||
*.layout
|
||||
*.qmlls.ini
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
@@ -28,3 +41,8 @@ Temporary Items
|
||||
|
||||
# Qt project file
|
||||
*.pro.user
|
||||
*.user
|
||||
|
||||
# Clion
|
||||
.idea/*
|
||||
cmake-build-*
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Qt QR Code Generator is a simple C++ class that uses the [qrcodegen](https://github.com/nayuki/QR-Code-generator) library to generate QR codes from QStrings in Qt applications.
|
||||
|
||||
[](example/screenshot.png)
|
||||
[](example/demo.gif)
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
+39
-10
@@ -22,14 +22,19 @@
|
||||
|
||||
#include "MainWindow.h"
|
||||
|
||||
MainWindow::MainWindow() {
|
||||
#include <QFile>
|
||||
#include <QFileDialog>
|
||||
#include <QStandardPaths>
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
// Initialize layout-related widgets
|
||||
m_centralWidget = new QWidget(this);
|
||||
m_header = new QWidget(m_centralWidget);
|
||||
|
||||
// Initialize horizontal layout widgets
|
||||
m_label = new QLabel(tr("Message") + ": ", m_header);
|
||||
m_pushButton = new QPushButton(tr("Generate QR Code"), m_header);
|
||||
m_saveButton = new QPushButton(tr("Export QR Code"), m_header);
|
||||
m_lineEdit = new QLineEdit(m_header);
|
||||
|
||||
// Initialize QR code image label
|
||||
@@ -42,7 +47,7 @@ MainWindow::MainWindow() {
|
||||
m_headerLayout = new QHBoxLayout(m_header);
|
||||
m_headerLayout->addWidget(m_label);
|
||||
m_headerLayout->addWidget(m_lineEdit);
|
||||
m_headerLayout->addWidget(m_pushButton);
|
||||
m_headerLayout->addWidget(m_saveButton);
|
||||
m_headerLayout->setContentsMargins(0, 0, 0, 0);
|
||||
m_headerLayout->setStretch(1, 1);
|
||||
|
||||
@@ -59,22 +64,26 @@ MainWindow::MainWindow() {
|
||||
setWindowTitle(tr("QR Code Generator"));
|
||||
|
||||
// Disable "Generate QR code" button when line edit contains empty text
|
||||
connect(m_lineEdit, &QLineEdit::textChanged, this, [this](const QString& text) {
|
||||
m_pushButton->setEnabled(!text.isEmpty());
|
||||
connect(m_lineEdit, &QLineEdit::textChanged, this,
|
||||
[this](const QString &text) {
|
||||
m_saveButton->setEnabled(!text.isEmpty());
|
||||
if (!text.isEmpty())
|
||||
generateQrCode();
|
||||
});
|
||||
|
||||
// Update QR code when user clicks on the generate QR code button
|
||||
connect(m_pushButton, &QPushButton::clicked, this, &MainWindow::onGenerateButtonClicked);
|
||||
connect(m_saveButton, &QPushButton::clicked, this, &MainWindow::saveQrCode);
|
||||
|
||||
// Set "Hello World" text & generate QR code
|
||||
m_lineEdit->setText(tr("Hello World!"));
|
||||
onGenerateButtonClicked();
|
||||
generateQrCode();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() {
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete m_label;
|
||||
delete m_lineEdit;
|
||||
delete m_pushButton;
|
||||
delete m_saveButton;
|
||||
delete m_qrCodeDisplay;
|
||||
delete m_headerLayout;
|
||||
delete m_verticalLayout;
|
||||
@@ -82,7 +91,27 @@ MainWindow::~MainWindow() {
|
||||
delete m_centralWidget;
|
||||
}
|
||||
|
||||
void MainWindow::onGenerateButtonClicked() {
|
||||
void MainWindow::saveQrCode()
|
||||
{
|
||||
auto svg = m_generator.generateSvgQr(m_lineEdit->text());
|
||||
if (svg.isEmpty())
|
||||
return;
|
||||
|
||||
auto path = QFileDialog::getSaveFileName(this, tr("Save QR Code"),
|
||||
QDir::homePath(), tr("*.svg"));
|
||||
if (!path.isEmpty())
|
||||
{
|
||||
QFile file(path);
|
||||
if (file.open(QFile::WriteOnly))
|
||||
{
|
||||
file.write(svg.toUtf8());
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::generateQrCode()
|
||||
{
|
||||
// Get text from line edit
|
||||
auto text = m_lineEdit->text();
|
||||
|
||||
|
||||
@@ -30,7 +30,8 @@
|
||||
|
||||
#include <QrCodeGenerator.h>
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
@@ -38,7 +39,8 @@ public:
|
||||
~MainWindow();
|
||||
|
||||
private Q_SLOTS:
|
||||
void onGenerateButtonClicked();
|
||||
void saveQrCode();
|
||||
void generateQrCode();
|
||||
|
||||
private:
|
||||
QrCodeGenerator m_generator;
|
||||
@@ -49,7 +51,7 @@ private:
|
||||
QLabel *m_label;
|
||||
QLineEdit *m_lineEdit;
|
||||
QLabel *m_qrCodeDisplay;
|
||||
QPushButton *m_pushButton;
|
||||
QPushButton *m_saveButton;
|
||||
|
||||
QHBoxLayout *m_headerLayout;
|
||||
QVBoxLayout *m_verticalLayout;
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.6 MiB |
+2
-1
@@ -22,7 +22,8 @@
|
||||
|
||||
#include "MainWindow.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
MainWindow window;
|
||||
window.show();
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 364 KiB |
+12
-8
@@ -46,11 +46,13 @@ QrCodeGenerator::QrCodeGenerator(QObject *parent)
|
||||
* @param errorCorrection The level of error correction to apply.
|
||||
* @return QImage representing the generated QR code.
|
||||
*/
|
||||
QImage QrCodeGenerator::generateQr(const QString &data, quint16 size, quint16 borderSize,
|
||||
QImage QrCodeGenerator::generateQr(const QString &data, quint16 size,
|
||||
quint16 borderSize,
|
||||
qrcodegen::QrCode::Ecc errorCorrection)
|
||||
{
|
||||
auto b = data.toUtf8();
|
||||
const auto qrCode = qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
|
||||
const auto qrCode
|
||||
= qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
|
||||
return qrCodeToImage(qrCode, borderSize, size);
|
||||
}
|
||||
|
||||
@@ -65,7 +67,8 @@ QString QrCodeGenerator::generateSvgQr(const QString &data, quint16 borderSize,
|
||||
qrcodegen::QrCode::Ecc errorCorrection)
|
||||
{
|
||||
auto b = data.toUtf8();
|
||||
const auto qrCode = qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
|
||||
const auto qrCode
|
||||
= qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
|
||||
return toSvgString(qrCode, borderSize);
|
||||
}
|
||||
|
||||
@@ -75,7 +78,8 @@ QString QrCodeGenerator::generateSvgQr(const QString &data, quint16 borderSize,
|
||||
* @param border The border size to use.
|
||||
* @return QString containing the SVG representation of the QR code.
|
||||
*/
|
||||
QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border) const
|
||||
QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr,
|
||||
quint16 border) const
|
||||
{
|
||||
QString str;
|
||||
QTextStream sb(&str);
|
||||
@@ -92,8 +96,8 @@ QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border
|
||||
{
|
||||
if (qr.getModule(x, y))
|
||||
{
|
||||
sb << (x == 0 && y == 0 ? "" : " ") << "M" << (x + border) << "," << (y + border)
|
||||
<< "h1v1h-1z";
|
||||
sb << (x == 0 && y == 0 ? "" : " ") << "M" << (x + border) << ","
|
||||
<< (y + border) << "h1v1h-1z";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,8 +113,8 @@ QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border
|
||||
* @param size The image size to generate.
|
||||
* @return QImage representing the QR code.
|
||||
*/
|
||||
QImage QrCodeGenerator::qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border,
|
||||
quint16 size) const
|
||||
QImage QrCodeGenerator::qrCodeToImage(const qrcodegen::QrCode &qrCode,
|
||||
quint16 border, quint16 size) const
|
||||
{
|
||||
QString svg = toSvgString(qrCode, border);
|
||||
QSvgRenderer render(svg.toUtf8());
|
||||
|
||||
+14
-8
@@ -30,8 +30,8 @@
|
||||
|
||||
/**
|
||||
* @class QrCodeGenerator
|
||||
* @brief The QrCodeGenerator class is a simple C++ class that uses the qrcodegen library
|
||||
* to generate QR codes from QStrings in Qt applications.
|
||||
* @brief The QrCodeGenerator class is a simple C++ class that uses the
|
||||
* qrcodegen library to generate QR codes from QStrings in Qt applications.
|
||||
*/
|
||||
class QrCodeGenerator : public QObject
|
||||
{
|
||||
@@ -46,26 +46,31 @@ public:
|
||||
* @brief Generates a QR code from the given data and error correction level.
|
||||
* @param data The QString containing the data to encode in the QR code.
|
||||
* @param size The desired width/height of the generated image (default: 500).
|
||||
* @param borderSize The desired border width of the generated image (default: 1).
|
||||
* @param borderSize The desired border width of the generated image (default:
|
||||
* 1).
|
||||
* @param errorCorrection The desired error correction level (default:
|
||||
* qrcodegen::QrCode::Ecc::MEDIUM).
|
||||
*
|
||||
* @return QImage containing the generated QR code.
|
||||
*/
|
||||
QImage generateQr(const QString &data, const quint16 size = 1000, const quint16 borderSize = 1,
|
||||
qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM);
|
||||
QImage generateQr(const QString &data, const quint16 size = 1000,
|
||||
const quint16 borderSize = 1,
|
||||
qrcodegen::QrCode::Ecc errorCorrection
|
||||
= qrcodegen::QrCode::Ecc::MEDIUM);
|
||||
|
||||
/**
|
||||
* @brief Generates a QR code from the given data and error correction level.
|
||||
* @param data The QString containing the data to encode in the QR code.
|
||||
* @param borderSize The desired border width of the generated image (default: 1).
|
||||
* @param borderSize The desired border width of the generated image (default:
|
||||
* 1).
|
||||
* @param errorCorrection The desired error correction level (default:
|
||||
* qrcodegen::QrCode::Ecc::MEDIUM).
|
||||
*
|
||||
* @return QString string containing the generated QR code in SVG format.
|
||||
*/
|
||||
QString generateSvgQr(const QString &data, const quint16 borderSize = 1,
|
||||
qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM);
|
||||
qrcodegen::QrCode::Ecc errorCorrection
|
||||
= qrcodegen::QrCode::Ecc::MEDIUM);
|
||||
|
||||
private:
|
||||
/**
|
||||
@@ -85,5 +90,6 @@ private:
|
||||
*
|
||||
* @return QImage containing the QR code.
|
||||
*/
|
||||
QImage qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border, const quint16 size) const;
|
||||
QImage qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border,
|
||||
const quint16 size) const;
|
||||
};
|
||||
|
||||
+355
-186
File diff suppressed because it is too large
Load Diff
+151
-112
@@ -4,21 +4,21 @@
|
||||
* Copyright (c) Project Nayuki. (MIT License)
|
||||
* https://www.nayuki.io/page/qr-code-generator-library
|
||||
*
|
||||
* 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:
|
||||
* 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.
|
||||
* 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
|
||||
@@ -29,7 +29,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace qrcodegen {
|
||||
namespace qrcodegen
|
||||
{
|
||||
|
||||
/*
|
||||
* A segment of character/binary/control data in a QR Code symbol.
|
||||
@@ -38,11 +39,13 @@ namespace qrcodegen {
|
||||
* and call a static factory function such as QrSegment::makeNumeric().
|
||||
* The low-level way to create a segment is to custom-make the bit buffer
|
||||
* and call the QrSegment() constructor with appropriate values.
|
||||
* 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.
|
||||
* 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 {
|
||||
class QrSegment final
|
||||
{
|
||||
|
||||
/*---- Public helper enumeration ----*/
|
||||
|
||||
@@ -50,7 +53,8 @@ class QrSegment final {
|
||||
* Describes how a segment's data bits are interpreted. Immutable.
|
||||
*/
|
||||
public:
|
||||
class Mode final {
|
||||
class Mode final
|
||||
{
|
||||
|
||||
/*-- Constants --*/
|
||||
|
||||
@@ -87,15 +91,16 @@ public:
|
||||
/*-- Methods --*/
|
||||
|
||||
/*
|
||||
* (Package-private) Returns the mode indicator bits, which is an unsigned 4-bit value (range 0
|
||||
* to 15).
|
||||
* (Package-private) Returns the mode indicator bits, which is an unsigned
|
||||
* 4-bit value (range 0 to 15).
|
||||
*/
|
||||
public:
|
||||
int getModeBits() const;
|
||||
|
||||
/*
|
||||
* (Package-private) Returns the bit width of the character count field for a segment in
|
||||
* this mode in a QR Code at the given version number. The result is in the range [0, 16].
|
||||
* (Package-private) Returns the bit width of the character count field for
|
||||
* a segment in this mode in a QR Code at the given version number. The
|
||||
* result is in the range [0, 16].
|
||||
*/
|
||||
public:
|
||||
int numCharCountBits(int ver) const;
|
||||
@@ -112,22 +117,25 @@ public:
|
||||
static QrSegment makeBytes(const std::vector<std::uint8_t> &data);
|
||||
|
||||
/*
|
||||
* Returns a segment representing the given string of decimal digits encoded in numeric mode.
|
||||
* Returns a segment representing the given string of decimal digits encoded
|
||||
* in numeric mode.
|
||||
*/
|
||||
public:
|
||||
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.
|
||||
* 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.
|
||||
*/
|
||||
public:
|
||||
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.
|
||||
* 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.
|
||||
*/
|
||||
public:
|
||||
static std::vector<QrSegment> makeSegments(const char *text);
|
||||
@@ -149,9 +157,10 @@ public:
|
||||
static bool isNumeric(const char *text);
|
||||
|
||||
/*
|
||||
* Tests whether the given string can be encoded as a segment in alphanumeric mode.
|
||||
* A string is encodable iff each character is in the following set: 0 to 9, A to Z
|
||||
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
||||
* Tests whether the given string can be encoded as a segment in alphanumeric
|
||||
* mode. A string is encodable iff each character is in the following set: 0
|
||||
* to 9, A to Z (uppercase only), space, dollar, percent, asterisk, plus,
|
||||
* hyphen, period, slash, colon.
|
||||
*/
|
||||
public:
|
||||
static bool isAlphanumeric(const char *text);
|
||||
@@ -177,16 +186,18 @@ private:
|
||||
|
||||
/*
|
||||
* Creates a new QR Code segment with the given attributes and data.
|
||||
* The character count (numCh) must agree with the mode and the bit buffer length,
|
||||
* but the constraint isn't checked. The given bit buffer is copied and stored.
|
||||
* The character count (numCh) must agree with the mode and the bit buffer
|
||||
* length, but the constraint isn't checked. The given bit buffer is copied
|
||||
* and stored.
|
||||
*/
|
||||
public:
|
||||
QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt);
|
||||
|
||||
/*
|
||||
* Creates a new QR Code segment with the given parameters and data.
|
||||
* The character count (numCh) must agree with the mode and the bit buffer length,
|
||||
* but the constraint isn't checked. The given bit buffer is moved and stored.
|
||||
* The character count (numCh) must agree with the mode and the bit buffer
|
||||
* length, but the constraint isn't checked. The given bit buffer is moved and
|
||||
* stored.
|
||||
*/
|
||||
public:
|
||||
QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt);
|
||||
@@ -211,9 +222,10 @@ public:
|
||||
public:
|
||||
const std::vector<bool> &getData() const;
|
||||
|
||||
// (Package-private) Calculates the number of bits needed to encode the given segments at
|
||||
// the given version. Returns a non-negative number if successful. Otherwise returns -1 if a
|
||||
// segment has too many characters to fit its length field, or the total bits exceeds INT_MAX.
|
||||
// (Package-private) Calculates the number of bits needed to encode the given
|
||||
// segments at the given version. Returns a non-negative number if successful.
|
||||
// Otherwise returns -1 if a segment has too many characters to fit its length
|
||||
// field, or the total bits exceeds INT_MAX.
|
||||
public:
|
||||
static int getTotalBits(const std::vector<QrSegment> &segs, int version);
|
||||
|
||||
@@ -228,20 +240,24 @@ private:
|
||||
/*
|
||||
* A QR Code symbol, which is a type of two-dimension barcode.
|
||||
* Invented by Denso Wave and described in the ISO/IEC 18004 standard.
|
||||
* Instances of this class represent an immutable square grid of dark and light cells.
|
||||
* The class provides static factory functions to create a QR Code from text or binary data.
|
||||
* The class covers the QR Code Model 2 specification, supporting all versions (sizes)
|
||||
* from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
|
||||
* Instances of this class represent an immutable square grid of dark and light
|
||||
* cells. The class provides static factory functions to create a QR Code from
|
||||
* text or binary data. The class covers the QR Code Model 2 specification,
|
||||
* supporting all versions (sizes) from 1 to 40, all 4 error correction levels,
|
||||
* and 4 character encoding modes.
|
||||
*
|
||||
* Ways to create a QR Code object:
|
||||
* - High level: Take the payload data and call QrCode::encodeText() or QrCode::encodeBinary().
|
||||
* - Mid level: Custom-make the list of segments and call QrCode::encodeSegments().
|
||||
* - High level: Take the payload data and call QrCode::encodeText() or
|
||||
* QrCode::encodeBinary().
|
||||
* - Mid level: Custom-make the list of segments and call
|
||||
* QrCode::encodeSegments().
|
||||
* - Low level: Custom-make the array of data codeword bytes (including
|
||||
* segment headers and final padding, excluding error correction codewords),
|
||||
* supply the appropriate version number, and call the QrCode() constructor.
|
||||
* (Note that all ways require supplying the desired error correction level.)
|
||||
*/
|
||||
class QrCode final {
|
||||
class QrCode final
|
||||
{
|
||||
|
||||
/*---- Public helper enumeration ----*/
|
||||
|
||||
@@ -249,7 +265,8 @@ class QrCode final {
|
||||
* The error correction level in a QR Code symbol.
|
||||
*/
|
||||
public:
|
||||
enum class Ecc {
|
||||
enum class Ecc
|
||||
{
|
||||
LOW = 0, // The QR Code can tolerate about 7% erroneous codewords
|
||||
MEDIUM, // The QR Code can tolerate about 15% erroneous codewords
|
||||
QUARTILE, // The QR Code can tolerate about 25% erroneous codewords
|
||||
@@ -263,22 +280,24 @@ private:
|
||||
/*---- Static factory functions (high level) ----*/
|
||||
|
||||
/*
|
||||
* Returns a QR Code 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 2953 or fewer UTF-8 code units (not Unicode code points) if the low error correction level
|
||||
* is used. 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.
|
||||
* Returns a QR Code 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 2953 or fewer UTF-8 code units
|
||||
* (not Unicode code points) if the low error correction level is used. 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.
|
||||
*/
|
||||
public:
|
||||
static QrCode encodeText(const char *text, Ecc ecl);
|
||||
|
||||
/*
|
||||
* Returns a QR Code representing the given binary data 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.
|
||||
* Returns a QR Code representing the given binary data 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.
|
||||
*/
|
||||
public:
|
||||
static QrCode encodeBinary(const std::vector<std::uint8_t> &data, Ecc ecl);
|
||||
@@ -286,19 +305,21 @@ public:
|
||||
/*---- Static factory functions (mid level) ----*/
|
||||
|
||||
/*
|
||||
* Returns a QR Code representing the given segments with the given encoding parameters.
|
||||
* The smallest possible QR Code version within the given range is automatically
|
||||
* chosen for the output. Iff boostEcl is true, then the ECC level of the result
|
||||
* may be higher than the ecl argument if it can be done without increasing the
|
||||
* version. The mask number is either between 0 to 7 (inclusive) to force that
|
||||
* mask, or -1 to automatically choose an appropriate mask (which may be slow).
|
||||
* This function allows the user to create a custom sequence of segments that switches
|
||||
* between modes (such as alphanumeric and byte) to encode text in less space.
|
||||
* This is a mid-level API; the high-level API is encodeText() and encodeBinary().
|
||||
* Returns a QR Code representing the given segments with the given encoding
|
||||
* parameters. The smallest possible QR Code version within the given range is
|
||||
* automatically chosen for the output. Iff boostEcl is true, then the ECC
|
||||
* level of the result may be higher than the ecl argument if it can be done
|
||||
* without increasing the version. The mask number is either between 0 to 7
|
||||
* (inclusive) to force that mask, or -1 to automatically choose an
|
||||
* appropriate mask (which may be slow). This function allows the user to
|
||||
* create a custom sequence of segments that switches between modes (such as
|
||||
* alphanumeric and byte) to encode text in less space. This is a mid-level
|
||||
* API; the high-level API is encodeText() and encodeBinary().
|
||||
*/
|
||||
public:
|
||||
static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl, int minVersion = 1,
|
||||
int maxVersion = 40, int mask = -1,
|
||||
static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl,
|
||||
int minVersion = 1, int maxVersion = 40,
|
||||
int mask = -1,
|
||||
bool boostEcl = true); // All optional parameters
|
||||
|
||||
/*---- Instance fields ----*/
|
||||
@@ -319,9 +340,10 @@ private:
|
||||
private:
|
||||
Ecc errorCorrectionLevel;
|
||||
|
||||
/* The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
|
||||
* Even if a QR Code is created with automatic masking requested (mask = -1),
|
||||
* the resulting object still has a mask value between 0 and 7. */
|
||||
/* The index of the mask pattern used in this QR Code, which is between 0 and
|
||||
* 7 (inclusive). Even if a QR Code is created with automatic masking
|
||||
* requested (mask = -1), the resulting object still has a mask value between
|
||||
* 0 and 7. */
|
||||
private:
|
||||
int mask;
|
||||
|
||||
@@ -332,8 +354,8 @@ private:
|
||||
private:
|
||||
std::vector<std::vector<bool>> modules;
|
||||
|
||||
// Indicates function modules that are not subjected to masking. Discarded when constructor
|
||||
// finishes.
|
||||
// Indicates function modules that are not subjected to masking. Discarded
|
||||
// when constructor finishes.
|
||||
private:
|
||||
std::vector<std::vector<bool>> isFunction;
|
||||
|
||||
@@ -346,7 +368,8 @@ private:
|
||||
* A mid-level API is the encodeSegments() function.
|
||||
*/
|
||||
public:
|
||||
QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords, int msk);
|
||||
QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords,
|
||||
int msk);
|
||||
|
||||
/*---- Public instance methods ----*/
|
||||
|
||||
@@ -375,16 +398,18 @@ public:
|
||||
int getMask() const;
|
||||
|
||||
/*
|
||||
* Returns the color of the module (pixel) at the given coordinates, which is false
|
||||
* for light or true for dark. The top left corner has the coordinates (x=0, y=0).
|
||||
* If the given coordinates are out of bounds, then false (light) is returned.
|
||||
* Returns the color of the module (pixel) at the given coordinates, which is
|
||||
* false for light or true for dark. The top left corner has the coordinates
|
||||
* (x=0, y=0). If the given coordinates are out of bounds, then false (light)
|
||||
* is returned.
|
||||
*/
|
||||
public:
|
||||
bool getModule(int x, int y) const;
|
||||
|
||||
/*---- Private helper methods for constructor: Drawing function modules ----*/
|
||||
|
||||
// Reads this object's version field, and draws and marks all function modules.
|
||||
// Reads this object's version field, and draws and marks all function
|
||||
// modules.
|
||||
private:
|
||||
void drawFunctionPatterns();
|
||||
|
||||
@@ -413,19 +438,23 @@ private:
|
||||
private:
|
||||
void setFunctionModule(int x, int y, bool isDark);
|
||||
|
||||
// Returns the color of the module at the given coordinates, which must be in range.
|
||||
// Returns the color of the module at the given coordinates, which must be in
|
||||
// range.
|
||||
private:
|
||||
bool module(int x, int y) const;
|
||||
|
||||
/*---- Private helper methods for constructor: Codewords and masking ----*/
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
private:
|
||||
std::vector<std::uint8_t> addEccAndInterleave(const std::vector<std::uint8_t> &data) const;
|
||||
std::vector<std::uint8_t>
|
||||
addEccAndInterleave(const std::vector<std::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. Function modules need to be marked off before this is called.
|
||||
// Draws the given sequence of 8-bit codewords (data and error correction)
|
||||
// onto the entire data area of this QR Code. Function modules need to be
|
||||
// marked off before this is called.
|
||||
private:
|
||||
void drawCodewords(const std::vector<std::uint8_t> &data);
|
||||
|
||||
@@ -437,39 +466,44 @@ private:
|
||||
private:
|
||||
void applyMask(int msk);
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
private:
|
||||
long getPenaltyScore() const;
|
||||
|
||||
/*---- Private helper functions ----*/
|
||||
|
||||
// Returns an ascending list of positions of alignment patterns for this version number.
|
||||
// Each position is in the range [0,177), and are used on both the x and y axes.
|
||||
// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
|
||||
// Returns an ascending list of positions of alignment patterns for this
|
||||
// version number. Each position is in the range [0,177), and are used on both
|
||||
// the x and y axes. This could be implemented as lookup table of 40
|
||||
// variable-length lists of unsigned bytes.
|
||||
private:
|
||||
std::vector<int> getAlignmentPatternPositions() const;
|
||||
|
||||
// Returns the number of data bits that can be stored in a QR Code of the given version number,
|
||||
// after all function modules are excluded. This includes remainder bits, so it might not be a
|
||||
// multiple of 8. The result is in the range [208, 29648]. This could be implemented as a 40-entry
|
||||
// lookup table.
|
||||
// Returns the number of data bits that can be stored in a QR Code of the
|
||||
// given version number, after all function modules are excluded. This
|
||||
// includes remainder bits, so it might not be a multiple of 8. The result is
|
||||
// in the range [208, 29648]. This could be implemented as a 40-entry lookup
|
||||
// table.
|
||||
private:
|
||||
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.
|
||||
// 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.
|
||||
private:
|
||||
static int getNumDataCodewords(int ver, Ecc ecl);
|
||||
|
||||
// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
|
||||
// implemented as a lookup table over all possible parameter values, instead of as an algorithm.
|
||||
// Returns a Reed-Solomon ECC generator polynomial for the given degree. This
|
||||
// could be implemented as a lookup table over all possible parameter values,
|
||||
// instead of as an algorithm.
|
||||
private:
|
||||
static std::vector<std::uint8_t> reedSolomonComputeDivisor(int degree);
|
||||
|
||||
// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
||||
// Returns the Reed-Solomon error correction codeword for the given data and
|
||||
// divisor polynomials.
|
||||
private:
|
||||
static std::vector<std::uint8_t>
|
||||
reedSolomonComputeRemainder(const std::vector<std::uint8_t> &data,
|
||||
@@ -485,16 +519,17 @@ private:
|
||||
private:
|
||||
int finderPenaltyCountPatterns(const std::array<int, 7> &runHistory) const;
|
||||
|
||||
// Must be called at the end of a line (row or column) of modules. A helper function for
|
||||
// getPenaltyScore().
|
||||
// Must be called at the end of a line (row or column) of modules. A helper
|
||||
// function for getPenaltyScore().
|
||||
private:
|
||||
int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength,
|
||||
std::array<int, 7> &runHistory) const;
|
||||
|
||||
// Pushes the given value to the front and drops the last value. A helper function for
|
||||
// getPenaltyScore().
|
||||
// Pushes the given value to the front and drops the last value. A helper
|
||||
// function for getPenaltyScore().
|
||||
private:
|
||||
void finderPenaltyAddHistory(int currentRunLength, std::array<int, 7> &runHistory) const;
|
||||
void finderPenaltyAddHistory(int currentRunLength,
|
||||
std::array<int, 7> &runHistory) const;
|
||||
|
||||
// Returns true iff the i'th bit of x is set to 1.
|
||||
private:
|
||||
@@ -533,19 +568,22 @@ private:
|
||||
/*---- Public exception class ----*/
|
||||
|
||||
/*
|
||||
* Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception
|
||||
* include:
|
||||
* Thrown when the supplied data does not fit any QR Code version. Ways to
|
||||
* handle this exception include:
|
||||
* - Decrease the error correction level if it was greater than Ecc::LOW.
|
||||
* - If the encodeSegments() function was called with a maxVersion argument, then increase
|
||||
* it if it was less than QrCode::MAX_VERSION. (This advice does not apply to the other
|
||||
* factory functions because they search all versions up to QrCode::MAX_VERSION.)
|
||||
* - Split the text data into better or optimal segments in order to reduce the number of bits
|
||||
* required.
|
||||
* - If the encodeSegments() function was called with a maxVersion argument,
|
||||
* then increase it if it was less than QrCode::MAX_VERSION. (This advice does
|
||||
* not apply to the other factory functions because they search all versions up
|
||||
* to QrCode::MAX_VERSION.)
|
||||
* - Split the text data into better or optimal segments in order to reduce the
|
||||
* number of bits required.
|
||||
* - Change the text or binary data to be shorter.
|
||||
* - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).
|
||||
* - Change the text to fit the character set of a particular segment mode (e.g.
|
||||
* alphanumeric).
|
||||
* - Propagate the error upward to the caller/user.
|
||||
*/
|
||||
class data_too_long : public std::length_error {
|
||||
class data_too_long : public std::length_error
|
||||
{
|
||||
|
||||
public:
|
||||
explicit data_too_long(const std::string &msg);
|
||||
@@ -554,7 +592,8 @@ public:
|
||||
/*
|
||||
* An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
|
||||
*/
|
||||
class BitBuffer final : public std::vector<bool> {
|
||||
class BitBuffer final : public std::vector<bool>
|
||||
{
|
||||
|
||||
/*---- Constructor ----*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user