Custom qrcode renderer to allow smooth resizing

This commit is contained in:
Aditya Kulkarni
2018-10-30 16:04:26 -07:00
parent 489bb4ae5f
commit 75e755707c
5 changed files with 96 additions and 39 deletions

View File

@@ -701,38 +701,7 @@ void MainWindow::setupRecieveTab() {
}
ui->txtRecieve->setPlainText(addr);
QSize sz = ui->qrcodeDisplay->size();
QPixmap pm(sz);
pm.fill(Qt::white);
QPainter painter(&pm);
// NOTE: At this point you will use the API to get the encoding and format you want, instead of my hardcoded stuff:
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(addr.toUtf8().constData(), qrcodegen::QrCode::Ecc::LOW);
const int s = qr.getSize()>0?qr.getSize():1;
const double w = sz.width();
const double h = sz.height();
const double aspect = w/h;
const double size = ((aspect>1.0)?h:w);
const double scale = size/(s+2);
const double offset = (w - size) > 0 ? (w - size) / 2 : 0;
// NOTE: For performance reasons my implementation only draws the foreground parts in supplied color.
// It expects background to be prepared already (in white or whatever is preferred).
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(Qt::black));
for(int y=0; y<s; y++) {
for(int x=0; x<s; x++) {
const int color=qr.getModule(x, y); // 0 for white, 1 for black
if(0!=color) {
const double rx1=(x+1)*scale+ offset, ry1=(y+1)*scale;
QRectF r(rx1, ry1, scale, scale);
painter.drawRects(&r,1);
}
}
}
ui->qrcodeDisplay->setPixmap(pm);
ui->qrcodeDisplay->setAddress(addr);
});
}