Qt实现二维码生成和识别

一、简介

QZxing开源库: 生成和识别条码和二维码

下载地址:https://gitcode.com/mirrors/ftylitak/qzxing/tree/master

二、编译与使用

1.下载并解压,解压之后如图所示
2.编译

打开src目录下的QZXing.pro,选择合适的编译器进行编译
最后生成库libQZXing3.a和QZXing3.dll 库

三、编写demo测试二维码生成和识别

除了上述两个库,还需要源码中的两个头文件
1、在pro包含库和头文件

c 复制代码
INCLUDEPATH +=$$PWD/qzxing/include
LIBS +=$$PWD/qzxing/libQZXing3.a

# 使用生成二维码功能需要加这一句
DEFINES += ENABLE_ENCODER_GENERIC

2、生成二维码程序

cpp 复制代码
QString text = ui->lineEdit->text();
    if(text.isEmpty())
        return;
    QImage img = QZXing::encodeData(text
                                    ,QZXing::EncoderFormat::EncoderFormat_QR_CODE
                                    ,QSize(200,200)
                                    ,QZXing::EncodeErrorCorrectionLevel::EncodeErrorCorrectionLevel_H
                                    ,true
                                    ,false);
    //图片大小设置,与label大小适配
    //img = img.scaled(ui->label->width(), ui->label->height()); //图片适应label,有点变形,太丑

    ui->label->setPixmap(QPixmap::fromImage(img));

3、识别二维码

cpp 复制代码
QImage img;
    //QString path= qApp->applicationDirPath()+"//file.png";

    //第一个参数:标准文件对话框的父窗口;第二个参数:标准文件对话框的标题;第三个参数:指定默认的目录;第四个参数:文件过滤器
    //QString path = QFileDialog::getOpenFileName(this,"open file dialog","/","png files(*.png);;jpg files(*.jpg)");
    //img.load(path);

    img = ui->label->pixmap()->toImage();

    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->lineEdit_2->setText(info);

4、注意事项

一定要将QZXing3.dll放在和exe同意目录,否则会出现编译通过,无法运行的问题

相关推荐
读书札记20224 小时前
Qt Creator使用cmake添加资源文件(.qrc)
qt
tycooncool5 小时前
QT数据库(三):QSqlQuery使用
数据库·qt·oracle
顶点多余5 小时前
QT-设计师模式基本知识
开发语言·qt
sunriver200010 小时前
【Qt】Qt 6.9.1 循环编译问题
qt·循环编译问题
古城码农11 小时前
Windows平台MSVC编译的FFmpeg库
开发语言·qt
sunriver200012 小时前
【Qt】Qt Creator 19.0.0 编译输出窗口乱码问题
qt·乱码·creator 19.0.0·娉ㄦ
冉佳驹1 天前
Qt【第五篇】——— QMainWindow 核心组件与对话框开发详解
qt·qdialog·qtoolbar·qstatusbar·qdockwidget·qmenubar
cpp_learners1 天前
银河麒麟V10+飞腾D2000/8处理器+在线源码编译安装Firefox78.0指南
qt·firefox·麒麟
Frank_refuel1 天前
QT->信号与槽详解上(概述、使用、自定义、连接方式、其他说明)
开发语言·qt
不想看见4041 天前
在AI时代下,刷LeetCode题的价值与意义
开发语言·c++·qt