Skip to content

Prevent image buffer exhaustion on Android using Qt #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions example/ZXingQtReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QAbstractVideoFilter>
#else
#include <QThreadPool>
#include <QVideoFrame>
#include <QVideoSink>
#endif
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -403,6 +414,7 @@ public slots:
#else
private:
QVideoSink *_sink = nullptr;
QThreadPool _pool;

public:
void setVideoSink(QVideoSink* sink) {
Expand All @@ -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

};
Expand Down
Loading
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