C# 利用AForge實現(xiàn)攝像頭信息采集
概述
AForge.NET是一個專門為開發(fā)者和研究者基于C#框架設計的,提供了不同的類庫和關于類庫的資源,還有很多應用程序例子,包括計算機視覺與人工智能,圖像處理,神經(jīng)網(wǎng)絡,遺傳算法,機器學習,機器人等領域。本文主要講解利用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 是一個還未正式發(fā)布的類庫,通過FFMPEG類庫對視頻進行讀寫。
通過NuGet管理器引入AForge類庫
項目名稱右鍵-->管理NuGet程序包,打卡NuGet包管理器 如下所示:

示例效果圖
本示例主要包括打開,關閉攝像頭,拍照,連續(xù)拍照,開始錄制視頻,暫停錄制視頻,停止錄視頻,退出等功能。
如下所示:左側為攝像頭投影區(qū)域,右側為圖像控件,顯示拍照所得的圖片

核心代碼
獲取視頻設備列表以及設備對應的分辨率
/// <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[index];
this.vsPlayer.VideoSource = videoSource;
//設置對應的事件
videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
this.vsPlayer.Start();
}
/// <summary>
/// 產(chǎn)生新幀的觸發(fā)事件
/// </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();
}
拍照
/// <summary>
/// 拍照
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCapture_Click(object sender, EventArgs e)
{
try
{
if (this.vsPlayer.IsRunning)
{
Bitmap bitMap = this.vsPlayer.GetCurrentVideoFrame();
this.pbImage.Image = bitMap;
//設置圖片相對控件的大小
this.pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
catch (Exception ex)
{
MessageBox.Show("攝像頭異常:" + ex.Message);
}
}
連拍功能
連拍主要是同時視頻控件的一個幀觸發(fā)事件,在事件中對圖像進行保存,達到連拍的效果,如下所示:
//設置對應的事件 videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
視頻錄制
視頻錄制,是采用VideoFileWriter對獲取到的每一幀進行寫入到視頻文件中,如下所示:
/// <summary>
/// 開始錄視頻
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStartVideo_Click(object sender, EventArgs e)
{
try
{
//創(chuàng)建一個視頻文件
string vdoPath = Common.GetVideoPath();
string vdoName = string.Format("{0}\\{1}.avi", vdoPath, DateTime.Now.ToString("yyyyMMdd HH-mm-ss"));
this.timer1.Enabled = true;//是否執(zhí)行System.Timers.Timer.Elapsed事件;
this.lblStatus.Text="錄制中...\n";
tickNum = 0;
videoWriter = new VideoFileWriter();
if (this.vsPlayer.IsRunning)
{
videoWriter.Open(vdoName, vdoWidth, vdoHeight, frameRate, VideoCodec.MPEG4);
isRecordVideo = true;
}
else
{
MessageBox.Show("沒有視頻源輸入,無法錄制視頻。", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show("攝像頭異常:" + ex.Message);
}
}
/// <summary>
/// 停止錄視頻
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStopVideo_Click(object sender, EventArgs e)
{
this.isRecordVideo = false;
this.videoWriter.Close();
this.timer1.Enabled = false;
tickNum = 0;
this.lblStatus.Text="錄制停止!\n";
}
/// <summary>
/// 定時器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
tickNum++;
int temp = tickNum;
string tick = Common.GetTimeSpan(temp);
this.lblTime.Text = tick;
}
/// <summary>
/// 暫停錄制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPauseVideo_Click(object sender, EventArgs e)
{
if (this.btnPauseVideo.Text.Trim() == "暫停錄像")
{
isRecordVideo = false;
this.btnPauseVideo.Text = "恢復錄像";
this.timer1.Enabled = false; //暫停計時
return;
}
if (this.btnPauseVideo.Text.Trim() == "恢復錄像")
{
isRecordVideo = true;
this.btnPauseVideo.Text = "暫停錄像";
this.timer1.Enabled = true; //恢復計時
}
}
注意事項
1. 由于視頻錄制是采用FFMPEG類庫進行處理,所以除了需要AForge.Video.FFMPEG.dll以外,還需要FFMPEG類庫(C++),位于【AForge.NET Framework-2.2.5\Externals\ffmpeg\bin】目錄下,copy到應用程序目下即可,如下圖所示:

2. 由于AForge.Video.FFMPEG.dll類庫只支持.NetFrameWork2.0,所以需要采用混合模式,App.config配置如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup> <supportedRuntime version="v2.0.50727"/> </configuration>
3. 由于FFMPEG只支持x86模式,不支持混合模式,所以需要在配置管理器進行配置x86平臺,如下所示:

4. 由于視頻幀頻率過快,所以需要進行加鎖控制,否則會造成【讀寫受保護的內存】錯誤。
經(jīng)過以上4步,才可以進行視頻錄制。如果是進行拍照,則不需要。
以上就是C# 利用AForge實現(xiàn)攝像頭信息采集的詳細內容,更多關于c# 攝像頭信息采集的資料請關注腳本之家其它相關文章!
相關文章
C#入門之checked和unchecked的區(qū)別實例解析
這篇文章主要介紹了C#中checked和unchecked的區(qū)別,是學習C#必須要牢固掌握的,需要的朋友可以參考下2014-08-08
詳解如何通過wireshark實現(xiàn)捕獲C#上傳的圖片
這篇文章主要為大家詳細介紹了如何通過wireshark實現(xiàn)捕獲C#上傳的圖片,文中的示例代碼簡潔易懂,具有一定的學習價值,感興趣的小伙伴可以了解下2023-11-11
c#基于Win32Api實現(xiàn)返回Windows桌面功能
本文分享下回到桌面功能的實現(xiàn)方法,效果與快捷鍵(Win+D)相同。有此需求的朋友可以參考下2021-05-05

