AForge.NET是一個專門為開發者和研究者基於C#框架設計的,提供了不同的類庫和關於類庫的資源,還有很多應用程式例子,包括電腦視覺與人工智慧,圖像處理,神經網路,遺傳演算法,機器學習,機器人等領域。本文主要講解利用AForge進行圖像採集的相關內容【包括拍照,視頻錄製】,僅供學習分享使用。 ...
概述
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包管理器 如下所示:
示例效果圖
本示例主要包括打開,關閉攝像頭,拍照,連續拍照,開始錄製視頻,暫停錄製視頻,停止錄視頻,退出等功能。
如下所示:左側為攝像頭投影區域,右側為圖像控制項,顯示拍照所得的圖片
核心代碼
獲取視頻設備列表以及設備對應的解析度
1 /// <summary> 2 /// 頁面載入攝像頭設備 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void FrmMain_Load(object sender, EventArgs e) 7 { 8 try 9 { 10 this.lblTime.Text = ""; 11 // 枚舉所有視頻輸入設備 12 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 13 if (videoDevices.Count == 0) 14 { 15 lblStatus.Text = "No local capture devices"; 16 } 17 foreach (FilterInfo device in videoDevices) 18 { 19 int i = 1; 20 cmbDevices.Items.Add(device.Name); 21 lblStatus.Text = ("攝像頭" + i + "初始化完畢..." + "\n"); 22 i++; 23 } 24 cmbDevices.SelectedIndex = 0; 25 } 26 catch (ApplicationException) 27 { 28 this.lblStatus.Text = "No local capture devices"; 29 videoDevices = null; 30 } 31 } 32 33 private void cmbDevices_SelectedIndexChanged(object sender, EventArgs e) 34 { 35 this.cmbResolution.Items.Clear(); 36 videoSource = new VideoCaptureDevice(videoDevices[cmbDevices.SelectedIndex].MonikerString); 37 foreach(var cap in videoSource.VideoCapabilities) { 38 this.cmbResolution.Items.Add(string.Format("({0},{1})",cap.FrameSize.Width,cap.FrameSize.Height)); 39 } 40 if (this.cmbResolution.Items.Count > 0) 41 { 42 this.cmbResolution.SelectedIndex = 0; 43 } 44 }View Code
打開視頻設備和關閉視頻設備
1 /// <summary> 2 /// 設備打開 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnOpen_Click(object sender, EventArgs e) 7 { 8 int index = this.cmbResolution.SelectedIndex; 9 videoSource = new VideoCaptureDevice(videoDevices[cmbDevices.SelectedIndex].MonikerString); 10 videoSource.VideoResolution = videoSource.VideoCapabilities[index]; 11 this.vsPlayer.VideoSource = videoSource; 12 //設置對應的事件 13 videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame); 14 this.vsPlayer.Start(); 15 } 16 17 /// <summary> 18 /// 產生新幀的觸發事件 19 /// </summary> 20 /// <param name="sender"></param> 21 /// <param name="eventArgs"></param> 22 public void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs) 23 { 24 lock (objLock) 25 { 26 Bitmap bmp = null; 27 if (isMultiPhoto) 28 { 29 bmp = (System.Drawing.Bitmap)eventArgs.Frame.Clone(); 30 string imgFolder = Common.GetImagePath(); 31 string picName = string.Format("{0}\\{1}.jpg", imgFolder, DateTime.Now.ToString("yyyyMMddHHmmss")); 32 Common.SaveImage(picName, bmp); 33 } 34 //Write Videos 35 if (isRecordVideo) 36 { 37 bmp = (System.Drawing.Bitmap)eventArgs.Frame.Clone(); 38 videoWriter.WriteVideoFrame(bmp); 39 } 40 } 41 } 42 43 /// <summary> 44 /// 設備關閉 45 /// </summary> 46 /// <param name="sender"></param> 47 /// <param name="e"></param> 48 private void btnClose_Click(object sender, EventArgs e) 49 { 50 this.vsPlayer.SignalToStop(); 51 this.vsPlayer.WaitForStop(); 52 }View Code
拍照
1 /// <summary> 2 /// 拍照 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnCapture_Click(object sender, EventArgs e) 7 { 8 try 9 { 10 if (this.vsPlayer.IsRunning) 11 { 12 Bitmap bitMap = this.vsPlayer.GetCurrentVideoFrame(); 13 this.pbImage.Image = bitMap; 14 //設置圖片相對控制項的大小 15 this.pbImage.SizeMode = PictureBoxSizeMode.StretchImage; 16 } 17 } 18 catch (Exception ex) 19 { 20 MessageBox.Show("攝像頭異常:" + ex.Message); 21 } 22 }View Code
連拍功能
連拍主要是同時視頻控制項的一個幀觸發事件,在事件中對圖像進行保存,達到連拍的效果,如下所示:
1 //設置對應的事件 2 videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
視頻錄製
視頻錄製,是採用VideoFileWriter對獲取到的每一幀進行寫入到視頻文件中,如下所示:
1 /// <summary> 2 /// 開始錄視頻 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnStartVideo_Click(object sender, EventArgs e) 7 { 8 try 9 { 10 //創建一個視頻文件 11 string vdoPath = Common.GetVideoPath(); 12 string vdoName = string.Format("{0}\\{1}.avi", vdoPath, DateTime.Now.ToString("yyyyMMdd HH-mm-ss")); 13 14 this.timer1.Enabled = true;//是否執行System.Timers.Timer.Elapsed事件; 15 this.lblStatus.Text="錄製中...\n"; 16 tickNum = 0; 17 videoWriter = new VideoFileWriter(); 18 if (this.vsPlayer.IsRunning) 19 { 20 videoWriter.Open(vdoName, vdoWidth, vdoHeight, frameRate, VideoCodec.MPEG4); 21 isRecordVideo = true; 22 } 23 else 24 { 25 MessageBox.Show("沒有視頻源輸入,無法錄製視頻。", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 26 } 27 } 28 catch (Exception ex) 29 { 30 MessageBox.Show("攝像頭異常:" + ex.Message); 31 } 32 } 33 34 /// <summary> 35 /// 停止錄視頻 36 /// </summary> 37 /// <param name="sender"></param> 38 /// <param name="e"></param> 39 private void btnStopVideo_Click(object sender, EventArgs e) 40 { 41 this.isRecordVideo = false; 42 this.videoWriter.Close(); 43 this.timer1.Enabled = false; 44 tickNum = 0; 45 this.lblStatus.Text="錄製停止!\n"; 46 } 47 48 /// <summary> 49 /// 定時器 50 /// </summary> 51 /// <param name="sender"></param> 52 /// <param name="e"></param> 53 private void timer1_Tick(object sender, EventArgs e) 54 { 55 tickNum++; 56 int temp = tickNum; 57 string tick = Common.GetTimeSpan(temp); 58 this.lblTime.Text = tick; 59 } 60 61 /// <summary> 62 /// 暫停錄製 63 /// </summary> 64 /// <param name="sender"></param> 65 /// <param name="e"></param> 66 private void btnPauseVideo_Click(object sender, EventArgs e) 67 { 68 if (this.btnPauseVideo.Text.Trim() == "暫停錄像") 69 { 70 isRecordVideo = false; 71 this.btnPauseVideo.Text = "恢復錄像"; 72 this.timer1.Enabled = false; //暫停計時 73 return; 74 } 75 if (this.btnPauseVideo.Text.Trim() == "恢復錄像") 76 { 77 isRecordVideo = true; 78 this.btnPauseVideo.Text = "暫停錄像"; 79 this.timer1.Enabled = true; //恢復計時 80 } 81 }View Code
註意事項
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配置如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <startup useLegacyV2RuntimeActivationPolicy="true"> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup> 5 <supportedRuntime version="v2.0.50727"/> 6 </configuration>
3. 由於FFMPEG只支持x86模式,不支持混合模式,所以需要在配置管理器進行配置x86平臺,如下所示:
4. 由於視頻幀頻率過快,所以需要進行加鎖控制,否則會造成【讀寫受保護的記憶體】錯誤。
經過以上4步,才可以進行視頻錄製。如果是進行拍照,則不需要。
備註
心有猛虎,細嗅薔薇。