Qt实现二维码生成和识别 - //img

📅 2026-11-30 👤 拼多多助力营销技巧 📂 pdd帮助力 👁️ 434 次阅读

一、简介

QZxing开源库: 生成和识别条码和二维码 下载地址:https://gitcode.com/mirrors/ftylitak/qzxing/tree/master

二、编译与使用

1.下载并解压,解压之后如图所示 2.编译 打开src目录下的QZXing.pro,选择合适的编译器进行编译 最后生成库libQZXing3.a和QZXing3.dll 库 三、编写demo测试二维码生成和识别 除了上述两个库,还需要源码中的两个头文件 1、在pro包含库和头文件

INCLUDEPATH +=$$PWD/qzxing/include

LIBS +=$$PWD/qzxing/libQZXing3.a

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

DEFINES += ENABLE_ENCODER_GENERIC

2、生成二维码程序

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、识别二维码

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

好文推荐

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧