基于Qt和微软Media Foundation 框架获取或设置UVC相机属性的完整demo
时间: 2023-11-28 19:47:25 浏览: 217
以下是一个基于Qt和微软Media Foundation框架的获取或设置UVC相机属性的完整演示代码。这个例子通过调用Media Foundation API来实现相机属性的获取和设置,并使用Qt框架来创建图形用户界面。
```c++
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLabel>
#include <QtWidgets/QSpinBox>
#include <Windows.h>
#include <mfapi.h>
#include <mfidl.h>
#include <Mfreadwrite.h>
#include <iostream>
#pragma comment(lib, "mfplat.lib")
#pragma comment(lib, "mfuuid.lib")
#pragma comment(lib, "mfreadwrite.lib")
#pragma comment(lib, "mf.lib")
class CameraControl : public QMainWindow
{
Q_OBJECT
public:
CameraControl(QWidget *parent = nullptr)
: QMainWindow(parent)
{
// UI Setup
setWindowTitle("Camera Control");
setFixedSize(400, 300);
auto* label1 = new QLabel("Brightness", this);
label1->move(20, 20);
auto* spinBox1 = new QSpinBox(this);
spinBox1->setRange(-100, 100);
spinBox1->setValue(0);
spinBox1->setSingleStep(5);
spinBox1->move(120, 20);
auto* button1 = new QPushButton("Set", this);
button1->move(260, 20);
auto* label2 = new QLabel("Contrast", this);
label2->move(20, 60);
auto* spinBox2 = new QSpinBox(this);
spinBox2->setRange(-100, 100);
spinBox2->setValue(0);
spinBox2->setSingleStep(5);
spinBox2->move(120, 60);
auto* button2 = new QPushButton("Set", this);
button2->move(260, 60);
auto* label3 = new QLabel("Saturation", this);
label3->move(20, 100);
auto* spinBox3 = new QSpinBox(this);
spinBox3->setRange(-100, 100);
spinBox3->setValue(0);
spinBox3->setSingleStep(5);
spinBox3->move(120, 100);
auto* button3 = new QPushButton("Set", this);
button3->move(260, 100);
auto* label4 = new QLabel("Sharpness", this);
label4->move(20, 140);
auto* spinBox4 = new QSpinBox(this);
spinBox4->setRange(-100, 100);
spinBox4->setValue(0);
spinBox4->setSingleStep(5);
spinBox4->move(120, 140);
auto* button4 = new QPushButton("Set", this);
button4->move(260, 140);
auto* label5 = new QLabel("Zoom", this);
label5->move(20, 180);
auto* spinBox5 = new QSpinBox(this);
spinBox5->setRange(0, 10);
spinBox5->setValue(0);
spinBox5->setSingleStep(1);
spinBox5->move(120, 180);
auto* button5 = new QPushButton("Set", this);
button5->move(260, 180);
// Camera Setup
IMFAttributes* pAttributes = nullptr;
IMFMediaSource* pSource = nullptr;
IMFActivate** ppDevices = nullptr;
UINT32 count = 0;
HRESULT hr = MFCreateAttributes(&pAttributes, 1);
if (SUCCEEDED(hr))
{
hr = pAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
}
if (SUCCEEDED(hr))
{
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
}
if (SUCCEEDED(hr))
{
// Pick the first camera
hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
}
if (SUCCEEDED(hr))
{
IMFMediaControl* pControl = nullptr;
IMFVideoDisplayControl* pDisplay = nullptr;
IMFGetService* pService = nullptr;
hr = pSource->QueryInterface(IID_PPV_ARGS(&pControl));
if (SUCCEEDED(hr))
{
hr = pControl->Start();
}
if (SUCCEEDED(hr))
{
hr = pSource->QueryInterface(IID_PPV_ARGS(&pDisplay));
}
if (SUCCEEDED(hr))
{
hr = pDisplay->SetVideoWindow((HWND)winId());
if (SUCCEEDED(hr))
{
hr = pDisplay->SetAspectRatioMode(MFVideoARMode_None);
}
}
if (SUCCEEDED(hr))
{
hr = pSource->QueryInterface(IID_PPV_ARGS(&pService));
}
// Button 1 Handler
connect(button1, &QPushButton::clicked, [=]() {
IPropertyStore* pProps = nullptr;
PROPVARIANT var;
HRESULT hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
if (SUCCEEDED(hr))
{
hr = InitPropVariantFromInt32(spinBox1->value(), &var);
if (SUCCEEDED(hr))
{
hr = pProps->SetValue(PKEY_VideoProcBrightness, var);
}
if (SUCCEEDED(hr))
{
hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
}
PropVariantClear(&var);
}
});
// Button 2 Handler
connect(button2, &QPushButton::clicked, [=]() {
IPropertyStore* pProps = nullptr;
PROPVARIANT var;
HRESULT hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
if (SUCCEEDED(hr))
{
hr = InitPropVariantFromInt32(spinBox2->value(), &var);
if (SUCCEEDED(hr))
{
hr = pProps->SetValue(PKEY_VideoProcContrast, var);
}
if (SUCCEEDED(hr))
{
hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
}
PropVariantClear(&var);
}
});
// Button 3 Handler
connect(button3, &QPushButton::clicked, [=]() {
IPropertyStore* pProps = nullptr;
PROPVARIANT var;
HRESULT hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
if (SUCCEEDED(hr))
{
hr = InitPropVariantFromInt32(spinBox3->value(), &var);
if (SUCCEEDED(hr))
{
hr = pProps->SetValue(PKEY_VideoProcSaturation, var);
}
if (SUCCEEDED(hr))
{
hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
}
PropVariantClear(&var);
}
});
// Button 4 Handler
connect(button4, &QPushButton::clicked, [=]() {
IPropertyStore* pProps = nullptr;
PROPVARIANT var;
HRESULT hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
if (SUCCEEDED(hr))
{
hr = InitPropVariantFromInt32(spinBox4->value(), &var);
if (SUCCEEDED(hr))
{
hr = pProps->SetValue(PKEY_VideoProcSharpness, var);
}
if (SUCCEEDED(hr))
{
hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
}
PropVariantClear(&var);
}
});
// Button 5 Handler
connect(button5, &QPushButton::clicked, [=]() {
IPropertyStore* pProps = nullptr;
PROPVARIANT var;
HRESULT hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
if (SUCCEEDED(hr))
{
hr = InitPropVariantFromInt32(spinBox5->value(), &var);
if (SUCCEEDED(hr))
{
hr = pProps->SetValue(PKEY_VideoProcZoom, var);
}
if (SUCCEEDED(hr))
{
hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, IID_PPV_ARGS(&pProps));
}
PropVariantClear(&var);
}
});
// Cleanup
if (pControl)
{
pControl->Stop();
pControl->Release();
}
if (pDisplay)
{
pDisplay->Release();
}
if (pService)
{
pService->Release();
}
}
if (pSource)
{
pSource->Release();
}
if (ppDevices)
{
for (UINT32 i = 0; i < count; i++)
{
ppDevices[i]->Release();
}
CoTaskMemFree(ppDevices);
}
if (pAttributes)
{
pAttributes->Release();
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CameraControl w;
w.show();
return a.exec();
}
```
在这个例子中,我们首先使用`MFCreateAttributes()`函数创建了一个属性集对象。然后,我们使用`SetGUID()`方法设置了属性集的源类型为`MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID`,这表示我们要获取视频捕获设备的属性。接下来,我们使用`MFEnumDeviceSources()`函数获取可用的视频捕获设备列表,并选择了列表中的第一个设备。然后,我们使用`IPropertyStore`接口获取了视频渲染服务,并使用`SetValue()`方法设置了不同的属性值。最后,我们使用`IMFMediaControl`和`IMFVideoDisplayControl`接口来控制视频的播放和显示。
这个例子只涉及了一些基本的相机属性,如亮度、对比度、饱和度、锐度和缩放。如果你需要获取或设置其他相机属性,可以像这个例子一样使用`IPropertyStore`接口,并使用相应的属性键。
阅读全文