Qt中QZXing 的編譯與使用
0.前言
zxing 是一個(gè)開(kāi)源的一維/二維條碼圖像處理庫(kù),當(dāng)前版本為 Java 語(yǔ)言開(kāi)發(fā):
https://github.com/zxing/zxing
QZXing 是 ZXing 的 Qt 移植版本,同樣還有 cpp 等語(yǔ)言和框架的移植版本。從 QZXing 的文檔可以看到,只有 QR Code 二維碼支持編碼,其他都只支持解碼。
https://github.com/ftylitak/qzxing
1.編譯
下載源碼后可以用 CMake 或者直接打開(kāi) pro 進(jìn)行構(gòu)建。網(wǎng)上有人編譯失敗,但是我用 Qt5.15 + VS2019 編譯 QZXing3.3.0 并沒(méi)有出現(xiàn)編譯問(wèn)題。 編譯復(fù)制頭文件和庫(kù)文件到我們的工程。
測(cè)試工程(Qt5 + MSVC2019):
https://github.com/gongjianbo/MyTestCode2021/tree/master/Qt/QtQZXingVS2019
2.二維碼生成
先打開(kāi)編碼功能,添加一個(gè)宏:
DEFINES += ENABLE_ENCODER_GENERIC
然后從 QZXing README 看簡(jiǎn)單的生成示例:
#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是個(gè)結(jié)構(gòu)體,成員如下一個(gè)重載接口的參數(shù) static QImage encodeData(const QString &data, const QZXingEncoderConfig &encoderConfig); //二維碼編碼接口,目前僅支持QR Code碼 //encoderFormat 編碼格式枚舉 //encoderImageSize 生成二維碼的大小 //errorCorrectionLevel 糾錯(cuò)等級(jí) //border =true會(huì)有一圈白邊,感覺(jué)沒(méi)啥用 //transparent =true會(huì)半透明,感覺(jué)沒(méi)啥用 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 封裝的,所以不用像其他庫(kù)那樣還要自己根據(jù)矩陣結(jié)果繪制 QImage。
3.二維碼識(shí)別
文檔里著重講了解碼的使用,并且封裝了相應(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ù)較多,如果有疑問(wèn)可以搜 zxing 的文檔。經(jīng)測(cè)試,該庫(kù)是可以做一些簡(jiǎn)單的圖像識(shí)別,可以識(shí)別截圖中的二維碼,但是對(duì)拍照的二維碼識(shí)別不了,所以要直接識(shí)別還是得用上圖像處理庫(kù)。
到此這篇關(guān)于Qt中QZXing 的編譯與使用的文章就介紹到這了,更多相關(guān)QZXing 編譯與使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?LeetCode1769移動(dòng)所有球到每個(gè)盒子最小操作數(shù)示例
這篇文章主要為大家介紹了C++?LeetCode1769移動(dòng)所有球到每個(gè)盒子所需最小操作數(shù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12C++設(shè)計(jì)模式之裝飾模式(Decorator)
這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之裝飾模式Decorator的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03C語(yǔ)言中的函數(shù)指針學(xué)習(xí)筆記
這篇文章主要介紹了C語(yǔ)言中的函數(shù)指針的一些學(xué)習(xí)知識(shí)點(diǎn)記錄,文中作者整理了一些比較interesting的函數(shù)指針用法,需要的朋友可以參考下2016-04-04cmake跨平臺(tái)構(gòu)建工具的學(xué)習(xí)筆記
CMake是一個(gè)跨平臺(tái)的安裝/編譯工具,通過(guò)CMake我們可以通過(guò)簡(jiǎn)單的語(yǔ)句來(lái)描述所有平臺(tái)的安裝/編譯過(guò)程,下面這篇文章主要給大家介紹了關(guān)于cmake跨平臺(tái)構(gòu)建工具的相關(guān)資料,需要的朋友可以參考下2023-02-02常用Hash算法(C語(yǔ)言的簡(jiǎn)單實(shí)現(xiàn))
下面小編就為大家?guī)?lái)一篇常用Hash算法(C語(yǔ)言的簡(jiǎn)單實(shí)現(xiàn))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09C語(yǔ)言利用UDP實(shí)現(xiàn)群聊聊天室的示例代碼
UDP是一個(gè)輕量級(jí)、不可靠、面向數(shù)據(jù)報(bào)的、無(wú)連接的傳輸層協(xié)議,多用于可靠性要求不嚴(yán)格,不是非常重要的傳輸,如直播、視頻會(huì)議等等。本文將利用UDP實(shí)現(xiàn)簡(jiǎn)單的群聊聊天室,感興趣的可以了解一下2022-08-08C++設(shè)計(jì)模式之享元模式(Flyweight)
這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之享元模式Flyweight,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04opencv+arduino實(shí)現(xiàn)物體點(diǎn)追蹤效果
這篇文章主要為大家詳細(xì)介紹了opencv+arduino實(shí)現(xiàn)物體點(diǎn)追蹤效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01