diff --git a/example/ZXingQtReader.h b/example/ZXingQtReader.h index 629f44423b..b32e37fe43 100644 --- a/example/ZXingQtReader.h +++ b/example/ZXingQtReader.h @@ -16,6 +16,7 @@ #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #include #else +#include #include #include #endif @@ -333,7 +334,16 @@ class BarcodeReader : public QObject, private ReaderOptions #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) BarcodeReader(QObject* parent = nullptr) : QAbstractVideoFilter(parent) {} #else - BarcodeReader(QObject* parent = nullptr) : QObject(parent) {} + BarcodeReader(QObject* parent = nullptr) : QObject(parent) + { + _pool.setMaxThreadCount(1); + } + ~BarcodeReader() + { + _pool.setMaxThreadCount(0); + _pool.waitForDone(-1); + } + #endif // TODO: find out how to properly expose QFlags to QML @@ -373,10 +383,11 @@ class BarcodeReader : public QObject, private ReaderOptions ZQ_PROPERTY(bool, isPure, setIsPure) // For debugging/development - int runTime = 0; + QAtomicInt runTime = 0; Q_PROPERTY(int runTime MEMBER runTime) public slots: + // Function should be thread safe, as it may be called from a separate thread. ZXingQt::Barcode process(const QVideoFrame& image) { QElapsedTimer t; @@ -403,6 +414,7 @@ public slots: #else private: QVideoSink *_sink = nullptr; + QThreadPool _pool; public: void setVideoSink(QVideoSink* sink) { @@ -413,9 +425,29 @@ public slots: disconnect(_sink, nullptr, this, nullptr); _sink = sink; - connect(_sink, &QVideoSink::videoFrameChanged, this, &BarcodeReader::process); + connect(_sink, &QVideoSink::videoFrameChanged, this, &BarcodeReader::onVideoFrameChanged, Qt::DirectConnection); + } + void onVideoFrameChanged(const QVideoFrame& frame) + { + if (_pool.activeThreadCount() >= _pool.maxThreadCount()) + return; // we are busy => skip the frame + + _pool.start([this, frame](){process(frame);}); } Q_PROPERTY(QVideoSink* videoSink MEMBER _sink WRITE setVideoSink) + Q_PROPERTY(int maxThreadCount READ maxThreadCount WRITE setMaxThreadCount) + int maxThreadCount () const + { + return _pool.maxThreadCount(); + } + void setMaxThreadCount (int maxThreadCount) + { + if (_pool.maxThreadCount() != maxThreadCount) { + _pool.setMaxThreadCount(maxThreadCount); + emit maxThreadCountChanged(); + } + } + Q_SIGNAL void maxThreadCountChanged(); #endif }; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy