C# 使用AForge调用摄像头
- 安装AForge
- 使用AForge控件
- 示例代码
AForge官网
安装AForge
Visual Studio 2022=>项目=>管理NuGet程序包,搜索AForge并依次安装作者为AForge.NET的多个关联组件。
使用AForge控件
安装AForge组件完成后,工具箱会新增AForge控件,将VideoSourcePlayer拖拽到Form控件区域即可。
- 定义变量
private FilterInfoCollection filterInfoCollection; // 摄像头设备集合
private VideoCaptureDevice videoCaptureDevice; // 捕获设备源
Bitmap bmp; // 处理图片 - 获取PC端所有摄像头集合
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice); - 设置视频源并启动
videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[0].MonikerString);
videoSourcePlayer1.VideoSource = videoCaptureDevice;
videoSourcePlayer1.Start(); - 获取一帧摄像头数据
bmp = videoSourcePlayer1.GetCurrentVideoFrame(); // 拍照 - 关闭视频源
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;
}
}
}