Windows Media Foundation 枚举相机设备

2023-12-02

enter image description hereI would like to enumerate the camera devices on my computer using Windows Media Foundation, I used the code on Microsoft : http://msdn.microsoft.com/en-us/library/windows/desktop/dd940326(v=vs.85).aspx I reproduced the same code they use here : 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;

}


你应该做的MFStartup(MF_VERSION);在开始调用其他 Media Foundation API 函数之前。

然后你打印m_cDevices在下面一行被初始化之前MFEnumDeviceSources.

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows Media Foundation 枚举相机设备 的相关文章

随机推荐

  • C++ 除以 0

    我正在运行长时间模拟 我将结果记录到向量中以计算有关数据的统计信息 我意识到 从理论上讲 这些样本可能是除以零的结果 这只是理论上的 我很确定事实并非如此 为了避免修改代码后重新运行模拟 我想知道这种情况下会发生什么 我能知道是否发生了除以
  • 这两种使用express中间件的方法有区别吗?

    我遇到过两种不同的方式来定义 express use 中间件 我想知道它们之间是否有任何区别 或者它是否只是语法糖 A const app express app use cors app use responseTime app use
  • 如何使用 jQuery 添加基于 URL 的“选定”类?

    我的 wikispace 主题中有一个侧边栏链接列表 当前使用 jQuery 根据 com 之后的 URL 将类应用到每个侧边栏链接 您可以在下面的代码中看到这一点的应用 div class WikiCustomNav WikiElemen
  • 使用 Python 从私钥输入中提取公钥

    我需要从私钥生成公钥 而不需要像我们在 sshgen 中那样在本地临时位置 所以我使用这个 这里我将我的私钥作为输入传递 如下所示 在执行时 python codekey py BEGIN RSA PRIVATE KEY nMIhhhhhh
  • Web 作业上的 Windows Azure 管理库认证错误

    我构建了一个 Azure Web 作业控制台 它引用了 Windows Azure 管理库 我尝试使用公共设置方法来验证我的应用程序 该程序在我的本地运行良好 但在 Azure Web 作业上失败并出现 X509Certificates 错
  • MySQL 错误 1005?

    我正在尝试创建一个数据库 但收到一个奇怪的错误 这是我的代码 DROP TABLE IF EXISTS Person DROP TABLE IF EXISTS Address DROP TABLE IF EXISTS Email DROP
  • 如何从 CUDA C 调用 ptx 函数?

    我正在尝试找到一种从 CUDA C 调用 ptx 函数 func 的方法 假设我有一个像这样的 ptx 函数 func reg s32 res inc ptr reg s32 ptr reg s32 inc add s32 res ptr
  • 为什么名称函数表达式在函数体外部不可用[重复]

    这个问题在这里已经有答案了 命名函数表达式定义为 var ninja function myNinja 有一种我无法理解的行为 看看下面的代码 var ninja function myNinja console log typeof my
  • 如何按键对 JSON 进行分组并按其计数排序?

    我从类似于此的 jsonlines 文件开始 kw foo age 1 kw foo age 1 kw foo age 1 kw bar age 1 kw bar age 1 请注意 每一行都是有效的 json 但整个文件不是 我正在寻找的
  • 我无法在我的 github 博客上更改 jekyll 语法荧光笔

    我创建了我的 github 博客 我想将语法荧光笔更改为 rouge 我做这个 gem install rouge rougify style monokai sublime gt assets css syntax css default
  • 处理 Android 中最近的应用程序点击和主页按下

    我正在为孩子们制作一个应用程序 一旦你进入该应用程序 如果没有我在菜单中要求的密码 你就无法退出 现在我希望如果在应用程序内按下应用程序中的主页按钮 它应该保持相同的活动 我搜索了很多 但到处的解决方案都是在清单中添加过滤器 但这些对我来说
  • 如果线程 B 希望看到线程 A 所做的更改,是否只能对 volatile 变量进行最后的更改,而不是对所有变量进行更改?

    我看过这个答案 并说明了如何 在新的内存模型下 当线程A写入易失性 变量 V 线程 B 从 V 读取任何变量值 现在保证在写入 V 时对 A 可见 对 B 可见 因此 给出示例 public class Main static int va
  • 如何将 Swing 应用程序转换为 Applet?

    我使用 Swing 应用程序框架创建了一个桌面应用程序 现在如何将其转换为小程序 主类扩展了 SingleFrame Application 编辑 这是起始类 使用 NetBeans GUI 构建器 public class PhotoAp
  • 如何在不更改url的情况下将所有页面请求重定向到一个页面

    我想将我网站上的所有页面 包括索引 重定向到UnderWork html我正在使用 htaccess用这个代码 RewriteEngine on RewriteRule UnderWork html 而且效果很好 现在我正在向我的 htac
  • Spring Security 更改 spring_security_login 表单

    我正在使用 spring security 我想知道如何更改默认登录表单 我发现我需要指向我的新表单位置 我想保留具有所有登录异常显示的现有默认表单的现有功能 所以我必须首先知道如何重现它 在我的研究中我遇到了它 http www code
  • 在 R 中:获取第一个标点符号之前的所有数字字符

    我有一个向量s字符串 或 NA 并且希望在第一次出现标点符号之前获得一个长度相同的向量 s lt c ABC1 2 22A 2 NA 我想要这样的结果 1 ABC1 22A NA 您可以使用以下类似 Perl 的正则表达式从第一个点删除所有
  • Webview保存页面状态

    我正在使用的网络视图有一个小问题 我正在尝试使用网络视图来允许用户填写注册表 当用户在一个会话中完成表单时 这种方法效果很好 但如果用户锁定手机 则 webview 活动将被破坏 我的记忆中有两项活动 其中一项是内存密集型的 我认为我遇到的
  • 从字符串 "" 到 long 的转换无效

    即使经过大约一个小时的研究 我也遇到了无法解决的错误 从字符串 Waseem PC Waseem 到 long 的转换无效 这个错误真的很烦人 我尝试了一切 我非常感谢您的帮助 我很想对你的答案竖起大拇指 但我必须有更大的代表 这是我的代码
  • 在网络视图中从相机上传图像不起作用

    我一直在尝试从 facebook 通过 webview 从画廊和相机上传 Workplace 中的图像 从图库中它工作正常 但从相机中图像不会出现在上传中 我看过类似的帖子有这个问题this and this但我不明白有什么问题 这是我的课
  • Windows Media Foundation 枚举相机设备

    I would like to enumerate the camera devices on my computer using Windows Media Foundation I used the code on Microsoft