Qt implements QR code generation and recognition

        QR code (full name: Quick Response Matrix Code; English: Quick Response Code) is a type of two-dimensional barcode that was invented by the Japanese company DENSO WAVE in 1994. QR comes from the English abbreviation of Quick Response, which means quick response, because the inventor hopes that the QR code can allow its content to be decoded quickly. QR codes use four standardized encoding modes (numeric, alphanumeric, byte (binary) and Chinese characters) to store data. QR code is most common in Japan and is currently the most popular two-dimensional space barcode in Japan. QR codes can store more data than ordinary barcodes, and they do not need to be aligned with the scanner when scanning like ordinary barcodes. Therefore, its application scope has expanded to include product tracking, item identification, document management, marketing, etc.

1.QZXing use
1.1. Source code download

Link address: GitHub - zxing/zxing: ZXing ("Zebra Crossing") barcode scanning library for Java, Android

Download address of QZXing: GitHub - ftylitak/qzxing: Qt/QML wrapper library for the ZXing library. 1D/2D barcode image processing library

1.2.Compile

Use Qt to open the pro file in the src folder and compile it. Find QZXing3.dll and libQZXing3.a in the corresponding folders; the msvc compiler compiles QZXing3.dll and QZXing3.lib

1.3.Configuration

Add the library compiled above to the project:

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. Generate QR code
#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. Identify QR code
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 usage
2.1. Source code download

Project link (github): GitHub - fukuchi/libqrencode: A fast and compact QR Code encoding library

For the official stable version, see: libqrencode

2.2. Add to your own project

Unzip the source code compressed package, then add useful .h and .cpp files to the subfolder of the project, and modify the config.h.in file to config.h and add it to the project

2.3. Modify the migration file

Add global macro definition in QT's .pro file :

DEFINES += HAVE_CONFIG_H

Redefine the macro at the end of the config.h file:

#define MAJOR_VERSION 1
#define MICRO_VERSION 1
#define MINOR_VERSION 1
#define VERSION 1
2.4. Draw QR code
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));
}

Guess you like

Origin blog.csdn.net/weixin_55238862/article/details/135397321