From 24e3e25cd4faf4c12bde8da16d60456d52520ccb Mon Sep 17 00:00:00 2001 From: kazord Date: Fri, 6 Apr 2018 02:32:01 +0200 Subject: [PATCH] screenshot from QT buffer --- src/scanner/AutoBarcodeScanner.cpp | 31 +++++++++++++++++++++++------- src/scanner/AutoBarcodeScanner.h | 3 +++ src/scanner/BarcodeDecoder.cpp | 4 ++++ src/scanner/BarcodeDecoder.h | 2 +- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/scanner/AutoBarcodeScanner.cpp b/src/scanner/AutoBarcodeScanner.cpp index 17b31ae..e151d1f 100644 --- a/src/scanner/AutoBarcodeScanner.cpp +++ b/src/scanner/AutoBarcodeScanner.cpp @@ -31,6 +31,8 @@ THE SOFTWARE. #include #include #include +#include +#include #include #include @@ -60,6 +62,15 @@ AutoBarcodeScanner::AutoBarcodeScanner(QObject * parent) createConnections(); m_timeoutTimer->setSingleShot(true); connect(m_timeoutTimer, SIGNAL(timeout()), this, SLOT(slotScanningTimeout())); + + QGuiApplication *app = qobject_cast(qApp); + + foreach (QWindow *win, app->allWindows()) { + QQuickWindow *quickWin = qobject_cast(win); + if (quickWin) { + m_mainWindow = quickWin; + } + } } AutoBarcodeScanner::~AutoBarcodeScanner() { @@ -231,17 +242,22 @@ void AutoBarcodeScanner::processDecode() { m_scanProcessMutex.unlock(); if (scanActive) { - createScreeshot(); - QImage screenshot(m_decoder->getCaptureLocation()); - saveDebugImage(screenshot, "debug_screenshot.jpg"); + QImage screenshot = m_mainWindow->grabWindow(); + //maybe + //saveDebugImage(screenshot, "debug_screenshot.jpg"); // crop the image - we need only the viewfinder QImage copy = screenshot.copy(m_viewFinderRect); - copy.save(m_decoder->getCaptureLocation()); - saveDebugImage(copy, "debug_cropped.jpg"); + //maybe + //copy.save(m_decoder->getCaptureLocation()); + //saveDebugImage(copy, "debug_cropped.jpg"); + + qDebug() << "decoding cropped screenshot ..."; - result = m_decoder->decodeBarcodeFromCache(); + result = m_decoder->decodeBarcodeFromImage(copy); + + copy.save(m_decoder->getCaptureLocation()); code = result["content"].toString(); if (code.isEmpty()) { @@ -249,11 +265,12 @@ void AutoBarcodeScanner::processDecode() { QTransform transform; transform.rotate(90); copy = copy.transformed(transform); + //maybe still do it in debug copy.save(m_decoder->getCaptureLocation()); saveDebugImage(copy, "debug_transformed.jpg"); qDebug() << "decoding rotated screenshot ..."; - result = m_decoder->decodeBarcodeFromCache(); + result = m_decoder->decodeBarcodeFromImage(copy); code = result["content"].toString(); } diff --git a/src/scanner/AutoBarcodeScanner.h b/src/scanner/AutoBarcodeScanner.h index 6dc833d..45bacb2 100644 --- a/src/scanner/AutoBarcodeScanner.h +++ b/src/scanner/AutoBarcodeScanner.h @@ -35,6 +35,8 @@ THE SOFTWARE. #include #include #include +#include +#include #include #include "BarcodeDecoder.h" @@ -129,6 +131,7 @@ public slots: QMutex m_scanProcessMutex; QWaitCondition m_scanProcessStopped; + QQuickWindow* m_mainWindow; // options QRect m_viewFinderRect; diff --git a/src/scanner/BarcodeDecoder.cpp b/src/scanner/BarcodeDecoder.cpp index 1bc465e..37595da 100644 --- a/src/scanner/BarcodeDecoder.cpp +++ b/src/scanner/BarcodeDecoder.cpp @@ -87,3 +87,7 @@ QVariantHash BarcodeDecoder::decodeBarcodeFromCache() { QVariantHash result = decoder->decodeImageEx(img); return result; } +QVariantHash BarcodeDecoder::decodeBarcodeFromImage(QImage &img) { + QVariantHash result = decoder->decodeImageEx(img); + return result; +} diff --git a/src/scanner/BarcodeDecoder.h b/src/scanner/BarcodeDecoder.h index 359e4ca..fc6bef1 100644 --- a/src/scanner/BarcodeDecoder.h +++ b/src/scanner/BarcodeDecoder.h @@ -40,7 +40,7 @@ class BarcodeDecoder : public QObject virtual ~BarcodeDecoder(); QVariantHash decodeBarcodeFromCache(); - + QVariantHash decodeBarcodeFromImage(QImage &img); void setDecoderFormat(const int &format); QString getCaptureLocation() const;