【问题标题】:Drawing QR code with Qt in native C/C++在原生 C/C++ 中使用 Qt 绘制二维码
【发布时间】:2017-08-16 04:49:25
【问题描述】:

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

我从here得到源代码。

【问题讨论】:

  • 好像倒过来了
  • 是的!在我反转颜色后它被识别
  • @kim366 哦,哦!你是对的!,我也尝试过反转,它起作用了!,我起初想知道为什么边缘的 3 个正方形内部是白色的(这很不寻常),但我更紧张的是它被反转了!
  • 是的,它对我有用,可能是应用程序开发人员负责此案。事实上,代码可以是任何颜色,而不仅仅是黑白。
  • QR 码规范确实允许使用倒色的符号。但是,除非您切换选项,否则流行的程序(例如 Android 条码扫描仪)无法识别反转,这很麻烦。因此,如果您想要最好的兼容性,请不要绘制反码。

标签: c++ qt cross-platform render qr-code


【解决方案1】:

我使用了相同的示例,但是在我意识到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);

【讨论】:

    【解决方案2】:

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

    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);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 2018-08-08
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 2012-10-28
      • 2017-07-19
      • 1970-01-01
      • 2021-02-04
      相关资源
      最近更新 更多