Skip to content
Snippets Groups Projects
Verified Commit 323ff16d authored by Burak Hançerli's avatar Burak Hançerli :headphones:
Browse files

enh: fix qr scanner issues and speed it up

parent 1b4446e0
No related branches found
No related tags found
1 merge request!56QDS-13458 Small fixes
Pipeline #76313 passed
...@@ -217,9 +217,12 @@ void Backend::scanQrCode() ...@@ -217,9 +217,12 @@ void Backend::scanQrCode()
m_qrScanner.reset(new QrScanner); m_qrScanner.reset(new QrScanner);
connect(m_qrScanner.get(), &QrScanner::qrCodeScanned, this, [&](const QString &qrCode) { connect(m_qrScanner.get(), &QrScanner::qrCodeScanned, this, [&](const QString &qrCode) {
qDebug() << "QR code scanned:" << qrCode; qDebug() << "QR code scanned:" << qrCode;
parseDesignViewerUrl(QUrl(qrCode));
m_qrScanner.reset(); m_qrScanner.reset();
parseDesignViewerUrl(QUrl(qrCode));
}); });
connect(m_qrScanner.get(), &QrScanner::windowClosed, this, [&]() { m_qrScanner.reset(); });
m_qrScanner->scanQrCode(); m_qrScanner->scanQrCode();
} }
......
...@@ -42,8 +42,8 @@ public: ...@@ -42,8 +42,8 @@ public:
private: private:
// Other members // Other members
std::unique_ptr<ProjectManager> m_projectManager; QScopedPointer<ProjectManager> m_projectManager;
std::unique_ptr<QrScanner> m_qrScanner; QScopedPointer<QrScanner> m_qrScanner;
// DS Connector // DS Connector
QScopedPointer<DesignStudioManager> m_dsManager; QScopedPointer<DesignStudioManager> m_dsManager;
......
...@@ -82,65 +82,54 @@ void QrScanner::openCamera() ...@@ -82,65 +82,54 @@ void QrScanner::openCamera()
m_captureSession.reset(new QMediaCaptureSession(this)); m_captureSession.reset(new QMediaCaptureSession(this));
m_captureSession->setCamera(new QCamera(this)); m_captureSession->setCamera(new QCamera(this));
m_captureSession->setVideoOutput(new CustomVideoWidget); m_captureSession->setVideoOutput(new CustomVideoWidget);
m_captureSession->camera()->setFocusMode(QCamera::FocusModeAuto); m_captureSession->camera()->setFocusMode(QCamera::FocusModeAuto);
CustomVideoWidget *videoWidget = qobject_cast<CustomVideoWidget *>( m_videoWidget = qobject_cast<CustomVideoWidget *>(m_captureSession->videoOutput());
m_captureSession->videoOutput()); m_processing = 0;
ZXing::ReaderOptions readerOptions;
readerOptions.setTryRotate(false);
readerOptions.setTryHarder(false);
readerOptions.setTryInvert(false);
readerOptions.setDownscaleFactor(2);
connect(m_captureSession->videoSink(), connect(m_captureSession->videoSink(),
&QVideoSink::videoFrameChanged, &QVideoSink::videoFrameChanged,
this, this,
[&](const QVideoFrame &frame) { [&, readerOptions](const QVideoFrame &frame) {
static QAtomicInt i = 0;
static QAtomicInt running = 0;
if (i++ < 20 || running > 0) {
return;
}
i = 0;
if (!frame.isValid()) if (!frame.isValid())
return; return;
if (!m_processing.testAndSetOrdered(0, 1)) {
return; // Another task is already running
}
QtConcurrent::run([=] { QtConcurrent::run([=] {
if (!m_captureSession) if (!m_captureSession)
return QList<Result>(); return QList<Result>();
// lock the thread so that only one barcode can be read at a time
running++;
QElapsedTimer timer; QElapsedTimer timer;
timer.start(); timer.start();
QList<Result> results = ReadBarcodes(frame.toImage()); QList<Result> results = ReadBarcodes(frame.toImage(), readerOptions);
qDebug() << "Barcode detection took" << timer.elapsed() << "ms"; qDebug() << "Barcode detection took" << timer.elapsed() << "ms";
if (!results.size())
m_processing = 0;
return results; return results;
}).then([=](const QList<Result> &results) { }).then([=](const QList<Result> &results) {
if (results.isEmpty() || !m_captureSession) if (results.isEmpty() || !m_captureSession)
return; return;
qDebug() << "Stopping camera";
qobject_cast<CustomVideoWidget *>(m_captureSession->videoOutput())->close();
qDebug() << "Camera stopped";
Result result = results.first(); Result result = results.first();
qDebug() << "Text: " << result.text(); qDebug() << "Text: " << result.text();
qDebug() << "Format: " << result.format(); qDebug() << "Format: " << result.format();
qDebug() << "Content:" << result.contentType(); qDebug() << "Content:" << result.contentType();
qDebug() << "Position:" << result.position().bottomLeft()
<< result.position().bottomRight() << result.position().topLeft()
<< result.position().topRight();
// we have to use invokeMethod because we are in a different thread
// then where the serviceConnector is created
// QMetaObject::invokeMethod(this,
// "parseDesignViewerUrl",
// Qt::QueuedConnection,
// Q_ARG(QUrl, result.text()));
emit qrCodeScanned(result.text()); emit qrCodeScanned(result.text());
// release the lock so that another barcode can be read
running--;
}); });
}); });
...@@ -155,15 +144,12 @@ void QrScanner::openCamera() ...@@ -155,15 +144,12 @@ void QrScanner::openCamera()
}); });
// stop camera when video widget is closed // stop camera when video widget is closed
connect(videoWidget, &CustomVideoWidget::closed, this, [&] { connect(m_videoWidget, &CustomVideoWidget::closed, this, [&] {
qDebug() << "Video widget closed. Clearing capture session"; qDebug() << "Video widget closed.";
m_captureSession->camera()->stop(); emit windowClosed();
m_captureSession->camera()->deleteLater();
m_captureSession->videoOutput()->deleteLater();
m_captureSession.clear();
}); });
videoWidget->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint); m_videoWidget->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint);
videoWidget->show(); m_videoWidget->show();
m_captureSession->camera()->start(); m_captureSession->camera()->start();
} }
...@@ -25,7 +25,9 @@ ...@@ -25,7 +25,9 @@
#pragma once #pragma once
#include <QList>
#include <QMediaCaptureSession> #include <QMediaCaptureSession>
#include <QRect>
#include <QVideoWidget> #include <QVideoWidget>
class CustomVideoWidget : public QVideoWidget class CustomVideoWidget : public QVideoWidget
...@@ -35,8 +37,8 @@ public: ...@@ -35,8 +37,8 @@ public:
explicit CustomVideoWidget(QWidget *parent = nullptr) explicit CustomVideoWidget(QWidget *parent = nullptr)
: QVideoWidget(parent) : QVideoWidget(parent)
{} {}
~CustomVideoWidget() override {}
~CustomVideoWidget() override {}
void closeEvent(QCloseEvent *) override { emit closed(); } void closeEvent(QCloseEvent *) override { emit closed(); }
signals: signals:
...@@ -52,9 +54,12 @@ public: ...@@ -52,9 +54,12 @@ public:
private: private:
QSharedPointer<QMediaCaptureSession> m_captureSession; QSharedPointer<QMediaCaptureSession> m_captureSession;
CustomVideoWidget *m_videoWidget;
QAtomicInt m_processing;
void openCamera(); void openCamera();
signals: signals:
void qrCodeScanned(const QString &qrCode); void qrCodeScanned(const QString &qrCode);
void windowClosed();
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment