/// <summary> /// 配置SampleGrabber /// </summary> /// <param name="sampGrabber"></param> private void ConfigureSampleGrabber(ISampleGrabber sampGrabber) { AMMediaType media = new AMMediaType(); media.majorType = MediaType.Video; media.subType = MediaSubType.RGB24; media.formatType = FormatType.VideoInfo; int hr = sampGrabber.SetMediaType(media); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(media); media = null; hr = sampGrabber.SetCallback(this, 1); DsError.ThrowExceptionForHR(hr); }
/// <summary> /// 读取视频头文件信息 /// </summary> /// <param name="sampleGrabber"></param> private void GetVideoHeaderInfo(ISampleGrabber sampleGrabber) { // Get the media type from the SampleGrabber AMMediaType media = new AMMediaType(); int hr = sampleGrabber.GetConnectedMediaType(media);//读取视频文件信息 DsError.ThrowExceptionForHR(hr); if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // 获取视频文件信息 VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader)); _VideoWidth = videoInfoHeader.BmiHeader.Width; _VideoHeight = videoInfoHeader.BmiHeader.Height; _VideoBitCount = videoInfoHeader.BmiHeader.BitCount; _ImageSize = videoInfoHeader.BmiHeader.ImageSize; DsUtils.FreeAMMediaType(media); media = null; }
/// <summary> /// 构建捕获图 /// </summary> private void CreateGraph(string Resolution, int Frames) { if (_graphBuilder != null) { return; } _graphBuilder = (IFilterGraph2) new FilterGraph(); // 获取IFilterGraph2接口对象 _captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); //获取ICaptureGraphBuilder2接口对象 int hr = _captureGraphBuilder.SetFiltergraph(this._graphBuilder); //将过滤器图形附加到捕获图 DsError.ThrowExceptionForHR(hr); //将视频输入设备添加到图形 hr = _graphBuilder.AddFilter(_theCamera, "source filter"); DsError.ThrowExceptionForHR(hr); //将视频压缩器过滤器添加到图形 if (_theCameraCompressor != null) { hr = _graphBuilder.AddFilter(_theCameraCompressor, "devicecompressor filter"); DsError.ThrowExceptionForHR(hr); } //将音频输入设备添加到图形 if (_theAudio != null) { hr = _graphBuilder.AddFilter(_theAudio, "audio filter"); DsError.ThrowExceptionForHR(hr); } //将音频压缩器过滤器添加到图形 if (_theAudioCompressor != null) { hr = _graphBuilder.AddFilter(_theAudioCompressor, "audiocompressor filter"); DsError.ThrowExceptionForHR(hr); } _mediaControl = (IMediaControl)this._graphBuilder;//获取IMediaControl接口对象 m_PictureReady = new ManualResetEvent(false); //添加采样器接口. sampleGrabber = new SampleGrabber() as ISampleGrabber; // 配置SampleGrabber。添加预览回调 ConfigureSampleGrabber(sampleGrabber); hr = _graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "Frame Callback");// 将SampleGrabber添加到图形. DsError.ThrowExceptionForHR(hr); AMMediaType mediaType = new AMMediaType(); IntPtr pmt = IntPtr.Zero; object oVideoStreamConfig;//视频流配置信息 hr = _captureGraphBuilder.FindInterface(PinCategory.Capture, MediaType.Video, _theCamera, typeof(IAMStreamConfig).GUID, out oVideoStreamConfig); if (!(oVideoStreamConfig is IAMStreamConfig videoStreamConfig)) { throw new Exception("Failed to get IAMStreamConfig"); } hr = videoStreamConfig.GetFormat(out pmt); if (hr != 0) { Marshal.ThrowExceptionForHR(hr); } Marshal.PtrToStructure(pmt, mediaType); VideoInfoHeader videoInfoHeader = new VideoInfoHeader(); Marshal.PtrToStructure(mediaType.formatPtr, videoInfoHeader); // 设置帧率 if (Frames > 0) { videoInfoHeader.AvgTimePerFrame = 10000000 / Frames; } //获取当前摄像头所支持的所有分辨率 var allResolution = GetCameraSupportResolution(); //当前所设置的分辨率此摄像头不支持 if (!allResolution.Contains(Resolution)) { Resolution = allResolution[0]; } // 设置宽度 设置高度 if (!string.IsNullOrEmpty(Resolution) && Resolution.Split('*').Length > 1) { videoInfoHeader.BmiHeader.Width = Convert.ToInt32(Resolution.Split('*')[0]); videoInfoHeader.BmiHeader.Height = Convert.ToInt32(Resolution.Split('*')[1]); } // 复制媒体结构 Marshal.StructureToPtr(videoInfoHeader, mediaType.formatPtr, false); // 设置新的视频格式 hr = videoStreamConfig.SetFormat(mediaType); DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(mediaType); mediaType = null; }