0

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

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);

我究竟做错了什么?

我几乎所有:

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);
}
4

1 回答 1

0

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

此外,可能还有一些与明显错误相关的其他问题 - 您没有将图形置于活动模式,您对所拥有的拓扑有错误的印象,您使用了错误的接口,您的代码有线程问题等。

考虑到如此广泛的可能原因,从一个简单的问题开始:在通话时,您是否已经将视频帧直观地呈现给您?

于 2013-05-07T12:05:20.687 回答