From patchwork Fri Feb 14 00:18:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 2832 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 14FDE61A2E for ; Fri, 14 Feb 2020 01:18:16 +0100 (CET) Received: from localhost.localdomain (cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A5323504; Fri, 14 Feb 2020 01:18:15 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1581639495; bh=EKTNFapm9xD4ofe37wUsen3sqcrcEp0sQRca5slKVno=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L9yV2kmjQ0W+PekQzwPxDO/FIbMD9q/0uG615tso3yJiJZP8LgWjJ4g5sg1aMyzVL q7W3CnBqazzBCLz9uwEryFf/2I5XjD8y+0zQPGGfNSyNpoFBp2GJ9JY6ahdFGucz78 GUIMLYpJD2zdlWflnc+EJyVsy6vwEiO+kcM4AgCQ= From: Kieran Bingham To: libcamera devel Date: Fri, 14 Feb 2020 00:18:10 +0000 Message-Id: <20200214001810.19302-8-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200214001810.19302-1-kieran.bingham@ideasonboard.com> References: <20200214001810.19302-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 7/7] qcam: Provide save image functionality X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Feb 2020 00:18:17 -0000 Implement a save image button on the toolbar which will take a current viewfinder image and present the user with a QFileDialog to allow them to choose where to save the image. Utilise the QImageWriter to perform the output task. Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- v2: - Rename save to saveAs - Add empty file check - Lock concurrent access to the ViewFinder image src/qcam/assets/feathericons/feathericons.qrc | 1 + src/qcam/main_window.cpp | 22 +++++++++++++++++++ src/qcam/main_window.h | 1 + src/qcam/viewfinder.cpp | 11 ++++++++++ src/qcam/viewfinder.h | 5 +++++ 5 files changed, 40 insertions(+) diff --git a/src/qcam/assets/feathericons/feathericons.qrc b/src/qcam/assets/feathericons/feathericons.qrc index b8e5c2266408..6ca3a846803c 100644 --- a/src/qcam/assets/feathericons/feathericons.qrc +++ b/src/qcam/assets/feathericons/feathericons.qrc @@ -1,6 +1,7 @@ ./play-circle.svg +./save.svg ./stop-circle.svg ./x-circle.svg diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp index ec93e0177b41..8ee2a7d68c96 100644 --- a/src/qcam/main_window.cpp +++ b/src/qcam/main_window.cpp @@ -12,7 +12,10 @@ #include #include +#include #include +#include +#include #include #include #include @@ -88,6 +91,9 @@ int MainWindow::createToolbars() action = toolbar_->addAction(QIcon(":stop-circle.svg"), "stop"); connect(action, &QAction::triggered, this, &MainWindow::stopCapture); + action = toolbar_->addAction(QIcon(":save.svg"), "saveAs"); + connect(action, &QAction::triggered, this, &MainWindow::saveImageAs); + return 0; } @@ -339,6 +345,22 @@ void MainWindow::stopCapture() setWindowTitle(title_); } +void MainWindow::saveImageAs() +{ + QImage image = viewfinder_->getCurrentImage(); + + QString filename = QFileDialog::getSaveFileName(this, "Save Image", "", + "Image Files (*.png *.jpg *.jpeg)"); + + std::cerr << "Save image to " << filename.toStdString() << std::endl; + + if (filename == "") + return; + + QImageWriter writer(filename); + writer.write(image); +} + void MainWindow::requestComplete(Request *request) { if (request->status() == Request::RequestCancelled) diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h index 27ceed611d59..dcc39d7de948 100644 --- a/src/qcam/main_window.h +++ b/src/qcam/main_window.h @@ -48,6 +48,7 @@ private Q_SLOTS: int startCapture(); void stopCapture(); + void saveImageAs(); private: int createToolbars(); diff --git a/src/qcam/viewfinder.cpp b/src/qcam/viewfinder.cpp index 6de284d1b782..211926e185d3 100644 --- a/src/qcam/viewfinder.cpp +++ b/src/qcam/viewfinder.cpp @@ -6,6 +6,8 @@ */ #include +#include +#include #include #include "format_converter.h" @@ -23,10 +25,19 @@ ViewFinder::~ViewFinder() void ViewFinder::display(const unsigned char *raw, size_t size) { + QMutexLocker locker(&mutex_); + converter_.convert(raw, size, image_); update(); } +QImage ViewFinder::getCurrentImage() +{ + QMutexLocker locker(&mutex_); + + return *image_; +} + int ViewFinder::setFormat(unsigned int format, unsigned int width, unsigned int height) { diff --git a/src/qcam/viewfinder.h b/src/qcam/viewfinder.h index ef5fd45b264a..06e8034fce1d 100644 --- a/src/qcam/viewfinder.h +++ b/src/qcam/viewfinder.h @@ -7,6 +7,7 @@ #ifndef __QCAM_VIEWFINDER_H__ #define __QCAM_VIEWFINDER_H__ +#include #include #include "format_converter.h" @@ -23,6 +24,8 @@ public: unsigned int height); void display(const unsigned char *rgb, size_t size); + QImage getCurrentImage(); + protected: void paintEvent(QPaintEvent *) override; QSize sizeHint() const override; @@ -33,7 +36,9 @@ private: unsigned int height_; FormatConverter converter_; + QImage *image_; + QMutex mutex_; // Prevent concurrent access to image_ }; #endif /* __QCAM_VIEWFINDER__ */