Replace libqrencode with nayuki/QR-Code-generator

This commit is contained in:
Felix Kauselmann
2023-03-25 11:06:17 +01:00
parent ad4139e017
commit 8f631763ce
17 changed files with 1718 additions and 103 deletions

View File

@ -262,6 +262,7 @@ include(../compressed_archive/libarchive/libarchive-wrapper.pri)
include(./comic_vine/comic_vine.pri)
include(../third_party/QsLog/QsLog.pri)
include(../shortcuts_management/shortcuts_management.pri)
include(../third_party/QrCode/QrCode.pri)
RESOURCES += images.qrc files.qrc
win32:RESOURCES += images_win.qrc

View File

@ -10,11 +10,13 @@
#include <QFormLayout>
#include <QBitmap>
#include <QPainter>
#include <QPixmap>
#include "yacreader_http_server.h"
#include "yacreader_global_gui.h"
#include "qnaturalsorting.h"
#include "qrcodegen.hpp"
#include <algorithm>
@ -271,20 +273,31 @@ void ServerConfigDialog::generateQR(const QString &serverAddress)
{
qrCode->clear();
QrEncoder encoder;
QBitmap image = encoder.encode(serverAddress);
if (image.isNull()) {
qrCode->setText(tr("Could not load libqrencode."));
} else {
image = image.scaled(qrCode->size() * devicePixelRatioF());
qrcodegen::QrCode code = qrcodegen::QrCode::encodeText(
serverAddress.toLocal8Bit(),
qrcodegen::QrCode::Ecc::LOW);
QPixmap pMask(image.size());
pMask.fill(QColor(66, 66, 66));
pMask.setMask(image.createMaskFromColor(Qt::white));
pMask.setDevicePixelRatio(devicePixelRatioF());
qrCode->setPixmap(pMask);
QBitmap image(code.getSize(), code.getSize());
image.fill();
QPainter painter;
painter.begin(&image);
for (int x = 0; x < code.getSize(); x++) {
for (int y = 0; y < code.getSize(); y++) {
if (code.getModule(x, y)) {
painter.drawPoint(x, y);
}
}
}
painter.end();
image = image.scaled(qrCode->size() * devicePixelRatioF());
QPixmap pMask(image.size());
pMask.fill(QColor(66, 66, 66));
pMask.setMask(image.createMaskFromColor(Qt::white));
pMask.setDevicePixelRatio(devicePixelRatioF());
qrCode->setPixmap(pMask);
}
void ServerConfigDialog::regenerateQR(const QString &ip)
@ -305,46 +318,3 @@ void ServerConfigDialog::updatePort()
generateQR(ip->currentText() + ":" + port->text());
}
QrEncoder::QrEncoder()
{
#ifdef Q_OS_MACOS
QLibrary encoder(QCoreApplication::applicationDirPath() + "/utils/libqrencode.dylib");
#else
QLibrary encoder("qrencode");
#ifdef Q_OS_UNIX
encoder.load();
// Fallback - this loads libqrencode.4.x.x.so when libqrencode.so is not available
if (!encoder.isLoaded()) {
encoder.setFileNameAndVersion("qrencode", 4);
}
#endif
#endif
QRcode_encodeString8bit = (_QRcode_encodeString8bit)encoder.resolve("QRcode_encodeString8bit");
QRcode_free = (_QRcode_free)encoder.resolve("QRcode_free");
}
QBitmap QrEncoder::encode(const QString &string)
{
if (!QRcode_encodeString8bit) {
return QBitmap();
}
QRcode *code;
code = QRcode_encodeString8bit(string.toUtf8().data(), 0, 0);
QBitmap result(code->width, code->width);
result.fill();
/* convert to QBitmap */
QPainter painter;
painter.begin(&result);
unsigned char *pointer = code->data;
for (int x = 0; x < code->width; x++) {
for (int y = 0; y < code->width; y++) {
if ((*pointer++ & 0x1) == 1) {
painter.drawPoint(x, y);
}
}
}
painter.end();
QRcode_free(code);
return result;
}

View File

@ -5,10 +5,8 @@
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QPixmap>
#include <QComboBox>
#include <QCheckBox>
#include <QLibrary>
class ServerConfigDialog : public QDialog
{
@ -37,24 +35,4 @@ public slots:
signals:
void portChanged(QString port);
};
class QrEncoder
{
public:
QrEncoder();
QBitmap encode(const QString &string);
private:
/*libqrencode data structures*/
typedef struct {
int version; ///< version of the symbol
int width; ///< width of the symbol
unsigned char *data; ///< symbol data
} QRcode;
typedef QRcode *(*_QRcode_encodeString8bit)(char[], int, int);
typedef void (*_QRcode_free)(QRcode *);
_QRcode_free QRcode_free;
_QRcode_encodeString8bit QRcode_encodeString8bit;
};
#endif