gpt4 book ai didi

c++ - 在原生 C/C++ 中使用 Qt 绘制二维码

转载 作者:行者123 更新时间:2023-11-28 05:14:16 25 4
gpt4 key购买 nike

我成功地在 QLabel 上绘制并显示了 QrCode,但是当我扫描它时无法识别它。这是我使用的代码 - 我制作了一个带有静态函数的小类:

void QrCodeDrawer::paintQR(QPainter &painter, const QSize sz, const QString &data, QColor fg)
{
char *str=data.toUtf8().data();
// NOTE: At this point you will use the API to get the encoding and format you want, instead of my hardcoded stuff:
QrCode qr = QrCode::encodeText(str, QrCode::Ecc::HIGH);
const int s=qr.size>0?qr.size: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);
// 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(fg);
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(0x0!=color) {
const double rx1=(x+1)*scale, ry1=(y+1)*scale;
QRectF r(rx1, ry1, scale, scale);
painter.drawRects(&r,1);
}
}
}
}

并在这里调用它:

QPixmap map(400,400);
QPainter painter(&map);
QrCodeDrawer::paintQR(painter,QSize(400,400),"Hello World", QColor("white"));
ui.qrCode->setPixmap(map);

我将“Hello World”作为输入字符串,这是我得到的代码: enter image description here

我从here得到了源代码.

最佳答案

我使用了相同的示例,但遇到垃圾 QR 码问题,直到我意识到 str 可能是一个悬空指针。我将我的代码更改为此并且它工作正常:

void QrCodeDrawer::paintQR(QPainter &painter, const QSize sz, const QString &data, QColor fg)
{
// NOTE: At this point you will use the API to get the encoding and format you want, instead of my hardcoded stuff:
QrCode qr = QrCode::encodeText(data.toUtf8().constData(), QrCode::Ecc::HIGH);

关于c++ - 在原生 C/C++ 中使用 Qt 绘制二维码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42979325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com