adjust camera shito for Qt6

This commit is contained in:
Arisotura
2022-10-01 20:16:52 +02:00
parent eca086ef95
commit 93dfcf8714
2 changed files with 112 additions and 23 deletions

View File

@ -20,6 +20,30 @@
#include "Config.h" #include "Config.h"
#if QT_VERSION >= 0x060000
CameraFrameDumper::CameraFrameDumper(QObject* parent) : QVideoSink(parent)
{
cam = (CameraManager*)parent;
connect(this, &CameraFrameDumper::videoFrameChanged, this, &CameraFrameDumper::present);
}
void CameraFrameDumper::present(const QVideoFrame& _frame)
{
QVideoFrame frame(_frame);
if (!frame.map(QVideoFrame::ReadOnly))
return;
if (!frame.isReadable())
return;
cam->feedFrame((u32*)frame.bits(0), frame.width(), frame.height(), frame.pixelFormat() == QVideoFrameFormat::Format_YUYV);
frame.unmap();
}
#else
CameraFrameDumper::CameraFrameDumper(QObject* parent) : QAbstractVideoSurface(parent) CameraFrameDumper::CameraFrameDumper(QObject* parent) : QAbstractVideoSurface(parent)
{ {
cam = (CameraManager*)parent; cam = (CameraManager*)parent;
@ -50,6 +74,8 @@ QList<QVideoFrame::PixelFormat> CameraFrameDumper::supportedPixelFormats(QAbstra
return ret; return ret;
} }
#endif
CameraManager::CameraManager(int num, int width, int height, bool yuv) : QObject() CameraManager::CameraManager(int num, int width, int height, bool yuv) : QObject()
{ {
@ -93,6 +119,8 @@ void CameraManager::init()
inputType = Config::Camera[num].InputType; inputType = Config::Camera[num].InputType;
imagePath = QString::fromStdString(Config::Camera[num].ImagePath); imagePath = QString::fromStdString(Config::Camera[num].ImagePath);
camDeviceName = QString::fromStdString(Config::Camera[num].CamDeviceName); camDeviceName = QString::fromStdString(Config::Camera[num].CamDeviceName);
camDevice = nullptr;
{ {
// fill the framebuffer with black // fill the framebuffer with black
@ -134,26 +162,51 @@ void CameraManager::init()
else if (inputType == 2) else if (inputType == 2)
{ {
// physical camera // physical camera
camDevice = new QCamera(camDeviceName.toUtf8()); #if QT_VERSION >= 0x060000
camDumper = new CameraFrameDumper(this); const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
camDevice->setViewfinder(camDumper); for (const QCameraDevice &cam : cameras)
/*camDevice->load();
QCameraViewfinderSettings settings;
auto resolutions = camDevice->supportedViewfinderResolutions();
for (auto& res : resolutions)
{ {
printf("RESOLUTION: %d x %d\n", res.width(), res.height()); if (QString(cam.id()) == camDeviceName)
{
camDevice = new QCamera(cam);
break;
}
} }
if (camDevice)
{
camDumper = new CameraFrameDumper(this);
camDevice->unload();*/ camSession = new QMediaCaptureSession(this);
camSession->setCamera(camDevice);
camSession->setVideoOutput(camDumper);
}
#else
camDevice = new QCamera(camDeviceName.toUtf8());
if (camDevice)
{
camDumper = new CameraFrameDumper(this);
camDevice->setViewfinder(camDumper);
QCameraViewfinderSettings settings; /*camDevice->load();
settings.setResolution(640, 480); QCameraViewfinderSettings settings;
settings.setPixelFormat(QVideoFrame::Format_YUYV);
camDevice->setViewfinderSettings(settings); auto resolutions = camDevice->supportedViewfinderResolutions();
for (auto& res : resolutions)
{
printf("RESOLUTION: %d x %d\n", res.width(), res.height());
}
camDevice->unload();*/
QCameraViewfinderSettings settings;
settings.setResolution(640, 480);
settings.setPixelFormat(QVideoFrame::Format_YUYV);
camDevice->setViewfinderSettings(settings);
}
#endif
} }
} }
@ -161,11 +214,18 @@ void CameraManager::deInit()
{ {
if (inputType == 2) if (inputType == 2)
{ {
camDevice->stop(); if (camDevice)
delete camDevice; {
delete camDumper; camDevice->stop();
delete camDevice;
delete camDumper;
#if QT_VERSION >= 0x060000
delete camSession;
#endif
}
} }
camDevice = nullptr;
inputType = -1; inputType = -1;
} }
@ -198,12 +258,14 @@ bool CameraManager::isStarted()
void CameraManager::camStart() void CameraManager::camStart()
{ {
camDevice->start(); if (camDevice)
camDevice->start();
} }
void CameraManager::camStop() void CameraManager::camStop()
{ {
camDevice->stop(); if (camDevice)
camDevice->stop();
} }
void CameraManager::setXFlip(bool flip) void CameraManager::setXFlip(bool flip)

View File

@ -23,17 +23,38 @@
#if QT_VERSION >= 0x060000 #if QT_VERSION >= 0x060000
#include <QMediaDevices> #include <QMediaDevices>
#include <QCameraDevice> #include <QCameraDevice>
#include <QMediaCaptureSession>
#include <QVideoSink>
#else #else
#include <QCameraInfo> #include <QCameraInfo>
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>
#endif #endif
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>
#include <QMutex> #include <QMutex>
#include "types.h" #include "types.h"
class CameraManager; class CameraManager;
#if QT_VERSION >= 0x060000
class CameraFrameDumper : public QVideoSink
{
Q_OBJECT
public:
CameraFrameDumper(QObject* parent = nullptr);
public slots:
void present(const QVideoFrame& frame);
private:
CameraManager* cam;
};
#else
class CameraFrameDumper : public QAbstractVideoSurface class CameraFrameDumper : public QAbstractVideoSurface
{ {
Q_OBJECT Q_OBJECT
@ -48,6 +69,9 @@ private:
CameraManager* cam; CameraManager* cam;
}; };
#endif
class CameraManager : public QObject class CameraManager : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -88,6 +112,9 @@ private:
QCamera* camDevice; QCamera* camDevice;
CameraFrameDumper* camDumper; CameraFrameDumper* camDumper;
#if QT_VERSION >= 0x060000
QMediaCaptureSession* camSession;
#endif
int frameWidth, frameHeight; int frameWidth, frameHeight;
bool frameFormatYUV; bool frameFormatYUV;