Qt: Improve camera initialization
Vicki Pfau vi@endrift.com
Tue, 05 Mar 2019 17:34:02 -0800
4 files changed,
36 insertions(+),
17 deletions(-)
M
src/platform/qt/InputController.cpp
→
src/platform/qt/InputController.cpp
@@ -725,12 +725,23 @@ void InputController::setupCam() {
#ifdef BUILD_QT_MULTIMEDIA if (!m_camera) { m_camera = std::make_unique<QCamera>(); + connect(m_camera.get(), &QCamera::statusChanged, this, &InputController::prepareCamSettings); } - QVideoFrame::PixelFormat format(QVideoFrame::Format_RGB32); -#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) + m_camera->setCaptureMode(QCamera::CaptureVideo); + m_camera->setViewfinder(&m_videoDumper); m_camera->load(); +#endif +} + +#ifdef BUILD_QT_MULTIMEDIA +void InputController::prepareCamSettings(QCamera::Status status) { + if (status != QCamera::LoadedStatus || m_camera->state() == QCamera::ActiveState) { + return; + } +#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) + QVideoFrame::PixelFormat format(QVideoFrame::Format_RGB32); QCameraViewfinderSettings settings; - QSize size(1920, 1080); + QSize size(1280, 720); auto cameraRes = m_camera->supportedViewfinderResolutions(settings); for (auto& cameraSize : cameraRes) { if (cameraSize.width() < m_image.w || cameraSize.height() < m_image.h) {@@ -741,10 +752,11 @@ size = cameraSize;
} } settings.setResolution(size); + auto cameraFormats = m_camera->supportedViewfinderPixelFormats(settings); auto goodFormats = m_videoDumper.supportedPixelFormats(); bool goodFormatFound = false; - for (auto& goodFormat : goodFormats) { + for (const auto& goodFormat : goodFormats) { if (cameraFormats.contains(goodFormat)) { settings.setPixelFormat(goodFormat); format = goodFormat;@@ -754,20 +766,20 @@ }
} if (!goodFormatFound) { LOG(QT, WARN) << "Could not find a valid camera format!"; + for (const auto& format : cameraFormats) { + LOG(QT, WARN) << "Camera supported format: " << QString::number(format); + } } m_camera->setViewfinderSettings(settings); #endif - m_camera->setCaptureMode(QCamera::CaptureVideo); - m_camera->setViewfinder(&m_videoDumper); m_camera->start(); -#endif } +#endif void InputController::teardownCam() { #ifdef BUILD_QT_MULTIMEDIA if (m_camera) { m_camera->stop(); - m_camera.reset(); } #endif }
M
src/platform/qt/InputController.h
→
src/platform/qt/InputController.h
@@ -27,12 +27,11 @@
#ifdef BUILD_QT_MULTIMEDIA #include "VideoDumper.h" +#include <QCamera> #endif struct mRotationSource; struct mRumble; - -class QCamera; namespace QGBA {@@ -124,6 +123,9 @@ void loadCamImage(const QString& path);
void setCamImage(const QImage& image); private slots: +#ifdef BUILD_QT_MULTIMEDIA + void prepareCamSettings(QCamera::Status); +#endif void setupCam(); void teardownCam();
M
src/platform/qt/VideoDumper.cpp
→
src/platform/qt/VideoDumper.cpp
@@ -23,14 +23,18 @@ QVideoFrame::PixelFormat vFormat = mappedFrame.pixelFormat();
QImage::Format format = QVideoFrame::imageFormatFromPixelFormat(vFormat); bool swap = false; if (format == QImage::Format_Invalid) { - vFormat = static_cast<QVideoFrame::PixelFormat>(vFormat - QVideoFrame::Format_BGRA32 + QVideoFrame::Format_ARGB32); - format = QVideoFrame::imageFormatFromPixelFormat(vFormat); - if (format == QImage::Format_ARGB32) { - format = QImage::Format_RGBA8888; - } else if (format == QImage::Format_ARGB32_Premultiplied) { - format = QImage::Format_RGBA8888_Premultiplied; + if (vFormat < QVideoFrame::Format_BGRA5658_Premultiplied) { + vFormat = static_cast<QVideoFrame::PixelFormat>(vFormat - QVideoFrame::Format_BGRA32 + QVideoFrame::Format_ARGB32); + format = QVideoFrame::imageFormatFromPixelFormat(vFormat); + if (format == QImage::Format_ARGB32) { + format = QImage::Format_RGBA8888; + } else if (format == QImage::Format_ARGB32_Premultiplied) { + format = QImage::Format_RGBA8888_Premultiplied; + } + swap = true; + } else { + return false; } - swap = true; } uchar* bits = mappedFrame.bits(); QImage image(bits, mappedFrame.width(), mappedFrame.height(), mappedFrame.bytesPerLine(), format);