Qt中QZXing 的編譯與使用
0.前言
zxing 是一個開源的一維/二維條碼圖像處理庫,當前版本為 Java 語言開發(fā):
https://github.com/zxing/zxing
QZXing 是 ZXing 的 Qt 移植版本,同樣還有 cpp 等語言和框架的移植版本。從 QZXing 的文檔可以看到,只有 QR Code 二維碼支持編碼,其他都只支持解碼。
https://github.com/ftylitak/qzxing
1.編譯
下載源碼后可以用 CMake 或者直接打開 pro 進行構(gòu)建。網(wǎng)上有人編譯失敗,但是我用 Qt5.15 + VS2019 編譯 QZXing3.3.0 并沒有出現(xiàn)編譯問題。 編譯復(fù)制頭文件和庫文件到我們的工程。


測試工程(Qt5 + MSVC2019):
https://github.com/gongjianbo/MyTestCode2021/tree/master/Qt/QtQZXingVS2019

2.二維碼生成
先打開編碼功能,添加一個宏:
DEFINES += ENABLE_ENCODER_GENERIC
然后從 QZXing README 看簡單的生成示例:
#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);
}接口聲明:
#ifdef ENABLE_ENCODER_GENERIC
//二維碼編碼接口,目前僅支持QR Code碼
//QZXingEncoderConfig是個結(jié)構(gòu)體,成員如下一個重載接口的參數(shù)
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 封裝的,所以不用像其他庫那樣還要自己根據(jù)矩陣結(jié)果繪制 QImage。
3.二維碼識別
文檔里著重講了解碼的使用,并且封裝了相應(yīng)的 QML 組件。
C++ 使用:
#include "QZXing.h"
int main()
{
QImage imageToDecode("file.png");
QZXing decoder;
//必要設(shè)置
decoder.setDecoder(QZXing::DecoderFormat_QR_CODE | QZXing::DecoderFormat_EAN_13 );
//可選設(shè)置
//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 使用:
#include "QZXing.h"
int main()
{
...
QZXing::registerQMLTypes();
...
}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
/
//可選設(shè)置
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") )
}
}參數(shù)較多,如果有疑問可以搜 zxing 的文檔。經(jīng)測試,該庫是可以做一些簡單的圖像識別,可以識別截圖中的二維碼,但是對拍照的二維碼識別不了,所以要直接識別還是得用上圖像處理庫。
到此這篇關(guān)于Qt中QZXing 的編譯與使用的文章就介紹到這了,更多相關(guān)QZXing 編譯與使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++ Windows SAPI實現(xiàn)文字轉(zhuǎn)語音功能
本文通過封裝Windows SAPI(Speech Application Programming Interface),提供了一個現(xiàn)代化的C++接口實現(xiàn)文字轉(zhuǎn)語音功能,這篇文章重點給大家介紹C/C++ Windows SAPI自實現(xiàn)文字轉(zhuǎn)語音功能,感興趣的朋友一起看看吧2025-02-02

