C#學習之 調用 AForge.NET Framework 啟動網路攝影機__.net

來源:互聯網
上載者:User

          今天開始搗鼓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);            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.