Qt实现二维码生成和识别

        QR码(全称为快速响应矩阵码;英语:Quick Response Code)是二维条码的一种,于1994年由日本DENSO WAVE公司发明。QR来自英文Quick Response的缩写,即快速反应,因为发明者希望QR码可以让其内容快速被解码。QR码使用四种标准化编码模式(数字,字母数字,字节(二进制)和汉字)来存储数据。QR码最常见于日本,为目前日本最流行的二维空间条码。QR码比较普通条码可以存储更多数据,也无需要像普通条码般在扫描时需要直线对准扫描仪。因此其应用范围已经扩展到包括产品跟踪,物品识别,文档管理,营销等方面。

1.QZXing使用
1.1.源码下载

链接地址:GitHub - zxing/zxing: ZXing ("Zebra Crossing") barcode scanning library for Java, Android

QZXing的下载地址:GitHub - ftylitak/qzxing: Qt/QML wrapper library for the ZXing library. 1D/2D barcode image processing library

1.2.编译

用Qt打开src文件夹里的pro文件,然后进行编译。在对应的文件夹找到QZXing3.dll和libQZXing3.a;msvc编译器编译的就是QZXing3.dll和QZXing3.lib

1.3.配置

在项目中添加上面编译生成的库:

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/QZXing/ -lQZXing3
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/QZXing/ -lQZXing3
else:unix: LIBS += -L$$PWD/QZXing/ -lQZXing3

INCLUDEPATH += $$PWD/QZXing
DEPENDPATH += $$PWD/QZXing

# 使用生成二维码功能需要加这一句
DEFINES += ENABLE_ENCODER_GENERIC
1.4.生成二维码
#include "QZXingWidget.h"
#include "ui_QZXingWidget.h"
#include "./include/QZXing.h"

QZXingWidget::QZXingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QZXingWidget)
{
    ui->setupUi(this);
    setWindowTitle(QStringLiteral("QZXing例子"));
}

QZXingWidget::~QZXingWidget()
{
    delete ui;
}

void QZXingWidget::on_pushButton_encode_clicked()
{
    QString text = ui->textEdit->toPlainText();
    if(text.isEmpty())
        return;
    QImage img = QZXing::encodeData(text
                 ,QZXing::EncoderFormat::EncoderFormat_QR_CODE
                 ,QSize(200,200)
                 ,QZXing::EncodeErrorCorrectionLevel::EncodeErrorCorrectionLevel_H
                 ,true
                 ,false);
    ui->label->setPixmap(QPixmap::fromImage(img));
}
1.5.识别二维码
void QZXingWidget::on_pushButton_decode_clicked()
{
    QImage img;
    QString path= qApp->applicationDirPath()+"//file.png";
    img.load(path);
    if(img.isNull())
    {
        qDebug()<<"图片为空";
        return;
    }
    QZXing decode;
    decode.setDecoder(QZXing::DecoderFormat_QR_CODE);
    decode.setSourceFilterType(QZXing::TryHarderBehaviour_ThoroughScanning|QZXing::TryHarderBehaviour_Rotate);
    decode.setSourceFilterType(QZXing::SourceFilter_ImageNormal);
    QString info = decode.decodeImage(img);
    ui->textEdit->setText(info);
}
2.QRenCode使用
2.1.源码下载

项目链接(github):GitHub - fukuchi/libqrencode: A fast and compact QR Code encoding library

官方的稳定版本,参见:libqrencode

2.2.添加到自己的项目中

解压源码的压缩包,然后将有用的.h和.cpp文件添加到项目的子文件夹中,同时将config.h.in文件修改为config.h添加到项目中

2.3.修改移植文件

在QT的.pro文件中添加全局宏定义:

DEFINES += HAVE_CONFIG_H

在config.h文件末尾重新定义宏:

#define MAJOR_VERSION 1
#define MICRO_VERSION 1
#define MINOR_VERSION 1
#define VERSION 1
2.4.绘制二维码
void WinGenerateOrder::GenerateQRcode(QString str)
{
    QRcode *qrcode; //二维码数据
    //将QString转化为const char * |2-QR码版本为2 | QR_ECLEVEL_Q 容错等级 |QR_MODE_8 八字节数据 |1-区分大小写
    qrcode = QRcode_encodeString(str.toStdString().c_str(), 2, QR_ECLEVEL_Q, QR_MODE_8, 1);
    qint32 temp_width=400;
    qint32 temp_height=400;
    qint32 qrcode_width = qrcode->width > 0 ? qrcode->width : 1;    //生成的二维码宽高(正方形的宽度=高度)
    double scale_x = (double)temp_width / (double)qrcode_width; //二维码图片的缩放比例
    double scale_y =(double) temp_height /(double) qrcode_width;
    QImage mainimg=QImage(temp_width,temp_height,QImage::Format_ARGB32);
    QPainter painter(&mainimg);
    QColor background(Qt::white);
    painter.setBrush(background);
    painter.setPen(Qt::NoPen);
    painter.drawRect(0, 0, temp_width, temp_height);
    QColor foreground(Qt::black);
    painter.setBrush(foreground);
    for( qint32 y = 0; y < qrcode_width; y ++)//qrcode->data是一个存了qrcode_width*qrcode_width个数据的一维数组
    {                                         //这里是把这个一维数组以每行qrcode_width个数据,以二维数组的形式表现出来
        for(qint32 x = 0; x < qrcode_width; x++)
        {
            unsigned char b = qrcode->data[y * qrcode_width + x];
            if(b & 0x01)
            {//根据二维码中黑白点(1/0),在QLabel上以缩放比例画出二维码
                QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
                painter.drawRects(&r, 1);
            }
        }
    }
    QPixmap mainmap=QPixmap::fromImage(mainimg);
    mainmap.save(QString("%1/%2.png").arg(order_path).arg(str));
}

猜你喜欢

转载自blog.csdn.net/weixin_55238862/article/details/135397321