C#中的网络摄像头使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/233455/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:12:21  来源:igfitidea点击:

Webcam usage in C#

c#clipboardwebcam

提问by Elva

I am making a program in C# to connect to a webcam and do some image manipulation with it. I have a working application that uses win32 api (avicap32.dll) to connect to the webcam and send messages to it that sends it to the clipboard. The problem is that, while accesible from paint, reading it from the program results in null pointers.

我正在用 C# 制作一个程序来连接到网络摄像头并用它做一些图像处理。我有一个工作应用程序,它使用 win32 api (avicap32.dll) 连接到网络摄像头并向它发送消息,将其发送到剪贴板。问题是,虽然可以从 Paint 访问,但从程序中读取它会导致空指针。

This is the code I use to connect the webcam:

这是我用来连接网络摄像头的代码:

mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, 320, 240, 1024, 0);

SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0);
SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0);

And this is what I use to copy the image to the clipboard:

这就是我用来将图像复制到剪贴板的方法:

SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0);

SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0);
tempObj = Clipboard.GetDataObject();
tempImg = (System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap);

Theres some error checking which I have removed from the code to make it shorter.

我从代码中删除了一些错误检查以使其更短。

Thanks in advance :)

提前致谢 :)

采纳答案by cfeduke

I've recently started doing some hobby work in this area.

我最近开始在这个领域做一些业余爱好工作。

We settled on using the OpenCVlibrary with the opencvdotnetwrapper. It supports capturing frames from a webcam:

我们决定使用带有opencvdotnet包装器的OpenCV库。它支持从网络摄像头捕获帧:

using (var cv = new OpenCVDotNet.CVCapture(0))
{
    var image = cv.CreateCompatibleImage();
    // ...
    cv.Release();
}

And if you're doing image manipulation, OpenCV's image processing algorithms have been wrapped within the OpenCVDotNet.Algs assembly.

如果您正在进行图像处理,OpenCV 的图像处理算法已包含在 OpenCVDotNet.Algs 程序集中。

If you decide to go this route be sure to install OpenCV version 1.0 (and install it to "c:\program files\opencv" if you are on Vista 64-bit, or "mklink OpenCV 'c:\program files (x86)\OpenCV`" from the correct directory or else opencvdotnet will not install).

如果您决定走这条路线,请确保安装 OpenCV 1.0 版(如果您使用的是 64 位 Vista,请将其安装到“c:\program files\opencv”,或者“mklink OpenCV 'c:\program files (x86)”) \OpenCV`" 来自正确的目录,否则 opencvdotnet 将不会安装)。

回答by Rob Prouse

Have you tried Clipboard.GetImage()? You could also try the various Clipboard.Contains*() methods to see what format the data is stored in the clipboard.

你试过 Clipboard.GetImage() 吗?您还可以尝试各种 Clipboard.Contains*() 方法来查看数据存储在剪贴板中的格式。

回答by ctacke

There are really two ways to get camera data into your application, DirectShow and WIA. Microsoft recommends that you use WIA, and the interface for WIA is fairly simple to wrap your brain around. I created and published an open source WIA desktop librarybased on work I did a while ago.

有两种方法可以将相机数据导入您的应用程序,DirectShow 和 WIA。Microsoft 建议您使用WIAWIA的界面相当简单,让您大开眼界。我根据前段时间的工作创建并发布了一个开源WIA 桌面库

Now the problem with WIA in some cases is that it's too simple. For example, if you want to adjust camera properties (like frame rate, resolution, etc) then WIA falls down. Microsoft deprecated DirectShow, but they really didn't give us any replacement that has all of its capabilities, and I've found that it continues to work fine on all existing platforms (it's very entrenched, so I can't imagine support going away any time soon).

在某些情况下,WIA 的问题在于它太简单了。例如,如果您想调整相机属性(如帧速率、分辨率等),那么 WIA 就会下降。Microsoft 弃用了 DirectShow,但他们确实没有给我们任何具有其所有功能的替代品,而且我发现它在所有现有平台上都能继续正常工作(它非常根深蒂固,所以我无法想象支持会消失)很快)。

There is a very good DirectShow library over at SourceForge. The only "problem" with it is it's really complex and that stems from the fact that DShow is just so damned complex and confusing in the first place. There are lots of things that the wrapper can do that just aren't easy to work out, but they do provide samples for a lot of common use cases like showing video or capturing a frame. If you want to add overlays, or insert other filters, it can do it, but be forewarned that it's not at all straightforward.

SourceForge 上有一个非常好的DirectShow 库。它唯一的“问题”是它真的很复杂,这源于这样一个事实,即 DShow 一开始就非常复杂和令人困惑。包装器可以做的很多事情并不容易解决,但它们确实为许多常见用例提供了示例,例如显示视频或捕获帧。如果你想添加叠加层,或者插入其他过滤器,它可以做到,但预先警告它一点也不简单。

回答by nikib3ro

Take a look at this article: http://www.codeproject.com/KB/miscctrl/webcam_c_sharp.aspx

看看这篇文章:http: //www.codeproject.com/KB/miscctrl/webcam_c_sharp.aspx

It is way too simpler than installing and using OpenCVNetWrapper.

它比安装和使用 OpenCVNetWrapper 太简单了。

回答by Shachar Weis

OpenCV capture, EMGU capture and all other capture libraries I have tried
all have the same problem: You cannot go higher than 640x480 programatically
(without opening the windows video source window).

OpenCV 捕获、EMGU 捕获和我尝试过的所有其他捕获库都有
相同的问题:您不能以编程方式超过 640x480
(不打开 Windows 视频源窗口)。

I suggest using this (which does work):
https://github.com/ofTheo/videoInput

我建议使用这个(确实有效):https:
//github.com/ofTheo/videoInput

回答by Fredrik Johansson

This was also asked in How to get web cam images in C#?and you might find the following useful (Sorry for spamming, but this really helps answering the question):

这也在如何在 C# 中获取网络摄像头图像?您可能会发现以下内容很有用(对不起,垃圾邮件,但这确实有助于回答问题):

I've just released the complete sourcecode of my Windows app CamTimer (written in .NET/C#). Download/view the complete code (with working Webcam examples) at https://github.com/johanssonrobotics/CamTimer

我刚刚发布了我的 Windows 应用程序 CamTimer(用 .NET/C# 编写)的完整源代码。在https://github.com/johanssonrobotics/CamTimer下载/查看完整代码(带有工作网络摄像头示例)

Happy coding!

快乐编码!