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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
(c) Copyright 2012 - 2026 Anton Sviridenko
https://picapica.im
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "qrcode.h"
#include "../PICA_id.h"
#include <QDebug>
#include <QtEndian>
#include <ZXing/ReadBarcode.h>
// Which of ZXing-C++'s two writer APIs this libZXing has is found out by
// configure, see the comments there. 3.x builds the new one by default, 2.x
// the old.
#ifdef HAVE_ZXING_CREATE_BARCODE
#include <ZXing/CreateBarcode.h>
#else
#include <ZXing/MultiFormatWriter.h>
#include <ZXing/BitMatrix.h>
#endif
#include <exception>
#include <string>
QImage picaQrCodeImage(const QString &text, int quietZone)
{
const std::string utf8 = text.toStdString();
if (quietZone < 0)
quietZone = 0;
// ZXing reports what it cannot encode by throwing, in both APIs - text too
// long for the largest QR code being the one way that could happen here.
try
{
#ifdef HAVE_ZXING_CREATE_BARCODE
const ZXing::Barcode barcode =
ZXing::CreateBarcodeFromText(utf8, ZXing::CreatorOptions(ZXing::BarcodeFormat::QRCode));
// One byte per module, 0 for a dark one and 0xff for a light one.
const ZXing::ImageView symbol = barcode.symbol();
const int width = symbol.width();
const int height = symbol.height();
auto dark = [&symbol](int x, int y) { return *symbol.data(x, y) < 0x80; };
#else
const ZXing::BitMatrix matrix =
ZXing::MultiFormatWriter(ZXing::BarcodeFormat::QRCode).setMargin(0).encode(utf8, 0, 0);
const int width = matrix.width();
const int height = matrix.height();
auto dark = [&matrix](int x, int y) { return matrix.get(x, y); };
#endif
if (width <= 0 || height <= 0)
return QImage();
QImage image(width + 2 * quietZone, height + 2 * quietZone, QImage::Format_RGB32);
if (image.isNull())
return QImage();
image.fill(Qt::white);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
if (dark(x, y))
image.setPixel(x + quietZone, y + quietZone, qRgb(0, 0, 0));
return image;
}
catch (const std::exception &e)
{
qWarning() << "Could not encode a QR code:" << e.what();
return QImage();
}
}
QByteArray picaIdFromQrText(const QString &text)
{
const QByteArray id = QByteArray::fromBase64(text.trimmed().toLatin1()).left(PICA_ID_SIZE);
return id.size() == PICA_ID_SIZE ? id : QByteArray();
}
QrCodeReader::QrCodeReader(QObject *parent) :
QObject(parent)
{
}
void QrCodeReader::read(QImage frame)
{
if (frame.format() != QImage::Format_RGB32 && frame.format() != QImage::Format_ARGB32)
frame = frame.convertToFormat(QImage::Format_RGB32);
if (frame.isNull())
{
emit frameRead(QString());
return;
}
// Format_RGB32 is a 32 bit 0xffRRGGBB per pixel in the machine's own byte
// order, so what ZXing sees in memory depends on it.
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
const ZXing::ImageFormat format = ZXing::ImageFormat::BGRX;
#else
const ZXing::ImageFormat format = ZXing::ImageFormat::XRGB;
#endif
const ZXing::ImageView image(frame.constBits(), frame.width(), frame.height(), format,
(int)frame.bytesPerLine());
// QR codes only, which spares trying every other symbology on every frame.
// tryHarder and tryRotate are what make a code held at an angle in front of
// the camera, or a little out of focus, readable at all; tryInvert also
// takes light-on-dark codes, as a phone in dark mode may draw them.
ZXing::ReaderOptions options;
options.setFormats(ZXing::BarcodeFormat::QRCode);
options.setTryHarder(true);
options.setTryRotate(true);
options.setTryInvert(true);
QString text;
try
{
const auto result = ZXing::ReadBarcode(image, options);
if (result.isValid())
text = QString::fromStdString(result.text());
}
catch (const std::exception &e)
{
qWarning() << "Could not read a QR code:" << e.what();
}
emit frameRead(text);
}
|