QZxing+qt 实现条形码二维码识别 二维码生成

由于项目需求使用,需要识别条形码和二维码以及生成二维码,最后选择使用QZxing开源库!

QZxing源码下载地址:https://github.com/ftylitak/qzxing

源码在手,怎么用自己说了算,如果是qt工程建议直接添加源码使用,工程里面有个pri直接包含到工程里面就可以直接使用了,还可以编译成dll供其他项目调用,我这边测试demo是直接用的源码

Qt使用很简单,创建一个空的工程,在pro里面添加一句就可以了

cpp 复制代码
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QZxing
TEMPLATE = app

DESTDIR = $${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.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        widget.cpp

HEADERS += \
        widget.h

FORMS += \
        widget.ui

include(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
cpp 复制代码
#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里面有支持的条码类型,如果是其他类型的条形码失败会失败。

相关推荐
Felix_One2 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit5 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理10 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12310 天前
matlab画图工具
开发语言·matlab
dustcell.10 天前
haproxy七层代理
java·开发语言·前端
norlan_jame10 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone10 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ40220549610 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月10 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_5312371710 天前
C语言-数组练习进阶
c语言·开发语言·算法