你好,欢迎来到电脑编程技巧与维护杂志社! [免费注册][请登录 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:杂志经典 / 图形图象处理与游戏编程
基于DirectShowLib的家庭视频监控系统设计与实现(二)
 

3)视频预览方法

public void VideoPreview()

  {  

try

      {

          int hr = 0;                 

          hr = this.m_graphBuilder.AddFilter(theVideoDevice, "Video Capture");

          DsError.ThrowExceptionForHR(hr);

          // 通过theVideoDeviceIBaseFilter)视频接口对象的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);

          }

              // 获取theVideoDeviceIAMVideoControl对象,对于具有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);

              //通过渲染将采集设备的相关输出Pinsample 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函数出现异常,视频预览失败!");

        }

}

4)抓图方法

public Bitmap SnapeBitmap()

        {

            if (this.m_IsPreview)

            {

                if (this.m_ipBuffer != IntPtr.Zero)

                {

                    Marshal.FreeCoTaskMem(this.m_ipBuffer);

                    this.m_ipBuffer = IntPtr.Zero;

                }

                this.m_PictureReady.Reset();

                            //分配空间存储抓取的图片

                this.m_ipBuffer = Marshal.AllocCoTaskMem(Math.Abs(this.videoStride) * this.videoHeight);

                try

                {

                    this.m_WantOne = true;

                    if (this.m_VidControl != null)

{                       

//启动图片抓拍

DsError.ThrowExceptionForHR(this.m_VidControl.SetMode(this.m_pinStill, VideoControlFlags.Trigger));

                    }

                    if (!this.m_PictureReady.WaitOne(0xbb8, false))

                    {

                        throw new Exception("获取图片超时");

                    }

                }

                catch

                {

                    Marshal.FreeCoTaskMem(this.m_ipBuffer);

                    this.m_ipBuffer = IntPtr.Zero;

                    //throw;

                }

                if (this.m_ipBuffer != IntPtr.Zero)

                {

                    Bitmap bitmap = new Bitmap(this.videoWidth, this.videoHeight, this.videoStride, PixelFormat.Format24bppRgb, this.m_ipBuffer);//图片转存到Bitmap对象

                    bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);//图片翻转

                    return bitmap;

                }

            }

            return null;

        }

 

5)视频录像方法

public bool RecordVideo(string mSaveFileName)

        {

            if (this.m_IsRecord)

            {

                throw new Exception("正在录制视频,请停止后重新录制!");

            }

            try

            {

                IBaseFilter ppbf;

                IFileSinkFilter ppSink;

                if (!this.m_IsPreview)

                {

                    this.VideoPreview();

                }

                this.m_mediaControl.Stop();

                this.m_IsRecord = true;

                if (this.m_VidControl == null)

                {

//去除Filter baseGrabFlt

DsError.ThrowExceptionForHR(this.m_graphBuilder.RemoveFilter(this.baseGrabFlt));

                }

//添加音频设备的Filter

DsError.ThrowExceptionForHR(this.m_graphBuilder.AddFilter(this.theAudioDevice, "Audio Input Device"));

//添加视频压缩Filter

DsError.ThrowExceptionForHR(this.m_graphBuilder.AddFilter(this.theVideoCompressor, "Video compressor filter"));

//添加音频压缩Filter

DsError.ThrowExceptionForHR(this.m_graphBuilder.AddFilter(this.theAudioCompressor, "Audio compressor filter"));

//设置视频录像输出文件参数

DsError.ThrowExceptionForHR(this.m_captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, mSaveFileName, out ppbf, out ppSink));

//Render视频设备和压缩Filter

DsError.ThrowExceptionForHR(this.m_captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, this.theVideoDevice, this.theVideoCompressor, ppbf));

//Render音频设备和压缩Filter

DsError.ThrowExceptionForHR(this.m_captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Audio, this.theAudioDevice, this.theAudioCompressor, ppbf));

                this.m_mediaControl.Run();

            }

            catch (Exception exception)

            {

                this.StopRecordVideo();

                this.m_IsRecord = false;

                throw new Exception(exception.Message + "\nRecordVideo函数出现异常,视频录制失败!");

            }

            return true;

        }

  推荐精品文章

·黑客频频攻击区块链安全软肋,如何保证区…
·阿里、商汤在香港设立人工智能实验室 本月…
·云端的竞争:巨头纷纷加码云市场
·中国云体系联盟主办未来科技独角兽与行业…
·Gartner:企业首席信息官们可能没有注意到…
·微软开源Azure云服务AI引擎Infer.NET
·创新指纹读取器出炉:平均准确率最高可达…
·微软投资东南亚出行公司Grab 将进行AI等技…
·Facebook开发AI系统:看美食图片能自动生…
·工信部:促进区块链与实体经济深度融合
·谷歌首度证实了重返中国的Dragonfly计划的…
·Java 11正式发布,告诉你到底应不应该升级…
·Stack Overflow 10 周年了,作为程序员你…
·新手初学Python最常犯的十个错误,其中有…

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备06048724号,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区长春桥路5号6号楼1209室,邮编:100089