Qt: Improve camera compatibility

This commit is contained in:
Vicki Pfau 2017-07-28 17:45:18 -07:00
parent d1db97cf0c
commit 6ca3e9940d
2 changed files with 30 additions and 2 deletions

View File

@ -9,6 +9,7 @@
#include "GamepadAxisEvent.h" #include "GamepadAxisEvent.h"
#include "GamepadButtonEvent.h" #include "GamepadButtonEvent.h"
#include "InputProfile.h" #include "InputProfile.h"
#include "LogController.h"
#include <QApplication> #include <QApplication>
#include <QTimer> #include <QTimer>
@ -742,13 +743,18 @@ void InputController::setupCam() {
settings.setResolution(size); settings.setResolution(size);
auto cameraFormats = m_camera->supportedViewfinderPixelFormats(settings); auto cameraFormats = m_camera->supportedViewfinderPixelFormats(settings);
auto goodFormats = m_videoDumper.supportedPixelFormats(); auto goodFormats = m_videoDumper.supportedPixelFormats();
bool goodFormatFound = false;
for (auto& goodFormat : goodFormats) { for (auto& goodFormat : goodFormats) {
if (cameraFormats.contains(goodFormat)) { if (cameraFormats.contains(goodFormat)) {
settings.setPixelFormat(goodFormat); settings.setPixelFormat(goodFormat);
format = goodFormat; format = goodFormat;
goodFormatFound = true;
break; break;
} }
} }
if (!goodFormatFound) {
LOG(QT, WARN) << "Could not find a valid camera format!";
}
m_camera->setViewfinderSettings(settings); m_camera->setViewfinderSettings(settings);
#endif #endif
m_camera->setCaptureMode(QCamera::CaptureVideo); m_camera->setCaptureMode(QCamera::CaptureVideo);

View File

@ -19,10 +19,26 @@ bool VideoDumper::present(const QVideoFrame& frame) {
if (!mappedFrame.map(QAbstractVideoBuffer::ReadOnly)) { if (!mappedFrame.map(QAbstractVideoBuffer::ReadOnly)) {
return false; return false;
} }
QImage::Format format = QVideoFrame::imageFormatFromPixelFormat(mappedFrame.pixelFormat()); 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;
}
swap = true;
}
uchar* bits = mappedFrame.bits(); uchar* bits = mappedFrame.bits();
QImage image(bits, mappedFrame.width(), mappedFrame.height(), mappedFrame.bytesPerLine(), format); QImage image(bits, mappedFrame.width(), mappedFrame.height(), mappedFrame.bytesPerLine(), format);
image = image.copy(); // Create a deep copy of the bits if (swap) {
image = image.rgbSwapped();
} else {
image = image.copy(); // Create a deep copy of the bits
}
mappedFrame.unmap(); mappedFrame.unmap();
emit imageAvailable(image); emit imageAvailable(image);
return true; return true;
@ -36,5 +52,11 @@ QList<QVideoFrame::PixelFormat> VideoDumper::supportedPixelFormats(QAbstractVide
list.append(QVideoFrame::Format_ARGB32_Premultiplied); list.append(QVideoFrame::Format_ARGB32_Premultiplied);
list.append(QVideoFrame::Format_RGB565); list.append(QVideoFrame::Format_RGB565);
list.append(QVideoFrame::Format_RGB555); list.append(QVideoFrame::Format_RGB555);
list.append(QVideoFrame::Format_BGR32);
list.append(QVideoFrame::Format_BGRA32);
list.append(QVideoFrame::Format_BGR24);
list.append(QVideoFrame::Format_BGRA32_Premultiplied);
list.append(QVideoFrame::Format_BGR565);
list.append(QVideoFrame::Format_BGR555);
return list; return list;
} }