Add border to generated QR codes

This commit is contained in:
Guillaume LE VAILLANT 2018-10-14 13:55:59 +02:00 committed by Sander Ferdinand
parent ca7c0e0c55
commit f17c407100
2 changed files with 15 additions and 6 deletions

View file

@ -473,7 +473,7 @@ Rectangle {
Image {
id: qrCode
anchors.fill: parent
anchors.margins: 6
anchors.margins: 1
smooth: false
fillMode: Image.PreserveAspectFit

View file

@ -7,12 +7,21 @@ QImage QRCodeImageProvider::genQrImage(const QString &id, QSize *size)
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)); // 1 is black, not "255/white"
unsigned int black = 0;
unsigned int white = 1;
unsigned int borderSize = 4;
unsigned int imageSize = qrcode.size + (2 * borderSize);
QImage img = QImage(imageSize, imageSize, QImage::Format_Mono);
for (unsigned int y = 0; y < imageSize; ++y)
for (unsigned int x = 0; x < imageSize; ++x)
if ((x < borderSize) || (x >= imageSize - borderSize) || (y < borderSize) || (y >= imageSize - borderSize))
img.setPixel(x, y, white);
else
img.setPixel(x, y, qrcode.getModule(x - borderSize, y - borderSize) ? black : white);
if (size)
*size = QSize(qrcode.size, qrcode.size);
*size = QSize(imageSize, imageSize);
return img;
}