Qt 二维码超简单示例


Qt 生成二维码需要使用第三方库,目前最常用的是 qrencode 库。

安装 qrencode 库

在 Manjaro 上直接

sudo pacman -S qrencode

安装即可。不能自动安装的也可以通过源码进行编译,config 之后 make 就能搞定。另外源码中有 cmake 文件,因此也可以使用 Qt cmake 进行编译。

一点点说明

一开始以为 qrencode 会直接输出二维码图像,但实际上不是这样的,导致走了许多弯路。qrencode 的输出主要是 QRcode 对象。QRcode 用来表示二维码数据和信息,QRcode 的 data 字段中每个字节的最低位表示二维码对应位置是白色方块还是黑色方块(1-7位包含了其他信息)。所以拿到 QRcode 对象后要经过转换才能得到最终的二维码图片。

参考代码

Pro 文件如下:

# Q2RCode.pro

QT += gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

unix:!macx: LIBS += -lqrencode

main.cpp 如下:

/**
 * @file    main.cpp
 * @author  Rich Chan (cy187lion@sina.com)
 * @brief   qrencode 库使用示例.
 * @version 0.1
 * @date    2020-05-15
 * 
 * @copyright Copyright (c) 2020
 * 
 */
#include <QCoreApplication>

#include <QImage>
#include <QPainter>

#include <qrencode.h>

/**
 * @brief Draw  通过 QRcode 生成图像.
 * @param qrc   输入的二维码数据.
 * @param image 生成的图像.
 */
void Draw(QRcode *qrc, QImage &image)
{
    const int width = qrc->width > 0 ? qrc->width : 1;
    double scale = image.width()/width;
    QPainter painter;

    painter.begin(&image);
    // 白色背景图.
    QColor background(Qt::white);
    painter.setBrush(background);
    painter.setPen(Qt::NoPen);
    painter.drawRect(0, 0, image.width(), image.width());

    // 码值为黑色.
    QColor foreground(Qt::black);
    painter.setBrush(foreground);

    // 二维码数据转换为图像需要进行放大, 否则将由单个像素点表示码值, 看起来很怪异.
    for(int y=0; y<width; y++)
    {
        for(int x=0; x<width; x++)
        {
            unsigned char b=qrc->data[y*width+x];
            /**
             * qrc->data 的每个字节对应二维码中的一个小方块, 如果该字节最低位为高则绘制黑色方块.
             * 字节中具体每一位的含义如下
             *
             *     bit0: 1=black/0=white
             *     bit1: data and ecc code area
             *     bit2: format information
             *     bit3: version information
             *     bit4: timing pattern
             *     bit5: alignment pattern
             *     bit6: finder pattern and separator
             *     bit7: non-data modules (format, timing, etc.)
             */
            if(b&0x01)
            {
                QRectF r(x*scale, y*scale, scale, scale);
                painter.drawRects(&r, 1);
            }
        }
    }
    painter.end();
}

int main(void)
{
    // 将字符串转换为二维码数据, QRcode_encodeString 的参数请参照官方文档.
    QRcode *qrc = QRcode_encodeString("Hello, This is a test!", 0, QR_ECLEVEL_H, QR_MODE_8, 1);
    QImage image(256, 256, QImage::Format_RGB32);
    Draw(qrc, image);
    // 使用 QImage 将二维码保存为图片, 字符串扩展名决定了图像编码格式.
    image.save("./qrcode.png");
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lionchan187

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值