gpt4 book ai didi

c++ - Windows Media Foundation 枚举相机设备

转载 作者:可可西里 更新时间:2023-11-01 11:18:45 27 4
gpt4 key购买 nike

enter image description here我想使用 Windows Media Foundation 枚举我计算机上的相机设备,我使用了 Microsoft 上的代码:http://msdn.microsoft.com/en-us/library/windows/desktop/dd940326(v=vs.85).aspx我复制了他们在这里使用的相同代码:http://msdn.microsoft.com/en-us/library/windows/desktop/ee663604(v=vs.85).aspx

当我使用他们的代码时,我得到了我的网络摄像头设备名称,但是我的代码没有找到任何摄像头捕获设备。我能够找到原因。

代码如下:

    #pragma once
#include <new>
#include <windows.h>
#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#include <Wmcodecdsp.h>
#include <assert.h>
#include <Dbt.h>
#include <shlwapi.h>
#include <mfplay.h>

#include <iostream>


const UINT WM_APP_PREVIEW_ERROR = WM_APP + 1; // wparam = HRESULT

class DeviceList
{
UINT32 m_cDevices; // contains the number of devices
IMFActivate **m_ppDevices; // contains properties about each device

public:
DeviceList() : m_ppDevices(NULL), m_cDevices(0)
{

}
~DeviceList()
{
Clear();
}

UINT32 Count() const { return m_cDevices; }

void Clear();
HRESULT EnumerateDevices();
HRESULT GetDevice(UINT32 index, IMFActivate **ppActivate);
HRESULT GetDeviceName(UINT32 index, WCHAR **ppszName);
};




#include "DeviceList.h"

/*
* A templated Function SafeRelease releasing pointers memories
* @param ppT the pointer to release
*/

template <class T> void SafeRelease(T **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}



/*
* A function which copy attribute form source to a destination
* @ param pSrc is an Interface to store key/value pairs of an Object
* @ param pDest is an Interface to store key/value pairs of an Object
* @ param GUID is an unique identifier
* @ return HRESULT return errors warning condition on windows
*/

HRESULT CopyAttribute(IMFAttributes *pSrc, IMFAttributes *pDest, const GUID& key);




/*
* A Method form DeviceList which clear the list of Devices
*/

void DeviceList::Clear()
{
for (UINT32 i = 0; i < m_cDevices; i++)
{
SafeRelease(&m_ppDevices[i]);
}
CoTaskMemFree(m_ppDevices);
m_ppDevices = NULL;

m_cDevices = 0;
}


/*
* A function which enumerate the list of Devices.
* @ return HRESULT return errors warning condition on windows
*/
HRESULT DeviceList::EnumerateDevices()
{
HRESULT hr = S_OK;
IMFAttributes *pAttributes = NULL;

this->Clear();

// Initialize an attribute store. We will use this to
// specify the enumeration parameters.
std::cout << "Enumerate devices" << std::endl;
hr = MFCreateAttributes(&pAttributes, 1);

// Ask for source type = video capture devices
if (SUCCEEDED(hr))
{
std::cout << "Enumerate devices" << std::endl;
hr = pAttributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID
);
}
// Enumerate devices.
if (SUCCEEDED(hr))
{
std::cout << "Enumerate devices:" << m_cDevices << std::endl;
hr = MFEnumDeviceSources(pAttributes, &m_ppDevices, &m_cDevices);
}

SafeRelease(&pAttributes);

return hr;
}


/*
* A function which copy attribute form source to a destination
* @ param index the index in an array
* @ param ppActivate is an Interface to store key/value pairs of an Object
* @ return HRESULT return errors warning condition on windows
*/


HRESULT DeviceList::GetDevice(UINT32 index, IMFActivate **ppActivate)
{
if (index >= Count())
{
return E_INVALIDARG;
}

*ppActivate = m_ppDevices[index];
(*ppActivate)->AddRef();

return S_OK;
}


/*
* A function which get the name of the devices
* @ param index the index in an array
* @ param ppszName Name of the device
*/


HRESULT DeviceList::GetDeviceName(UINT32 index, WCHAR **ppszName)
{
std::cout << "Get Device name" << std::endl;
if (index >= Count())
{
return E_INVALIDARG;
}

HRESULT hr = S_OK;

hr = m_ppDevices[index]->GetAllocatedString(
MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME,
ppszName,
NULL
);

return hr;
}


#include <iostream>
#include "DeviceList.h"

HRESULT UpdateDeviceList()
{
HRESULT hr = S_OK;
WCHAR *szFriendlyName = NULL;

DeviceList g_devices;


g_devices.Clear();

hr = g_devices.EnumerateDevices();

if (FAILED(hr)) { goto done; }
std::cout << "Nb devices found:"<< g_devices.Count() << std::endl;

for (UINT32 iDevice = 0; iDevice < g_devices.Count(); iDevice++)
{
//std::cout << "" << std::endl;
hr = g_devices.GetDeviceName(iDevice, &szFriendlyName);
if (FAILED(hr)) { goto done; }
std::cout << szFriendlyName << std::endl;
// The list might be sorted, so the list index is not always the same as the
// array index. Therefore, set the array index as item data.
CoTaskMemFree(szFriendlyName);
szFriendlyName = NULL;
}
std::cout << "End of EnumDeviceList" << std::endl;
done:
return hr;
}



int main()
{
std::cout <<"Main" << std::endl;
UpdateDeviceList();
while (1);
return 0;

最佳答案

在开始调用其他媒体基础 API 函数之前,您需要执行 MFStartup(MF_VERSION);

然后在 MFEnumDeviceSources 下一行对其进行初始化之前打印 m_cDevices

    std::cout << "Enumerate devices:" << m_cDevices << std::endl;
hr = MFEnumDeviceSources(pAttributes, &m_ppDevices, &m_cDevices);

修复此问题后,您的代码将开始为您获取设备。

关于c++ - Windows Media Foundation 枚举相机设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23144501/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com