使用DirectShow API中的ICreateDevEnum接口,枚举系统中可用的视频采集设备,使用DirectShow API中的IBaseFilter接口,打开指定的视频采集设备。
时间: 2024-05-07 22:15:23 浏览: 17
以下是使用DirectShow API枚举系统中可用的视频采集设备的示例代码:
```c++
HRESULT hr;
ICreateDevEnum* pDevEnum = NULL;
IEnumMoniker* pEnum = NULL;
IMoniker* pMoniker = NULL;
// Create the System Device Enumerator.
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (void**)&pDevEnum);
if (FAILED(hr))
{
// Handle error.
}
// Create an enumerator for the video capture devices.
hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
if (hr == S_OK)
{
// Enumerate the devices.
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
{
// Do something with the moniker.
// For example, display the friendly name of the device.
IPropertyBag* pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&pPropBag);
if (SUCCEEDED(hr))
{
VARIANT var;
var.vt = VT_BSTR;
hr = pPropBag->Read(L"FriendlyName", &var, 0);
if (SUCCEEDED(hr))
{
// Display the friendly name.
wprintf(L"%s\n", var.bstrVal);
VariantClear(&var);
}
pPropBag->Release();
}
pMoniker->Release();
}
}
else if (hr == S_FALSE)
{
// No devices were found.
}
else
{
// Handle error.
}
pDevEnum->Release();
pEnum->Release();
```
以下是使用DirectShow API打开指定的视频采集设备的示例代码:
```c++
HRESULT hr;
IBaseFilter* pCaptureFilter = NULL;
IMoniker* pMoniker = NULL;
// Get the moniker for the device.
ICreateDevEnum* pDevEnum = NULL;
IEnumMoniker* pEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (void**)&pDevEnum);
if (FAILED(hr))
{
// Handle error.
}
hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
if (hr == S_OK)
{
ULONG cFetched;
while (pEnum->Next(1, &pMoniker, &cFetched) == S_OK)
{
IPropertyBag* pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&pPropBag);
if (FAILED(hr))
{
pMoniker->Release();
continue;
}
VARIANT var;
var.vt = VT_BSTR;
hr = pPropBag->Read(L"FriendlyName", &var, 0);
pPropBag->Release();
if (FAILED(hr))
{
pMoniker->Release();
continue;
}
// Check if this is the device we want to use.
if (wcscmp(var.bstrVal, L"Device Name") == 0)
{
hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, (void**)&pCaptureFilter);
if (FAILED(hr))
{
// Handle error.
}
VariantClear(&var);
break;
}
VariantClear(&var);
pMoniker->Release();
}
}
else if (hr == S_FALSE)
{
// No devices were found.
}
else
{
// Handle error.
}
pDevEnum->Release();
pEnum->Release();
// Use the capture filter for video capture.
// For example, add it to the DirectShow filter graph.
```
注意:在使用DirectShow API之前,需要调用CoInitialize或CoInitializeEx初始化COM库。在使用完成之后,需要调用CoUninitialize结束COM库的使用。
相关推荐
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)