IBasicVideo CetCurrentImage灾难性故障(DirectShow.NET) [英] IBasicVideo CetCurrentImage catastrophic failure(DirectShow.NET)

查看:122
本文介绍了IBasicVideo CetCurrentImage灾难性故障(DirectShow.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DirectShow.NET和IBasicVideo CetCurrentImage从网络摄像头抓取图像。但是我只会在第二次调用GetCurrentImage时遇到灾难性的失败。
我特别在做什么:

  IBasicVideo bv =(IBasicVideo)graph; 
IntPtr bvp =新的IntPtr();
int size = 0;
int hr = bv.GetCurrentImage(ref size,IntPtr.Zero);
DsError.ThrowExceptionForHR(hr);
bvp = Marshal.AllocCoTaskMem(size);
hr = bv.GetCurrentImage(ref size,bvp);
DsError.ThrowExceptionForHR(hr);
位图图像=新位图(480,320,480 *(24/8),System.Drawing.Imaging.PixelFormat.Format24bppRgb,bvp);
image.Save(path);

我在做什么错了?



我所有的东西都比这更重要了:

  IGraphBuilder graph = null; 
IMediaEventEx eventEx = null;
IMediaControl控件= null;
ICaptureGraphBuilder2 capture = null;
IBaseFilter srcFilter = null;
public IVideoWindow videoWindow = null;
IntPtr videoWindowHandle = IntPtr.Zero;

public void GetPreviewFromCam()
{
graph =(IGraphBuilder)(new FilterGraph());
capture =(ICaptureGraphBuilder2)(新的CaptureGraphBuilder2());
eventEx =(IMediaEventEx)图;
control =(IMediaControl)graph;
videoWindow =(IVideoWindow)graph;
videoWindowHandle = hVideoWindow;
eventEx.SetNotifyWindow(hVideoWindow,WM_GRAPHNOTIFY,IntPtr.Zero);

int hr;

//将过滤器图形附加到捕获图形
hr = capture.SetFiltergraph(graph);
DsError.ThrowExceptionForHR(hr);

//查找捕获设备并将其绑定到srcFilter
FindCaptureDevice();

//将捕获过滤器添加到图形中。
hr = graph.AddFilter(srcFilter, Video Capture);
DsError.ThrowExceptionForHR(hr);

//在视频捕获过滤器上渲染预览图钉
//使用它代替graph-> RenderFile
hr = capture.RenderStream(PinCategory.Preview,MediaType。 Video,srcFilter,null,null);
DsError.ThrowExceptionForHR(hr);

hr = control.Run();
DsError.ThrowExceptionForHR(hr);
}


解决方案

IBasicVideo :: GetCurrentImage 不必无条件成功。它所做的是将调用转移到图形中的视频渲染器(如果您没有调用,或者您有一个奇怪的非渲染器过滤器,该过滤器意外地实现了该接口,则失败),然后渲染器将尝试为您获取图像。如果渲染器在不兼容模式下运行,则渲染器可能会失败(无窗视频渲染器没有 IBasicVideo -此处可能会失败),或者渲染器尚未接收到任何视频帧收到一份副本给您,即通话为时过早。



此外,可能还有其他一些与明显错误相关的问题-您没有将图形置于活动模式,对拓扑结构的印象不正确,您使用的接口错误,代码中存在线程问题等。 ,您是否已经在视觉上呈现了视频帧?


I'm trying to grab image from webcam using DirectShow.NET and IBasicVideo CetCurrentImage. But I only get catastrophic failure on second call GetCurrentImage. What I'm doing particularly:

IBasicVideo bv = (IBasicVideo)graph;
IntPtr bvp = new IntPtr();
int size = 0;
int hr = bv.GetCurrentImage(ref size, IntPtr.Zero);
DsError.ThrowExceptionForHR(hr);
bvp = Marshal.AllocCoTaskMem(size);
hr = bv.GetCurrentImage(ref size, bvp);
DsError.ThrowExceptionForHR(hr);
Bitmap image = new Bitmap(480, 320, 480 * (24 / 8), System.Drawing.Imaging.PixelFormat.Format24bppRgb, bvp);
image.Save(path);

What am I doing wrong?

Prety much all I have:

IGraphBuilder graph = null;
IMediaEventEx eventEx = null;
IMediaControl control = null;
ICaptureGraphBuilder2 capture = null;
IBaseFilter srcFilter = null;
public IVideoWindow videoWindow = null;
IntPtr videoWindowHandle = IntPtr.Zero;

public void GetPreviewFromCam()
{
    graph = (IGraphBuilder)(new FilterGraph());
    capture = (ICaptureGraphBuilder2)(new CaptureGraphBuilder2());
    eventEx = (IMediaEventEx)graph;
    control = (IMediaControl)graph;
    videoWindow = (IVideoWindow)graph;
    videoWindowHandle = hVideoWindow;
    eventEx.SetNotifyWindow(hVideoWindow, WM_GRAPHNOTIFY, IntPtr.Zero);

    int hr;

    // Attach the filter graph to the capture graph
    hr = capture.SetFiltergraph(graph);
    DsError.ThrowExceptionForHR(hr);

    // Find capture device and bind it to srcFilter
    FindCaptureDevice();

    // Add Capture filter to our graph.
    hr = graph.AddFilter(srcFilter, "Video Capture");
    DsError.ThrowExceptionForHR(hr);

    // Render the preview pin on the video capture filter
    // Use this instead of graph->RenderFile
    hr = capture.RenderStream(PinCategory.Preview, MediaType.Video, srcFilter, null, null);
    DsError.ThrowExceptionForHR(hr);

    hr = control.Run();
    DsError.ThrowExceptionForHR(hr);
}

解决方案

IBasicVideo::GetCurrentImage does not have to unconditionally succeed. What it does is forwarding of the call to video renderer in your graph (fails if you don't have one, or you have a weird non-renderer filter which unexpectedly implements the interface), then the renderer would attempt to get you the image. The renderer might fail if it is operating in incompatible mode (windowless video renderers don't have IBasicVideo - might fail here), or the renderer yet did not receive any video frame to have a copy delivered to you, that is the call is premature.

Additionally, there might be a handful of other issues related to obvious bugs - you did not put the graph into active mode, you are under wrong impression about topology you are having, you are using wrong interface, your code has threading issues etc.

With a specter of possibly causes this wide, start with a simple question: at the time of the call, do you have the video frame already presented to you visually?

这篇关于IBasicVideo CetCurrentImage灾难性故障(DirectShow.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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