在 Windows 下以编程方式禁用网络摄像头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7696290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling the webcam programmatically under Windows
提问by rid
How can I disable the attached or built-in webcam(s) programmatically under Windows?
如何在 Windows 下以编程方式禁用附加或内置的网络摄像头?
By disabling them, I mean that any program trying to access the cameras should either:
通过禁用它们,我的意思是任何尝试访问相机的程序都应该:
- be unable to access them
- be able to access them, but instead of the video feed, they should receive a black picture
- 无法访问它们
- 能够访问它们,但他们应该收到一张黑色图片而不是视频
Why I want to do this:
我为什么要这样做:
I have a laptop with a webcam. There's no easy way to disable it via software. I want to create an application with a button that disables the webcam, so that any application that wants to use it will be unable to, and a button to re-enable it. I don't want to put anything over the cam physically, otherwise I wouldn't be asking this question here.
我有一台带网络摄像头的笔记本电脑。没有简单的方法可以通过软件禁用它。我想创建一个带有禁用网络摄像头按钮的应用程序,以便任何想要使用它的应用程序都无法使用它,以及一个重新启用它的按钮。我不想在摄像头上放任何东西,否则我不会在这里问这个问题。
采纳答案by Simon Mourier
Here are two links here on SO to enable/disable a device programmatically:
这里有两个链接,用于以编程方式启用/禁用设备:
How do I disable a system device?
Win32 API function to programmatically enable/disable device
There is also a tool, DevConthat you could automate.
还有一个可以自动化的工具DevCon。
回答by Roman R.
A simple yet effective solution is to have an application on background which locks the camera by active capture from it (such as capture in minimal resolution, to
Null Renderer
filter, or perhaps paused capture would be even better). As long as your app acquired access to the camera noone else could be capturing from it.Another solution, more complicated and somewhat dangerous as for interaction with other applications, is to redefine (hook, substitute)
System Device Enumerator
class and intercept its enumeration of video input device category. Having hold of this, you can hide the camera and do not pass its enumeration to the application. The keyword here isCoTreatAsClass
and this requires that you are familiar withCOM
. This will still let legacy applications access the camera viaVideo for Windows API
(which you might be OK with).
一个简单而有效的解决方案是在后台安装一个应用程序,通过主动捕捉来锁定相机(例如以最小分辨率捕捉,
Null Renderer
过滤,或者暂停捕捉可能会更好)。只要您的应用程序获得了对相机的访问权限,就没有其他人可以从中捕获。对于与其他应用程序的交互而言,另一种更复杂且有些危险的解决方案是重新定义(挂钩,替代)
System Device Enumerator
类并拦截其对视频输入设备类别的枚举。掌握了这一点,您可以隐藏相机并且不将其枚举传递给应用程序。这里的关键词是CoTreatAsClass
,这需要你熟悉COM
. 这仍然可以让遗留应用程序通过Video for Windows API
(您可能会同意)访问相机。
回答by Jim Rhodes
You can write an application using DirectShow that takes control of the web cam which will prevent other applications from accessing it. You will probably have to use C++ and you will probably need Visual Studio 2005 or greater.
您可以使用 DirectShow 编写一个应用程序来控制网络摄像头,这将阻止其他应用程序访问它。您可能必须使用 C++,并且可能需要 Visual Studio 2005 或更高版本。
Start by downloading the Windows SDK: http://go.microsoft.com/fwlink/?LinkID=129787After installing the SDK, open Visual Studio and open the solution at:
首先下载 Windows SDK:http: //go.microsoft.com/fwlink/?LinkID= 129787安装 SDK 后,打开 Visual Studio 并在以下位置打开解决方案:
[SDK Root]\Samples\Multimedia\DirectShow\baseclasses\baseclasses.sln
[SDK 根目录]\Samples\Multimedia\DirectShow\baseclasses\baseclasses.sln
[SDK Root] will typically be: C:\Program Files\Microsoft SDKs\Windows\v7.1
[SDK Root] 通常为:C:\Program Files\Microsoft SDKs\Windows\v7.1
Build the solution which will create strmbase.lib (release) or strmbasd.lib (debug). This library is needed to build a DirectShow application.
构建将创建 strmbase.lib(发布)或 strmbasd.lib(调试)的解决方案。构建 DirectShow 应用程序需要此库。
Next open the AmCap sample at:
接下来在以下位置打开 AmCap 示例:
[SDK Root]\Samples\multimedia\directshow\capture\amcap\amcap.sln
[SDK Root]\Samples\multimedia\directshow\capture\amcap\amcap.sln
Build and run AmCap. You should now be able to control the web cam. You can use AmCap as a basis for what you want to do or you may even be able to just modify AmCap for your needs.
构建并运行 AmCap。您现在应该可以控制网络摄像头了。您可以使用 AmCap 作为您想做的事情的基础,或者您甚至可以仅根据需要修改 AmCap。
Hope this helps.
希望这可以帮助。
回答by Escovado
The only way you can block a user from accessing a camera connected to the system is to uninstall the driver for it. That would be an extremely unfriendlything to do!!
阻止用户访问连接到系统的相机的唯一方法是卸载它的驱动程序。那将是一件非常不友好的事情!!
If you want to block the use of a system device from within your application, then all you have to do is remove the offending device from the list of available devices shown to the user.
如果您想在您的应用程序中阻止使用系统设备,那么您所要做的就是从向用户显示的可用设备列表中删除有问题的设备。
Question: Why do you want to do this?
问题:你为什么要这样做?
回答by Gabriel
Well you don't need programming for this... I'm sure your aware of what the task manager is. Just create a .bat file (aka the .exe file) that kills the webcam process... and have another .bat file to open it back up? I mean this would work for any webcam on windows, BUT if you wanted to do it programatically (is that word right? lol) then you'd have to supply us with the information on what type of webcam it is and such...
那么你不需要为此编程......我相信你知道任务管理器是什么。只需创建一个 .bat 文件(也称为 .exe 文件)来终止网络摄像头进程......并有另一个 .bat 文件来打开它?我的意思是这适用于 Windows 上的任何网络摄像头,但是如果您想以编程方式进行(这个词对吗?哈哈)那么您必须向我们提供有关它是什么类型的网络摄像头的信息等等......
and just in case you don't know how to make a bat file: open up an empty .txt file type in:
以防万一您不知道如何制作 bat 文件:打开一个空的 .txt 文件,输入:
command.com taskkill /IM notepad.exe
command.com taskkill /IM notepad.exe
and save the file as anything.bat, and be sure to change the file type(right under where you name the file) click on where it says Save as type:*txt, and select all files instead.
并将文件另存为anything.bat,并确保更改文件类型(就在您命名文件的下方)单击“另存为类型:*txt”的位置,然后选择所有文件。