• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C# 利用AForge实现摄像头信息采集

c# 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

概述

AForge.NET是一个专门为开发者和研究者基于C#框架设计的,提供了不同的类库和关于类库的资源,还有很多应用程序例子,包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,机器人等领域。本文主要讲解利用AForge进行图像采集的相关内容【包括拍照,视频录制】,仅供学习分享使用。

AForge.Net相关类库介绍

  • AForge.dll 是框架的核心基础类库,为其他类库提供服务。
  • AForge.Controls.dll 包含AForge.Net的UI控件,主要用于页面显示。
  • AForge.Imaging.dll 主要是框架中用于图像处理的类库,主要负责图像的处理
  • AForge.Video.dll 主要是框架中对视频处理的类库。
  • AForge.Video.DirectShow.dll 主要是通过DirectShow接口访问视频资源的类库。
  • AForge.Video.FFMPEG.dll 是一个还未正式发布的类库,通过FFMPEG类库对视频进行读写。

通过NuGet管理器引入AForge类库

项目名称右键–>管理NuGet程序包,打卡NuGet包管理器  如下所示:

示例效果图

本示例主要包括打开,关闭摄像头,拍照,连续拍照,开始录制视频,暂停录制视频,停止录视频,退出等功能。

如下所示:左侧为摄像头投影区域,右侧为图像控件,显示拍照所得的图片

核心代码

获取视频设备列表以及设备对应的分辨率

/// <summary>
  /// 页面加载摄像头设备
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void FrmMain_Load(object sender, EventArgs e)
  {
   try
   {
    this.lblTime.Text = "";
    // 枚举所有视频输入设备
    videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    if (videoDevices.Count == 0)
    {
     lblStatus.Text = "No local capture devices";
    }
    foreach (FilterInfo device in videoDevices)
    {
     int i = 1;
     cmbDevices.Items.Add(device.Name);
     lblStatus.Text = ("摄像头" + i + "初始化完毕..." + "\n");
     i++;
    }
    cmbDevices.SelectedIndex = 0;
   }
   catch (ApplicationException)
   {
    this.lblStatus.Text = "No local capture devices";
    videoDevices = null;
   }
  }

  private void cmbDevices_SelectedIndexChanged(object sender, EventArgs e)
  {
   this.cmbResolution.Items.Clear();
   videoSource = new VideoCaptureDevice(videoDevices[cmbDevices.SelectedIndex].MonikerString);
   foreach(var cap in videoSource.VideoCapabilities) {
    this.cmbResolution.Items.Add(string.Format("({0},{1})",cap.FrameSize.Width,cap.FrameSize.Height));
   }
   if (this.cmbResolution.Items.Count > 0)
   {
    this.cmbResolution.SelectedIndex = 0;
   }
  }

打开视频设备和关闭视频设备

/// <summary>
  /// 设备打开
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void btnOpen_Click(object sender, EventArgs e)
  {
   int index = this.cmbResolution.SelectedIndex;
   videoSource = new VideoCaptureDevice(videoDevices[cmbDevices.SelectedIndex].MonikerString);
   videoSource.VideoResolution = videoSource.VideoCapabilities[<a>本文来源gao($daima.com搞@代@#码8网^</a>index];
   this.vsPlayer.VideoSource = videoSource;
   //设置对应的事件
   videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
   this.vsPlayer.Start();
  }

  /// <summary>
  /// 产生新帧的触发事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="eventArgs"></param>
  public void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
  {
   lock (objLock)
   {
    Bitmap bmp = null;
    if (isMultiPhoto)
    {
     bmp = (System.Drawing.Bitmap)eventArgs.Frame.Clone();
     string imgFolder = Common.GetImagePath();
     string picName = string.Format("{0}\\{1}.jpg", imgFolder, DateTime.Now.ToString("yyyyMMddHHmmss"));
     Common.SaveImage(picName, bmp);
    }
    //Write Videos
    if (isRecordVideo)
    {
     bmp = (System.Drawing.Bitmap)eventArgs.Frame.Clone();
     videoWriter.WriteVideoFrame(bmp);
    }
   }
  }

  /// <summary>
  /// 设备关闭
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void btnClose_Click(object sender, EventArgs e)
  {
   this.vsPlayer.SignalToStop();
   this.vsPlayer.WaitForStop();
  }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C# 利用AForge实现摄像头信息采集

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址