PDA

View Full Version : QCameraViewfinder aspect ratio



gpuckett
29th May 2014, 20:52
I am developing an application that includes displaying the feed from an attached camera. The application starts up with the video display only on half the window. The user can then click a button that toggles the video between full screen and the half window. It works great, however the aspect ratio is not being applied. I tried setting the aspect ratio mode on the QCameraViewfinder widget but it made no differed. I really need some help with this. I will attempt to show the parts of the code that applies.

Here is the ui file:


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1024</width>
<height>768</height>
</rect>
</property>
<property name="windowTitle">
<string>LEAP</string>
</property>
<widget class="QWidget" name="centralWidget">
<property name="minimumSize">
<size>
<width>1024</width>
<height>768</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1024</width>
<height>768</height>
</size>
</property>
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1024</width>
<height>768</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
</property>
<item>
<widget class="MovingMap" name="mapFrame" native="true">
<property name="minimumSize">
<size>
<width>512</width>
<height>768</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1024</width>
<height>768</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QCameraViewfinder" name="cameraFrame" native="true">
<property name="minimumSize">
<size>
<width>512</width>
<height>768</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1024</width>
<height>768</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QCameraViewfinder</class>
<extends>QWidget</extends>
<header>qcameraviewfinder.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>MovingMap</class>
<extends>QWidget</extends>
<header>movingmap.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>


I have a function that starts up the camera:


void MainWindow::startCamera()
{
qDebug() << "At MainWindow::startCamera()";

QByteArray cameraDevice = QCamera::availableDevices()[0];
camera = new QCamera(cameraDevice);
camera->setViewfinder(ui->cameraFrame);
ui->cameraFrame->setAspectRatioMode(Qt::KeepAspectRatioByExpanding) ;
camera->setCaptureMode(QCamera::CaptureStillImage);
camera->start();
}


The result is the video fills it's widget. But it is stretched top to bottom instead of expanding the video output so the image is displayed according to the proper aspect ratio.

What am I doing wrong? Is it the layout that is causing the problem? Should I capture the image and trim it in my own code before it is passed to the display? I can think of a couple possibilities but I'd prefer to not put extra processing in my code if it not necessary.