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. openwrt-智能路由器hack技术(1)---"DNS劫持"

    openwrt-智能路由器hack技术(1)---"DNS劫持" 1   导读 PS:之前写的一个文章,现在发现结构内容排版不是太好,导致阅读体验太差,影响传播和SEO,所以现在整 ...

  2. C#抓取网页HTML内容

    网上很多内容采集工具,今天就自己试着写一个,发现C#可以轻松的抓去网页的内容,进而通过正则来分离出自己感兴趣的数据.下面是抓去网页内容的代码: using System; using System.C ...

  3. COM思想的背后

    最近看公司的一些新产品和框架 , 发现清一色的“COM思想架构 ”, 这里说的“COM思想架构”是指不完全是标准COM组件的方式,而是指在设计上用到了COM思想. COM组件技术大概在1993年产生, ...

  4. jQuery的attr与prop

    jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...

  5. java内存管理总结

    编译好的java程序需要运行在jvm中. 程序,无论是代码还是数据,都需要存储在内存中.JVM为java提供并管理所需要的内存空间. JVM内存分为堆.栈.方法区. 对象存储在堆中. This liv ...

  6. MVVM架构~knockoutjs实现简单的购物车

    返回目录 概念相关 购物车相信大家都用过,很方便,可以将多个商品添加到购物车,并且可以修改购买商品的数据,当然为了用户体验好,在修改数据时,你的价格也会出现变化的,这使用JS可以实现,但我认为,代码量 ...

  7. 基于Oracle的SQL优化(社区万众期待 数据库优化扛鼎巨著)

    基于Oracle的SQL优化(社区万众期待数据库优化扛鼎巨著) 崔华 编   ISBN 978-7-121-21758-6 2014年1月出版 定价:128.00元 856页 16开 编辑推荐 本土O ...

  8. MVC3中如何输出富文本

    MVC3中如何输出富文本 在网站的文本输出中,经常会将DB里的文本输出到页面上. 一般来说是直接利用MVC3中的ViewBag将文本带到前台并表示, 或是是直接以<%:model.data%&g ...

  9. 在configure distribution时遇到错误,不能打开sql agent

    今天在配置Distribution时,遇到一个错误,不能打开sql agent,详细错误信息如下: SQL Server blocked access to procedure 'dbo.sp_set ...

  10. Visual Studio Emulator for Android 里面的安卓模拟器如何启用

    打开软件