DirectShow .Net 实现视频

 、获取视频采集设备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) == )
{
device.Mon.BindToObject(null, null, ref iid, out source);
break;
}
} return (IBaseFilter)source;
} 、初始化基本的接口对象 private void InitInterfaces()
{
int hr = ; // 获取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);
} 、开始视频预览
public void VideoPreview()
try
{ int hr = ;
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, ); if (m_pinStill == null)
{
m_pinStill = DsFindPin.ByCategory(theVideoDevice, PinCategory.Capture, );
} // 获取theVideoDevice的IAMVideoControl对象,对于具有Still Pin的对象可以获到,采集设备不具备Still Pin,那么该对象将为Null
m_VidControl = theVideoDevice as IAMVideoControl; // 设置采集视频参数
if (this.videoHeight + this.videoWidth + this.videoStride > )
{
SetConfigParms(m_pinStill, this.videoWidth, this.videoHeight, );
} //开始拍照功能所需的接口对象
// 获得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(, , 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 实现视频的更多相关文章

  1. DirectShow 获取音视频输入设备列表

    开发环境:Win10 + VS2015 本文介绍一个 "获取音频视频输入设备列表" 的示例代码. 效果图 代码下载 代码下载(VC2015):Github - DShow_simp ...

  2. DirectShow建立一个视频捕捉程序

    DirectShow 提供了用应用程序从适当的硬件中捕捉和预览音/视频的能力.数据源包括:VCR,camera,TV tuner,microphone,或其他的数据源.一个应用程序可以立刻显示捕捉的数 ...

  3. 摘录DirectShow数据,视频采集

    DirectShow在,数据流(Data Flow)它们是依次流过每Filter的.管理其数据具有其自己的方法,并且并没有向用户提供一个统一的接口,供用户操作数据流.这里以提取视频採集在的每帧为位图数 ...

  4. 基于DirectShow的MPEG-4视频传输系统的研究与实现

    1 引言     近年来,随着国民经济的发展,社会各个部门对于视频监视系统的需求越来越多.但目前的很多监视系统都跟具体的硬件相关,必须要具体的采集卡的支持才能实现.所以有必要开发一种具有通用性的视频监 ...

  5. Directshow 判断音视频设备是否被占用<转>

    直接上代码吧: 代码是参考网上大神分享的,在原基础上做了些修改(只检测视频设备): int DeviceIsBusy(char *videoName) { //输入设备的音视频名称 HRESULT h ...

  6. Directshow 采集音视频数据H264+AAC+rtmp效果还不错

    从usb摄像头或者采集卡中采集效果还是不错的.

  7. 转:最简单的基于 DirectShow 的视频播放器

    50行代码实现的一个最简单的基于 DirectShow 的视频播放器 本文介绍一个最简单的基于 DirectShow 的视频播放器.该播放器对于初学者来说是十分有用的,它包含了使用 DirectSho ...

  8. 最简单的基于DirectShow的示例:视频播放器

    ===================================================== 最简单的基于DirectShow的示例文章列表: 最简单的基于DirectShow的示例:视 ...

  9. 采集音频和摄像头视频并实时H264编码及AAC编码[转]

    0. 前言 我在前两篇文章中写了DirectShow捕获音视频然后生成avi,再进行264编码的方法.那种方法有一些局限性,不适合实时性质的应用,如:视频会议.视频聊天.视频监控等.本文所使用的技术, ...

随机推荐

  1. css position全解析

    1.position:static 所有的元素的默认定位都是position:static,这意味着元素没有被定位,而且在文档中出现在它应该在的位置. 2.position:relative 如果设定 ...

  2. DDD领域驱动设计之聚合、实体、值对象

    关于具体需求,请看前面的博文:DDD领域驱动设计实践篇之如何提取模型,下面是具体的实体.聚合.值对象的代码,不想多说什么是实体.聚合等概念,相信理论的东西大家已经知晓了.本人对DDD表示好奇,没有在真 ...

  3. iOS——Core Animation 知识摘抄(四)

    原文地址http://www.cocoachina.com/ios/20150106/10840.html 延迟解压 一旦图片文件被加载就必须要进行解码,解码过程是一个相当复杂的任务,需要消耗非常长的 ...

  4. 有强迫症的我只能自己写一个json格式化工具

    缘由 为什么博客园的markdown解析出问题了啊?好奇怪啊! 一直以来在编码规范界有2大争论不休的话题,一个是关于是用空格缩进还是tab缩进的问题,一个是花括号是否换行的问题,笔者是tab缩进和花括 ...

  5. Gradle与Gatling脚本集成

    Gatling作为次时代的性能测试工具,由于其API简洁明了.性能出众,越来越受欢迎.但是运行Gatling脚本却有诸多不便,其提供的默认方式不是很方便.考虑到Gatling脚本本质上是Scala类, ...

  6. java 堆栈分析2

    有了mat.同时我们发现Java有提供jvisualvm, jvisualvm是一个不错的工具: heap dump . thread dump. cpu/mem profile 无所不能. 不过观察 ...

  7. linux奇技淫巧 4

    压缩解压 tar 即可压缩也可以解压 c 压缩 如果没有z.j参数,则表示,只打包,不压缩. 就说, t 查看 z 以gzip方式压缩 相当于 gzip ?.. j 以bzip方式压缩 bzip2 ? ...

  8. Java程序员的日常 —— 注册工厂的妙用

    注册工厂是一种很常用的框架书写方法,它适合于快速创建相同类型的对象. 举个栗子 比如一个家具工厂,有沙发.椅子.茶几等等,正常的编程模式是这样的: //创建 class 沙发{} class 椅子{} ...

  9. JS BOM

    一.window对象 //系统对话框 var flag=confirm("提示语句");//弹出一个对话框 当你点击确定flag=true,点击取消flag=false: var ...

  10. 无服务端纯前台导出数据到Excel-JSExcelXML.js 使用指南

    JSExcelXML 使用指南 先来个效果预览 a.前端显示 b.导出excel效果 表头部分 表尾部分 1.功能描述 JsExcelXml 采用js生成excel中可显示的xml格式文本,将输出文本 ...