C#使用AForge实现调用摄像头的示例详解
更新时间:2023年11月14日 09:01:24 作者:SongYuLong的博客
AForge是一个专门为开发者和研究者基于C#框架设计的,这个框架提供了不同的类库和关于类库的资源,本文为大家介绍了C#使用AForge实现调用摄像头的相关教程,需要的可以了解下
安装AForge
Visual Studio 2022=>项目=>管理NuGet程序包,搜索AForge并依次安装作者为AForge.NET的多个关联组件。
使用AForge控件
安装AForge组件完成后,工具箱会新增AForge控件,将VideoSourcePlayer拖拽到Form控件区域即可。
1.定义变量
private FilterInfoCollection filterInfoCollection; // 摄像头设备集合 private VideoCaptureDevice videoCaptureDevice; // 捕获设备源 Bitmap bmp; // 处理图片
2.获取PC端所有摄像头集合
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
3.设置视频源并启动
videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[0].MonikerString); videoSourcePlayer1.VideoSource = videoCaptureDevice; videoSourcePlayer1.Start();
4.获取一帧摄像头数据
bmp = videoSourcePlayer1.GetCurrentVideoFrame(); // 拍照
5.关闭视频源
if (videoSourcePlayer1.VideoSource != null) { videoSourcePlayer1.SignalToStop(); videoSourcePlayer1.WaitForStop(); videoSourcePlayer1.VideoSource = null; }
示例代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using AForge; using AForge.Controls; using AForge.Video; using AForge.Video.DirectShow; namespace CameraDemo { public partial class Form1 : Form { private FilterInfoCollection filterInfoCollection; // 摄像头设备集合 private VideoCaptureDevice videoCaptureDevice; // 捕获设备源 Bitmap bmp; // 处理图片 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 检测PC端所有摄像头 filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice); MessageBox.Show("识别到:" + filterInfoCollection.Count.ToString() +"个摄像头"); comboBox1.Items.Add(filterInfoCollection[0].MonikerString); } private void CloseCamera() { if (videoSourcePlayer1.VideoSource != null) { videoSourcePlayer1.SignalToStop(); videoSourcePlayer1.WaitForStop(); videoSourcePlayer1.VideoSource = null; } } private void button2_Click(object sender, EventArgs e) { try { TimeSpan now = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); long t = Convert.ToInt64(now.TotalMilliseconds)/1000; bmp.Save(string.Format(@"D:\{0}.jpg", t.ToString())); MessageBox.Show("保存成功"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { CloseCamera(); if (comboBox1.SelectedIndex == 0 && filterInfoCollection.Count > 0) { videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[0].MonikerString); } else if (comboBox1.SelectedIndex == 1 && filterInfoCollection.Count > 1) { videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[1].MonikerString); } else { MessageBox.Show("摄像头选择有误"); return; } videoSourcePlayer1.VideoSource = videoCaptureDevice; videoSourcePlayer1.Start(); button1.Enabled = true; button2.Enabled = true; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { CloseCamera(); } /// <summary> /// 拍照 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { bmp = videoSourcePlayer1.GetCurrentVideoFrame(); // 拍照 pictureBox1.Image = bmp; } } }
以上就是C#使用AForge实现调用摄像头的示例详解的详细内容,更多关于C# AForge调用摄像头的资料请关注脚本之家其它相关文章!
最新评论