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

Drawing QR code with Qt in native C/C++

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

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" 作为输入字符串,这是我得到的代码:

我从 here 获得了源代码。

我终于解决了这个问题,方法是先绘制背景(白色或任何颜色),然后用足够不同的颜色在其上绘制二维码...

void paintQR(QPainter &painter, const QSize sz, const QString &data, QColor fg, QColor bg)
{
    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);

    painter.setPen(Qt::NoPen);
    // Setting the background color....
    painter.setBrush(bg);
    for(int y=0; y<400; y++) {
        for(int x=0; x<400; x++) {
            const double rx1=(x+1)*scale, ry1=(y+1)*scale;
            QRectF r(x, y, 1, 1);
            painter.drawRects(&r,1);
        }
    }
    //Drawing the Code....
    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);
            }
        }
    }
}

我使用了相同的示例,但遇到垃圾 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);