Qt中QZXing 的编译与使用
本文主要介绍了Qt中QZXing 的编译与使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
目录
0.前言
1.编译
2.二维码生成
3.二维码识别
0.前言
zxing 是一个开源的一维/二维条码图像处理库,当前版本为 Java 语言开发:
https://github.com/zxing/zxing
QZXing 是 ZXing 的 Qt 移植版本,同样还有 cpp 等语言和框架的移植版本。从 QZXing 的文档可以看到,只有 QR Code 二维码支持编码,其他都只支持解码。
https://github.com/ftylitak/qzxing
1.编译
下载源码后可以用 CMake 或者直接打开 pro 进行构建。网上有人编译失败,但是我用 Qt5.15 + VS2019 编译 QZXing3.3.0 并没有出现编译问题。 编译复制头文件和库文件到我们的工程。
测试工程(Qt5 + MSVC2019):
https://github.com/gongjianbo/MyTestCode2021/tree/master/Qt/QtQZXingVS2019
2.二维码生成
先打开编码功能,添加一个宏:
1 | DEFINES += ENABLE_ENCODER_GENERIC |
然后从 QZXing README 看简单的生成示例:
1 2 3 4 5 6 7 8 9 | #include "QZXing.h" int main() { QString data = "text to be encoded" ; QImage barcode = QZXing::encodeData(data); //QImage barcode = QZXing::encodeData(data, QZXing::EncoderFormat_QR_CODE, // QSize(240, 240), QZXing::EncodeErrorCorrectionLevel_H); } |
接口声明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #ifdef ENABLE_ENCODER_GENERIC //二维码编码接口,目前仅支持QR Code码 //QZXingEncoderConfig是个结构体,成员如下一个重载接口的参数 static QImage encodeData( const QString &data, const QZXingEncoderConfig &encoderConfig); //二维码编码接口,目前仅支持QR Code码 //encoderFormat 编码格式枚举 //encoderImageSize 生成二维码的大小 //errorCorrectionLevel 纠错等级 //border =true会有一圈白边,感觉没啥用 //transparent =true会半透明,感觉没啥用 static QImage encodeData( const QString& data, const EncoderFormat encoderFormat = EncoderFormat_QR_CODE, const QSize encoderImageSize = QSize(240, 240), const EncodeErrorCorrectionLevel errorCorrectionLevel = EncodeErrorCorrectionLevel_L, const bool border = false , const bool transparent = false ); #endif // ENABLE_ENCODER_GENERIC |
由于是使用 Qt 封装的,所以不用像其他库那样还要自己根据矩阵结果绘制 QImage。
3.二维码识别
文档里着重讲了解码的使用,并且封装了相应的 QML 组件。
C++ 使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include "QZXing.h" int main() { QImage imageToDecode( "file.png" ); QZXing decoder; //必要设置 decoder.setDecoder(QZXing::DecoderFormat_QR_CODE | QZXing::DecoderFormat_EAN_13 ); //可选设置 //decoder.setSourceFilterType(QZXing::SourceFilter_ImageNormal | QZXing::SourceFilter_ImageInverted); decoder.setSourceFilterType(QZXing::SourceFilter_ImageNormal); decoder.setTryHarderBehaviour(QZXing::TryHarderBehaviour_ThoroughScanning | QZXing::TryHarderBehaviour_Rotate); //解码 QString result = decoder.decodeImage(imageToDecode); } |
QML 使用:
1 2 3 4 5 6 7 8 | #include "QZXing.h" int main() { ... QZXing::registerQMLTypes(); ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import QtQuick 2.0 import QZXing 3.3 Item{ function decode(preview) { imageToDecode.source = preview decoder.decodeImageQML(imageToDecode); } Image{ id:imageToDecode } QZXing{ id: decoder enabledDecoders: QZXing.DecoderFormat_QR_CODE / //可选设置 tryHarderType: QZXing.TryHarderBehaviour_ThoroughScanning | QZXing.TryHarderBehaviour_Rotate imageSourceFilter: QZXing.SourceFilter_ImageNormal //| QZXing.SourceFilter_ImageInverted / onDecodingStarted: console. log ( "Decoding of image started..." ) onTagFound: console. log ( "Barcode data: " + tag) onDecodingFinished: console. log ( "Decoding finished " + (succeeded== true ? "successfully" : "unsuccessfully" ) ) } } |
参数较多,如果有疑问可以搜 zxing 的文档。经测试,该库是可以做一些简单的图像识别,可以识别截图中的二维码,但是对拍照的二维码识别不了,所以要直接识别还是得用上图像处理库。
到此这篇关于Qt中QZXing 的编译与使用的文章就介绍到这了
原文链接:https://blog.csdn.net/gongjianbo1992/article/details/122463817