使用DirectShowLib-2005的网络摄像头拍摄的照片和视频 [英] Photo and video with webcam using DirectShowLib-2005

查看:382
本文介绍了使用DirectShowLib-2005的网络摄像头拍摄的照片和视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c#应用程序中的网络摄像头拍照。我发现要使用DirectShowLib。经过艰苦的研究,我找到了一个使用网络摄像头的示例,该摄像头可以完美地在屏幕上显示视频,但是我无法随意使用位图图像进行保存。

I'm trying to take pictures with a webcam in my c# application. I found out to use DirectShowLib. After a hard research, I found an example of how using the webcam, it displays the video on screen perfect, but it can't take the bitmap image for saving it at my will.

我发现可以用来拍照的代码示例

sample of the code I found to take the picure would be

    public Bitmap snapImage()
    {
        IVMRWindowlessControl9 windowlessCtrl = null;
        IBaseFilter vmr9 = null;
        vmr9 = (IBaseFilter)new DirectShowLib.VideoMixingRenderer9();
        DirectShowLib.IVMRFilterConfig9 filterConfig = (DirectShowLib.IVMRFilterConfig9)vmr9;
        int hr = filterConfig.SetNumberOfStreams(1);
        hr = filterConfig.SetRenderingMode(DirectShowLib.VMR9Mode.Windowless);

        windowlessCtrl = (IVMRWindowlessControl9)vmr9;
        hr = windowlessCtrl.SetVideoClippingWindow(this.PreviewWindow.Handle);
        hr = windowlessCtrl.SetAspectRatioMode(VMR9AspectRatioMode.LetterBox);

        IntPtr currentImage = IntPtr.Zero;

        Bitmap bmp = null;

        //this is the line in wich I have problems
        hr = windowlessCtrl.GetCurrentImage(out currentImage);

        BitmapInfoHeader structure = new BitmapInfoHeader();
        Marshal.PtrToStructure(currentImage, structure);

        bmp = new Bitmap(structure.Width, structure.Height, (structure.BitCount / 8) * structure.Width, System.Drawing.Imaging.PixelFormat.Format32bppArgb, new IntPtr(currentImage.ToInt64() + 40));

        return bmp;
    }

我得到:错误HRESULT E_FAIL已从调用返回COM组件,错误代码-2147467259

And I got: "Error HRESULT E_FAIL has been returned from a call to a COM component" with error code -2147467259

可以在此页面找到我找到的示例 http://www.codeproject.com/Articles/34663/DirectShow-Examples-for-Using-SampleGrabber-for-Gr

Can sample I found is in this page http://www.codeproject.com/Articles/34663/DirectShow-Examples-for-Using-SampleGrabber-for-Gr

有人可以帮我拍摄快照吗?或者有人可以告诉我该怎么做(显示视频并拍摄网络摄像头快照)

Can anyone help me to take the snapshot? or can any one tell me how to do what I want to do (show video and take snapshot of webcam)

谢谢

推荐答案

GetCurrentImage 可能返回 E_FAIL (设计使然)直到显示真实的视频帧为止,这是异步发生的。要对此进行检查,只需在有问题的呼叫之前显示一个消息框,等到视频开始流式传输,然后关闭该框,您就应该可以像预期的那样正常通话。

GetCurrentImage might return E_FAIL (behavior by design) until a real video frame is displayed, which takes place asynchronously. To check this out simply display a message box before the call in question, wait until video starts streaming, close the box and you should have the call working as you originally expected.

这篇关于使用DirectShowLib-2005的网络摄像头拍摄的照片和视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆