本文主要是介绍QZxing+qt 实现条形码二维码识别 二维码生成,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
由于项目需求使用,需要识别条形码和二维码以及生成二维码,最后选择使用QZxing开源库!
QZxing源码下载地址:https://github.com/ftylitak/qzxing
源码在手,怎么用自己说了算,如果是qt工程建议直接添加源码使用,工程里面有个pri直接包含到工程里面就可以直接使用了,还可以编译成dll供其他项目调用,我这边测试demo是直接用的源码
Qt使用很简单,创建一个空的工程,在pro里面添加一句就可以了
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = QZxing
TEMPLATE = appDESTDIR = $${PWD}/bin# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0CONFIG += c++11SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.uiinclude(qzxing-master/src/QZXing.pri)# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include "widget.h"
#include "ui_widget.h"
#include "QZXing.h"
#include <QDebug>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked()
{QImage img1("1.png");QImage img2("2.png");QImage img3("3.png");QImage img4("4.png");QImage img5("5.png");QImage img6("6.png");QImage img7("7.png");QImage img8("8.png");qDebug() << "1-->" << QZXing::instance()->decodeImage(img1);qDebug() << "2-->" << QZXing::instance()->decodeImage(img2);qDebug() << "3-->" << QZXing::instance()->decodeImage(img3);qDebug() << "4-->" << QZXing::instance()->decodeImage(img4);qDebug() << "5-->" << QZXing::instance()->decodeImage(img5);qDebug() << "6-->" << QZXing::instance()->decodeImage(img6);qDebug() << "7-->" << QZXing::instance()->decodeImage(img7);qDebug() << "8-->" << QZXing::instance()->decodeImage(img8);QImage qrImage = QZXing::encodeData(ui->lineEdit->text());if (!qrImage.isNull()) {// 将生成的二维码显示在界面上或保存为文件QPixmap pixmap = QPixmap::fromImage(qrImage);ui->label->setPixmap(pixmap);// display or save the QR code image} else {qDebug() << "Failed to generate QR code";}
}
使用就更简单了,导入头文件后,直接调用相关api就可以识别和生成条码二维码等:
测试的条形码二维码,是找了一个在线生码器直接生成的,条形码有很多种,需要根据QZxing readme里面有支持的条码类型,如果是其他类型的条形码失败会失败。
这篇关于QZxing+qt 实现条形码二维码识别 二维码生成的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!