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"
#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)
{
cam = (CameraManager*)parent;
@ -50,6 +74,8 @@ QList<QVideoFrame::PixelFormat> CameraFrameDumper::supportedPixelFormats(QAbstra
return ret;
}
#endif
CameraManager::CameraManager(int num, int width, int height, bool yuv) : QObject()
{
@ -93,6 +119,8 @@ void CameraManager::init()
inputType = Config::Camera[num].InputType;
imagePath = QString::fromStdString(Config::Camera[num].ImagePath);
camDeviceName = QString::fromStdString(Config::Camera[num].CamDeviceName);
camDevice = nullptr;
{
// fill the framebuffer with black
@ -134,26 +162,51 @@ void CameraManager::init()
else if (inputType == 2)
{
// physical camera
camDevice = new QCamera(camDeviceName.toUtf8());
camDumper = new CameraFrameDumper(this);
camDevice->setViewfinder(camDumper);
/*camDevice->load();
QCameraViewfinderSettings settings;
auto resolutions = camDevice->supportedViewfinderResolutions();
for (auto& res : resolutions)
#if QT_VERSION >= 0x060000
const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
for (const QCameraDevice &cam : cameras)
{
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;
settings.setResolution(640, 480);
settings.setPixelFormat(QVideoFrame::Format_YUYV);
camDevice->setViewfinderSettings(settings);
/*camDevice->load();
QCameraViewfinderSettings 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)
{
camDevice->stop();
delete camDevice;
delete camDumper;
if (camDevice)
{
camDevice->stop();
delete camDevice;
delete camDumper;
#if QT_VERSION >= 0x060000
delete camSession;
#endif
}
}
camDevice = nullptr;
inputType = -1;
}
@ -198,12 +258,14 @@ bool CameraManager::isStarted()
void CameraManager::camStart()
{
camDevice->start();
if (camDevice)
camDevice->start();
}
void CameraManager::camStop()
{
camDevice->stop();
if (camDevice)
camDevice->stop();
}
void CameraManager::setXFlip(bool flip)

View File

@ -23,17 +23,38 @@
#if QT_VERSION >= 0x060000
#include <QMediaDevices>
#include <QCameraDevice>
#include <QMediaCaptureSession>
#include <QVideoSink>
#else
#include <QCameraInfo>
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>
#endif
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>
#include <QMutex>
#include "types.h"
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
{
Q_OBJECT
@ -48,6 +69,9 @@ private:
CameraManager* cam;
};
#endif
class CameraManager : public QObject
{
Q_OBJECT
@ -88,6 +112,9 @@ private:
QCamera* camDevice;
CameraFrameDumper* camDumper;
#if QT_VERSION >= 0x060000
QMediaCaptureSession* camSession;
#endif
int frameWidth, frameHeight;
bool frameFormatYUV;