好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于Aforge摄像头调用简单实例

基于C#的Aforge类调用简单示例,供大家参考,具体内容如下

由题,本程序是使用Aforge类库调用摄像头的demo。

功能:

1.预览

2.前后摄像头切换

    1.判断是否有摄像头,无则提示
    2.有,判断摄像头个数,有1个则直接打开预览
    3.有2个或以上,提示选择后打开相应摄像头预览
    3.拍照功能

控件:

1.摄像头列表下拉框:用于选择摄像头,如无摄像头则不能下拉
2.拍照按钮:用于拍照,照片默认存储在当前程序运行目录,名称为[Aforge.bmp]。如无摄像头则不能点击
3.预览按钮:用于打开当前选择摄像头预览,如当前选择摄像头不变,则不进行重新预览。如没有摄像头则不能点击
4.提示标签:用于给出软件的一些提示
5.预览控件videoSourcePlayer:用于预览拍照界面以及实现拍照功能
    该控件需要右击工具箱-选择项-.NET选项卡中-浏览,找到Aforge.Controls.dll库添加后才会出现

程序设计

1.加载窗体
    查找所有摄像头设备

2.预览按钮:
    根据选中摄像头打开该摄像头并预览

3.拍照按钮:
    记录当前帧保存为图像

代码

程序主要代码:

添加引用 using AForge.Video.DirectShow;

?

///查找所有摄像头设备

  private void loadCameraList()

     {

     VideoCaptureDevice cameraDevices =

     new FilterInfoCollection(FilterCategory.VideoInputDevice);

       if (cameraDevices.Count == 0)

       {

         capture_btn.Enabled = false ;

         cameraId_cob.Enabled = false ;

         preview_btn.Enabled = false ;

         guide_lab.Text = noCameraDevice;

         cameraDevices = null ;

 

       } else if (cameraDevices.Count == 1)

       {

         isSingleCamera = true ;

         preview_btn.Enabled = false ;

         guide_lab.Visible = false ;

       }

       foreach (FilterInfo cameraDevice in cameraDevices)

       {

         cameraId_cob.Items.Add(cameraDevice.Name);

         cameraId_cob.SelectedIndex = 0;

       }

     }

  ///根据选中摄像头打开该摄像头并预览

  private VideoCaptureDevice cameraDevice;

  private void preview()

     {

       if ( null != cameraDevice)

       { //在2个或以上摄像头进行切换时执行

         preview_player.SignalToStop();

         preview_player.WaitForStop();

       }

       cameraDevice =

       new VideoCaptureDevice(cameraDevices[cameraId_cob.SelectedIndex].MonikerString);

       cameraDevice.DesiredFrameSize = new Size(320, 240);

       cameraDevice.DesiredFrameRate = 1;

       preview_player.VideoSource = cameraDevice;

       preview_player.Start();

     }

///记录当前帧保存为图像

private void takePhoto()

     {

       if (cameraDevice == null )

         return ;

       Bitmap bitmap = preview_player.GetCurrentVideoFrame();

       string fullPath = Application.StartupPath + "\\" ;

       if (!Directory.Exists(fullPath))

         Directory.CreateDirectory(fullPath);

       string img = fullPath + "Aforge.jpg" ;

       bitmap.Save(img);

       guide_lab.Text = img;

       guide_lab.Visible = true ;

     }

备注

1.关闭窗体时需要关闭控件:

?

private void aforgeForm_FormClosing( object sender, FormClosingEventArgs e)

   {

     preview_player.SignalToStop();

     preview_player.WaitForStop();

   }

2.调用preview_player.GetCurrentVideoFrame()方法提示没有该方法

由于使用的Aforge.Controls.dll库版本过低导致,根据官方记录,该方法需要 (in AForge.Controls.dll) Version: 2.2.5.0 (2.2.5.0)版本支持

3.在添加预览控件videoSourcePlayer时提示没有可以加入的控件

目前笔者遇到的这个问题比较奇葩,使用AForge.Controls.dll  Version: 2.2.5.0 库文件添加会报这个错误,看过各种解法,比如直接把库文件拖到工具面板上,还是没能解决。说奇葩是因为目前亲测到的解决方法步骤是:

1).引用那边先不要添加AForge.Controls.dll文件
2).找到AForge.Controls.dll Version: 2.0.0.0 注意版本号是低版本的。将这个低版本的加入工具面板就可以看到预览控件videoSourcePlayer。
3).但是,此时代码里会出现第二个错误(调用preview_player.GetCurrentVideoFrame()方法提示没有该方法)。原因就不解释了,解决方法是:在引用里面的AForge.Controls.dll(这个时候可以看下属性是Version: 2.0.0.0)删除这个旧版本,添加新的版本的AForge.Controls.dll  Version: 2.2.5.0。
至此,大功告成。可以正常编译了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_36957825/article/details/53636430

dy("nrwz");

查看更多关于基于Aforge摄像头调用简单实例的详细内容...

  阅读:53次