[libcamera-devel] [PATCH] qcam: viewfinder: Maintain aspect ratio

Kieran Bingham kieran.bingham at ideasonboard.com
Fri Mar 26 23:05:40 CET 2021


Keep the image aspect ratio when displaying in the viewfinder.

When the window is adjusted to a size that differs in aspect ratio to
the image, keep the image centered in the main viewpoint while
maintaining the image's aspect ratio.

Signed-off-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
---
It's been really annoying me that the QCam viewfinder can scale to fit
the window, but that it breaks the aspect ratio of the image it
contains.

I can't imagine why we would want to viewfind with an incorrect aspect
ratio, but if we do we can add that as an option later. For now -
enforce aspect ratio on the QT viewfinder.

Of course I'm aware that this doesn't cover the GL viewfinder. That can
either be handled on top, or independently if this is deemed a worthy
feature.


 src/qcam/viewfinder_qt.cpp | 19 +++++++++++++++++--
 src/qcam/viewfinder_qt.h   |  2 ++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/qcam/viewfinder_qt.cpp b/src/qcam/viewfinder_qt.cpp
index e436714c6bdb..f6ca6793b4e4 100644
--- a/src/qcam/viewfinder_qt.cpp
+++ b/src/qcam/viewfinder_qt.cpp
@@ -15,6 +15,7 @@
 #include <QMap>
 #include <QMutexLocker>
 #include <QPainter>
+#include <QResizeEvent>
 #include <QtDebug>
 
 #include <libcamera/formats.h>
@@ -34,7 +35,7 @@ static const QMap<libcamera::PixelFormat, QImage::Format> nativeFormats
 };
 
 ViewFinderQt::ViewFinderQt(QWidget *parent)
-	: QWidget(parent), buffer_(nullptr)
+	: QWidget(parent), place_(rect()), buffer_(nullptr)
 {
 	icon_ = QIcon(":camera-off.svg");
 }
@@ -146,7 +147,7 @@ void ViewFinderQt::paintEvent(QPaintEvent *)
 
 	/* If we have an image, draw it. */
 	if (!image_.isNull()) {
-		painter.drawImage(rect(), image_, image_.rect());
+		painter.drawImage(place_, image_, image_.rect());
 		return;
 	}
 
@@ -179,3 +180,17 @@ QSize ViewFinderQt::sizeHint() const
 {
 	return size_.isValid() ? size_ : QSize(640, 480);
 }
+
+void ViewFinderQt::resizeEvent(QResizeEvent *event)
+{
+	if (!size_.isValid()) {
+		event->accept();
+		return;
+	}
+
+	QPoint zero(0, 0);
+	place_ = QRect(zero, size_.scaled(event->size(), Qt::KeepAspectRatio));
+	place_.moveCenter(QRect(zero, size()).center());
+
+	event->accept();
+}
diff --git a/src/qcam/viewfinder_qt.h b/src/qcam/viewfinder_qt.h
index d755428887c0..bf31b81b3437 100644
--- a/src/qcam/viewfinder_qt.h
+++ b/src/qcam/viewfinder_qt.h
@@ -42,6 +42,7 @@ Q_SIGNALS:
 
 protected:
 	void paintEvent(QPaintEvent *) override;
+	void resizeEvent(QResizeEvent *) override;
 	QSize sizeHint() const override;
 
 private:
@@ -49,6 +50,7 @@ private:
 
 	libcamera::PixelFormat format_;
 	QSize size_;
+	QRect place_;
 
 	/* Camera stopped icon */
 	QSize vfSize_;
-- 
2.25.1



More information about the libcamera-devel mailing list