溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Qt中QZXing如何編譯使用

發布時間:2022-01-13 14:44:21 來源:億速云 閱讀:186 作者:小新 欄目:開發技術

小編給大家分享一下Qt中QZXing如何編譯使用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1.編譯

下載源碼后可以用 CMake 或者直接打開 pro 進行構建。網上有人編譯失敗,但是我用 Qt5.15 + VS2019 編譯 QZXing3.3.0 并沒有出現編譯問題。 編譯復制頭文件和庫文件到我們的工程。

Qt中QZXing如何編譯使用

Qt中QZXing如何編譯使用

 測試工程(Qt5 + MSVC2019):

https://github.com/gongjianbo/MyTestCode2021/tree/master/Qt/QtQZXingVS2019

Qt中QZXing如何編譯使用

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是個結構體,成員如下一個重載接口的參數
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++ 使用:

#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 使用:

#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
 
        /
        //可選設置
        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如何編譯使用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女