DirectShow .Net 实现视频

2024-01-15 04:58
文章标签 视频 实现 net directshow

本文主要是介绍DirectShow .Net 实现视频,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实例引用DirectShowLib-2005.dll,这个DLL可以到http://directshownet.sourceforge.net/直接下载使用。

1、获取视频采集设备IBaseFilter接口对象的方法

//获取所有视频设备名称
public ArrayList GetVideoInputDevice()
      { return GetDeviceCollection(FilterCategory.VideoInputDevice);}
private ArrayList GetDeviceCollection(Guid DeviceType)
      {
          ArrayList returnString = new ArrayList();
          foreach (DsDevice ds in DsDevice.GetDevicesOfCat(DeviceType))
          {
              returnString.Add(ds.Name);
          }
          return returnString;
      }

//通过获取到的视频设备名称设置采集设备的IBaseFilter对象
 public bool SetVideoInputDevice(string VideoInputDeviceName)
      {    //创建视频输入设备接口
          theVideoDevice = CreateFilter(FilterCategory.VideoInputDevice, VideoInputDeviceName); 
      }
//通过过滤器类型和过滤器名称获取IBaseFilter接口
private IBaseFilter CreateFilter(Guid category, string friendlyname)
      {
          object source = null;
          Guid iid = typeof(IBaseFilter).GUID;
          foreach (DsDevice device in DsDevice.GetDevicesOfCat(category))
          {
              if (device.Name.CompareTo(friendlyname) == 0)
              {
                  device.Mon.BindToObject(null, null, ref iid, out source);
                  break;
              }
          }

          return (IBaseFilter)source;
      }

2、初始化基本的接口对象 

private void InitInterfaces()
      {
          int hr = 0;

          // 获取IGraphBuilder接口对象
          this.m_graphBuilder = (IGraphBuilder)new FilterGraph();
          //获取ICaptureGraphBuilder2接口对象
          this.m_captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
          //获取m_graphBuilder 接口对象的IMediaControl对象
          this.m_mediaControl = (IMediaControl)this.m_graphBuilder;
          //获取m_graphBuilder 接口对象的IVideoWindow对象
          this.m_videoWindow = (IVideoWindow)this.m_graphBuilder;
          //获取m_graphBuilder 接口对象的IMediaEventEx对象
          this.m_mediaEventEx = (IMediaEventEx)this.m_graphBuilder;
          //设置ICaptureGraphBuilder2的IGraphBuilder接口对象为当前对象
          hr = this.m_captureGraphBuilder.SetFiltergraph(this.m_graphBuilder);
          DsError.ThrowExceptionForHR(hr);
          //注册事件到应用程序的窗体上
          hr = this.m_mediaEventEx.SetNotifyWindow(this.hwnPropertyPageOwner, WM_GRAPHNOTIFY, IntPtr.Zero);
          DsError.ThrowExceptionForHR(hr);
      }

 

 

 

3、开始视频预览
public void VideoPreview()
       try
      {

          int hr = 0;                  
          hr = this.m_graphBuilder.AddFilter(theVideoDevice, "Video Capture");
          DsError.ThrowExceptionForHR(hr);

          // 通过theVideoDevice(IBaseFilter)视频接口对象的Preview Pin预览
          hr = this.m_captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, theVideoDevice, null, null);
          DsError.ThrowExceptionForHR(hr);

           //插入SampleGrabber 
          m_pinStill = DsFindPin.ByCategory(theVideoDevice, PinCategory.Still, 0);

          if (m_pinStill == null)
              {
                  m_pinStill = DsFindPin.ByCategory(theVideoDevice, PinCategory.Capture, 0);
              }


              // 获取theVideoDevice的IAMVideoControl对象,对于具有Still Pin的对象可以获到,采集设备不具备Still Pin,那么该对象将为Null
              m_VidControl = theVideoDevice as IAMVideoControl;

              // 设置采集视频参数
              if (this.videoHeight + this.videoWidth + this.videoStride > 0)
              {
                  SetConfigParms(m_pinStill, this.videoWidth, this.videoHeight, 24);
              }

             //开始拍照功能所需的接口对象
              // 获得SampleGrabber对象接口
              sampGrabber = new SampleGrabber() as ISampleGrabber;

              // 配置sample grabber
              baseGrabFlt = sampGrabber as IBaseFilter;
              ConfigureSampleGrabber(sampGrabber);

              // 将sample grabber添加到图形过滤器中
              hr = m_graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
              DsError.ThrowExceptionForHR(hr);

              //通过渲染将采集设备的相关输出Pin与sample grabber对象的输入Pin连接起来
              //如果采集设备提供Still Pin,则通过Still Pin连接,否则直接使用Capture Pin连接
              if (m_VidControl!=null)
              {
                  hr = this.m_captureGraphBuilder.RenderStream(PinCategory.Still, MediaType.Video, theVideoDevice, null, baseGrabFlt);
                  DsError.ThrowExceptionForHR(hr);

              }
              else
              {
                  hr = this.m_captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, theVideoDevice, null, baseGrabFlt);
                  DsError.ThrowExceptionForHR(hr);
              }
            //设置抓取图片相关参数
              SaveSizeInfo(sampGrabber);
            //拍照功能所需的接口对象添加结束

          // 开始将视频窗口绑定到主窗体上
          hr = this.m_videoWindow.put_Owner(this.hwnVideoWindowOwner);
          DsError.ThrowExceptionForHR(hr);

          hr = this.m_videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren);
          DsError.ThrowExceptionForHR(hr);

          if (this.m_videoWindow != null)
          {
              this.m_videoWindow.SetWindowPosition(0, 0, this.videoWidth, this.videoHeight);
          }


          hr = this.m_videoWindow.put_Visible(OABool.True);
          DsError.ThrowExceptionForHR(hr);

          // 开始预览采集设备采集到的视频
          hr = this.m_mediaControl.Run();

          DsError.ThrowExceptionForHR(hr);
          m_IsPreview = true;
      }
      catch
          {
          m_IsPreview = false;
          throw new Exception("VideoPreview函数出现异常,视频预览失败!");
       
      }
      }

 

这篇关于DirectShow .Net 实现视频的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/607729

相关文章

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局

Springboot3统一返回类设计全过程(从问题到实现)

《Springboot3统一返回类设计全过程(从问题到实现)》文章介绍了如何在SpringBoot3中设计一个统一返回类,以实现前后端接口返回格式的一致性,该类包含状态码、描述信息、业务数据和时间戳,... 目录Spring Boot 3 统一返回类设计:从问题到实现一、核心需求:统一返回类要解决什么问题?

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Java利用Spire.Doc for Java实现在模板的基础上创建Word文档

《Java利用Spire.DocforJava实现在模板的基础上创建Word文档》在日常开发中,我们经常需要根据特定数据动态生成Word文档,本文将深入探讨如何利用强大的Java库Spire.Do... 目录1. Spire.Doc for Java 库介绍与安装特点与优势Maven 依赖配置2. 通过替换

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求