我目前正在尝试将 ISampleGrabber 过滤器添加到我的程序中。目前,该程序捕获并显示预览到我的 Windows 窗体,但是当我尝试使用其他示例添加我自己的 ISampleGrabber 过滤器时,程序的网络摄像头部分完全停止工作。
IVideoWindow videoWindow = null;
IMediaControl mediaControl = null;
IMediaEventEx mediaEventEx = null;
IGraphBuilder graphBuilder = null;
ICaptureGraphBuilder2 captureGraphBuilder = null;
IBaseFilter baseFilterForSampleGrabber;
ISampleGrabber sampleGrabber;
AMMediaType mediaType;
VideoInfoHeader videoInfoHeader;
public void capturePreview()
{
int hr = 0;
IBaseFilter baseFilter = null;
try
{
interfaces();
hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
DsError.ThrowExceptionForHR(hr);
baseFilter = getListOfDevices();
hr = this.graphBuilder.AddFilter(baseFilter, "Webcam");
DsError.ThrowExceptionForHR(hr);
sampleGrabber = new SampleGrabber() as ISampleGrabber;
baseFilterForSampleGrabber = (IBaseFilter)new SampleGrabber();
if (baseFilterForSampleGrabber == null)
{
Marshal.ReleaseComObject(sampleGrabber);
sampleGrabber = null;
}
mediaType = new AMMediaType();
mediaType.majorType = MediaType.Video;
mediaType.subType = MediaSubType.RGB24;
mediaType.formatType = FormatType.VideoInfo;
//int width = videoInfoHeader.BmiHeader.Width;
//int height = videoInfoHeader.BmiHeader.Height;
//int size = videoInfoHeader.BmiHeader.ImageSize;
//mediaType.formatPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(videoInfoHeader));
//Marshal.StructureToPtr(videoInfoHeader, mediaType.formatPtr, false);
hr = sampleGrabber.SetMediaType(mediaType);
DsUtils.FreeAMMediaType(mediaType);
hr = graphBuilder.AddFilter(baseFilterForSampleGrabber, "ISampleGrabber Filter");
DsError.ThrowExceptionForHR(hr);
hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, baseFilter, baseFilterForSampleGrabber, null);
DsError.ThrowExceptionForHR(hr);
Marshal.ReleaseComObject(baseFilter);
videoWindowSetup();
hr = sampleGrabber.SetBufferSamples(true);
DsError.ThrowExceptionForHR(hr);
hr = this.mediaControl.Run();
DsError.ThrowExceptionForHR(hr);
}
catch
{
MessageBox.Show("Error...Try restart");
}
}
上面的代码包含我当前的图表以及我在每个示例中看到的重复的起始 ISampleGrabber 代码,但是当我添加注释代码时,这是程序停止的时候。我不知道问题出在哪里,并假设我至少应该在继续添加图表之前对基础知识进行排序。
如果我解决了这个问题,对完成此图表所需的任何其他帮助将非常有帮助,我的目标是将捕获的帧转换为位图,以便我可以立即编辑它们,例如添加十字准线,并在窗口中显示它们表格一旦被立即编辑。
任何帮助表示赞赏:)