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同意目录,否则会出现编译通过,无法运行的问题

相关推荐
誰能久伴不乏3 小时前
用 UDP 做一条“可靠“的控制通道
c++·qt·网络协议·udp
Bczheng17 小时前
VS2022 安装MFC和QT
c++·qt·mfc
会编程的小孩8 小时前
8.19号函数
qt
蓝祁子yoky1 天前
Qt学习日记 Day15:网络编程(二)- UDP基础
网络·qt·学习
读书札记20221 天前
Qt UTF-8和UTF-8(BOM)
开发语言·qt
ShineWinsu1 天前
对于Qt:QProgressBar常见属性的解析
开发语言·c++·qt·进度条·gui·定时器·qprogressbar
ShineWinsu1 天前
对于Qt:QLCDNumber常见属性以及QTimer的解析
服务器·数据库·c++·qt·gui·qtimer·qlcdnumber
听风吹等浪起2 天前
第26章:ViT+InceptionDW+FocalLoss改进|CT骨折影像二分类完整Pytorch实现(含PyQt5推理GUI)
pytorch·qt·分类
ShineWinsu2 天前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup
sycmancia2 天前
Qml——Image和AnimatedImage使用
qt