From patchwork Thu Feb 6 15:05:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 2792 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AADE760902 for ; Thu, 6 Feb 2020 16:05:11 +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 5784EB7D; Thu, 6 Feb 2020 16:05:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1581001511; bh=PtmplQqhe0wEuklPc8Jw/+DcHL1p5yhct/6PJlwZ0PY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DbE95t+YDa1mXtoFhsk5u637OdWjqJAtUhBt7GVoxkpsXPJWiopmJYOXxkMaJIsvN t50JPbRrIeC0esDnRApg1hMFNDbRmBbLtN5ed8THkE+c/Vy0h9QknsnEvSPRj5jmzR 4hFl0rWKqYv+o44xc0lhxfNQcp/OqZuP3Cc8Q4fA= From: Kieran Bingham To: LibCamera Devel Date: Thu, 6 Feb 2020 15:05:04 +0000 Message-Id: <20200206150504.24204-7-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200206150504.24204-1-kieran.bingham@ideasonboard.com> References: <20200206150504.24204-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 6/6] 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: Thu, 06 Feb 2020 15:05:12 -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 --- src/qcam/main_window.cpp | 22 ++++++++++++++++++++++ src/qcam/main_window.h | 1 + src/qcam/viewfinder.cpp | 7 +++++++ src/qcam/viewfinder.h | 2 ++ 4 files changed, 32 insertions(+) diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp index 0ae4c60b8699..0e994b1e9197 100644 --- a/src/qcam/main_window.cpp +++ b/src/qcam/main_window.cpp @@ -11,7 +11,10 @@ #include #include +#include #include +#include +#include #include #include #include @@ -89,6 +92,9 @@ int MainWindow::createToolbars(CameraManager *cm) action = toolbar_->addAction(QIcon(":stop-circle.svg"), "stop"); connect(action, &QAction::triggered, this, &MainWindow::stopCapture); + action = toolbar_->addAction(QIcon(":save.svg"), "save"); + connect(action, &QAction::triggered, this, &MainWindow::saveImage); + return 0; } @@ -339,6 +345,22 @@ void MainWindow::stopCapture() setWindowTitle(title_); } +void MainWindow::saveImage() +{ + /* Take a lock to prevent updating the backed image, copy, + * then ask where to save with lock released */ + + QImage image = viewfinder_->getCurrentImage(); + + QString filename = QFileDialog::getSaveFileName(this, "Save Image", "", + "Image Files (*.png *.jpg *.jpeg)"); + + std::cerr << "Save jpeg to " << filename.toStdString() << std::endl; + + 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 b0bf16dd2a09..fc85b6a46491 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 saveImage(); private: int createToolbars(CameraManager *cm); diff --git a/src/qcam/viewfinder.cpp b/src/qcam/viewfinder.cpp index 6de284d1b782..3fa4a326c342 100644 --- a/src/qcam/viewfinder.cpp +++ b/src/qcam/viewfinder.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include "format_converter.h" @@ -27,6 +28,12 @@ void ViewFinder::display(const unsigned char *raw, size_t size) update(); } +QImage ViewFinder::getCurrentImage() +{ + /* Ideally need to lock, return/copy, then unlock... Can a scoped lock work? */ + 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..1da79d3e67aa 100644 --- a/src/qcam/viewfinder.h +++ b/src/qcam/viewfinder.h @@ -23,6 +23,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;