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

相关推荐
用户805533698039 小时前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner9 小时前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz5 天前
QML Hello World 入门示例
qt
xcyxiner8 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner9 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner9 天前
DicomViewer (添加模型类)3
qt
xcyxiner10 天前
DicomViewer (目录调整) 2
qt
xcyxiner10 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能12 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G12 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt