首页 > 解决方案 > 在 C# 中获取网络摄像头的当前状态

问题描述

我试图弄清楚如何检查网络摄像头/视频捕获设备是否已被另一个应用程序使用而不实际激活它。

我目前的方法是使用 AForge.NET 库并使用 VideoCaptureDevice 对象的 .IsRunning 属性,如下所示:

 var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                
 foreach (FilterInfo videoDevice in videoDevices)
 {
    VideoCaptureDevice camera new AForge.Video.DirectShow.VideoCaptureDevice(videoDevice.MonikerString);
    Debug.Print(camera.IsRunning)
 }

我猜 IsRunning 属性仅适用于已使用该库启动的 VideoCaptureDevices,我需要对设备进行较低级别的 DirectShow 访问。

虽然在 C# 中使用 DirectShow 有很多方法,但即使在 C++ 中使用 DirectShow,我也无法找到检查状态的方法。我需要在这里表演一些魔法吗?

谢谢

托拜厄斯·廷佩

标签: c#c++winapidirectshowaforge

解决方案


我不完全确定这是否对您有帮助,但我找到了您的问题,因为我想编写一个自定义应用程序来控制我的忙灯。这是非常“在我的机器上工作”认证的 - 这不是试图给出一般性答案。但是,我认为它可能会对您有所帮助,并且可能是下一个在谷歌搜索时遇到此页面的人......

private static bool IsWebCamInUse()
{
    using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam\NonPackaged"))
    {
        foreach (var subKeyName in key.GetSubKeyNames())
        {
            using (var subKey = key.OpenSubKey(subKeyName))
            {
                if (subKey.GetValueNames().Contains("LastUsedTimeStop"))
                {
                    var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long) subKey.GetValue("LastUsedTimeStop") : -1;
                    if (endTime <= 0)
                    {
                        return true;
                    }
                }
            }
        }
    }

    return false;
}

推荐阅读