今天開始搗鼓C#啟動網路攝影機,之前做過用 C++調用OPENCV庫啟動網路攝影機,C#理論上也可以。 但是看了 這篇博文筆記, 頓時腦洞大開,原來可以和網路攝影機打交道的庫有這麼多啊。之前,我就直到OPENCV可以辦到。慚愧,自己的知識面太窄了 。
當然,今天的重頭戲是 AForge.NET Framework 。
先來看個我鼓搗出來的一個簡單一實例吧:
由於是晚上嘛,光線不太好哦。導致看起來畫質不夠好。
功能其實很簡單,開啟網路攝影機,捕獲每一幀映像 --------- AForge.NET Framework 介紹 1,簡介
AForge.NET是一個專門為開發人員和研究者基於C#架構設計的,他包括電腦視覺與人工智慧,影像處理,神經網路,遺傳演算法,機器學習,模糊系統,機器人控制等領域。 2,主要架構
這個架構由一系列的類庫組成。主要包括有:
AForge.Imaging —— 一些日常的影像處理和過濾器
AForge.Vision —— 電腦視覺應用類庫
AForge.Neuro —— 神經網路計算庫AForge.Genetic -進化演算法編程庫
AForge.MachineLearning —— 機器學習類庫
AForge.Robotics —— 提供一些機器人的工具類庫
AForge.Video —— 一系列的視頻處理類庫
AForge.Fuzzy —— 模糊推理系統類別庫
AForge.Controls—— 映像,三維,圖表顯示控制項 ---------------下載
現在,版本已更新到2.2.5了。
下載串連:http://www.aforgenet.com/framework/downloads.html
----------------在VS中添加引用
1,建立一個C#視窗引用程式。在工具箱中拖一個按鈕控制項 和 一個pictureBox控制項。空間位置和大小自定。
2,添加引用。
添加的DLL的名稱有:
AForge.Video.DirectShow.dllAForge.Video.dll
------------添加名稱空間
using AForge.Video.DirectShow;using AForge.Video;
------------添加類屬性
///---聲明變數 public FilterInfoCollection USE_Webcams = null; public VideoCaptureDevice cam = null;
--------------按鈕被單擊事件代碼
//---按鈕被單擊事件 private void startBtn_Click(object sender, EventArgs e) { try { if (startBtn.Text == "開始") { ///-- startBtn.Text = "停止"; ///---啟動網路攝影機 cam.Start(); } else { ///--設定按鈕提示字樣 startBtn.Text = "開始"; ///--停止網路攝影機捕獲映像 cam.Stop(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
--------視窗的Load函數源碼
//--視窗載入事件 private void Form1_Load(object sender, EventArgs e) { try { ///---執行個體化對象 USE_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice); ///---網路攝影機數量大於0 if (USE_Webcams.Count > 0) { ///---禁用按鈕 startBtn.Enabled = true; ///---執行個體化對象 cam = new VideoCaptureDevice(USE_Webcams[0].MonikerString); ///---綁定事件 cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame); } else { ///--沒有網路攝影機 startBtn.Enabled = false; ///---提示沒有網路攝影機 MessageBox.Show("沒有網路攝影機外設"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 這裡自訂了一個函數,源碼如下:
///------自訂函數 private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs) { pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone(); ///---throw new NotImplementedException(); }---------視窗的FormClosed事件來源碼
///---視窗關閉事件 private void Form1_FormClosed(object sender, FormClosedEventArgs e) { try { if (cam != null) { ///---關閉網路攝影機 if (cam.IsRunning) { cam.Stop(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
-----編譯運行試試
------完整源碼如下
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;///---添加名稱空間using AForge.Video.DirectShow;using AForge.Video;namespace OS_webcam{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } ///---聲明變數 public FilterInfoCollection USE_Webcams = null; public VideoCaptureDevice cam = null; //---按鈕被單擊事件 private void startBtn_Click(object sender, EventArgs e) { try { if (startBtn.Text == "開始") { ///-- startBtn.Text = "停止"; ///---啟動網路攝影機 cam.Start(); } else { ///--設定按鈕提示字樣 startBtn.Text = "開始"; ///--停止網路攝影機捕獲映像 cam.Stop(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } //--視窗載入事件 private void Form1_Load(object sender, EventArgs e) { try { ///---執行個體化對象 USE_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice); ///---網路攝影機數量大於0 if (USE_Webcams.Count > 0) { ///---禁用按鈕 startBtn.Enabled = true; ///---執行個體化對象 cam = new VideoCaptureDevice(USE_Webcams[0].MonikerString); ///---綁定事件 cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame); } else { ///--沒有網路攝影機 startBtn.Enabled = false; ///---提示沒有網路攝影機 MessageBox.Show("沒有網路攝影機外設"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } ///------自訂函數 private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs) { pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone(); ///---throw new NotImplementedException(); } ///---視窗關閉事件 private void Form1_FormClosed(object sender, FormClosedEventArgs e) { try { if (cam != null) { ///---關閉網路攝影機 if (cam.IsRunning) { cam.Stop(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }}