您好,登錄后才能下訂單哦!
小編給大家分享一下Qt中QZXing如何編譯使用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
下載源碼后可以用 CMake 或者直接打開 pro 進(jìn)行構(gòu)建。網(wǎng)上有人編譯失敗,但是我用 Qt5.15 + VS2019 編譯 QZXing3.3.0 并沒有出現(xiàn)編譯問題。 編譯復(fù)制頭文件和庫文件到我們的工程。
測試工程(Qt5 + MSVC2019):
https://github.com/gongjianbo/MyTestCode2021/tree/master/Qt/QtQZXingVS2019
先打開編碼功能,添加一個(gè)宏:
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是個(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ì)有一圈白邊,感覺沒啥用 //transparent =true會(huì)半透明,感覺沒啥用 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。
文檔里著重講了解碼的使用,并且封裝了相應(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)測試,該庫是可以做一些簡單的圖像識(shí)別,可以識(shí)別截圖中的二維碼,但是對(duì)拍照的二維碼識(shí)別不了,所以要直接識(shí)別還是得用上圖像處理庫。
以上是“Qt中QZXing如何編譯使用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。