当前位置: 代码迷 >> 综合 >> Aforge.net framework采集攝像頭視頻
  详细解决方案

Aforge.net framework采集攝像頭視頻

热度:13   发布时间:2023-12-16 02:40:48.0

http://blog.csdn.net/halen0820/article/details/6897147

對於視頻編程,網絡上的東西不是很好找,攝像頭算是比較初級的東東了,我查了很多資料,才算是有一點結果,現在把攝像頭采集程序代碼與大家分享一下。(vs2008編譯通過)

其中,Aforge.net下載地址http://www.aforgenet.com/framework/

下載lib之後,引用就可以了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;


namespace 視頻
{
    publicpartial class Form1 : Form
    {
       private bool DeviceExist = false;
       private FilterInfoCollection videoDevices;
       private VideoCaptureDevice videoSource = null;
       private Timer timer1 = new Timer();
       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           getCamList();
       }

       private voidgetCamList()       
                 
           try           
                         
               videoDevices = newFilterInfoCollection(FilterCategory.VideoInputDevice);               
               comboBox1.Items.Clear();              
               if (videoDevices.Count ==0)                  
                   throw newApplicationException();              
               DeviceExist =true;              
               foreach (FilterInfo device invideoDevices)              
                                
                   comboBox1.Items.Add(device.Name);            
                            
               comboBox1.SelectedIndex = 0; //make dafault to firstcam          
                    
           catch(ApplicationException)           
                         
               DeviceExist =false;             
               comboBox1.Items.Add("No capture device on yoursystem");       
               
       }

       private void CloseVideoSource()
       {
           if (!(videoSource ==null))           
               if (videoSource.IsRunning)
               {
                   videoSource.SignalToStop();
                   videoSource = null;
               }
       }

       private void video_NewFrame(object sender, NewFrameEventArgseventArgs)       
                
           Bitmap img =(Bitmap)eventArgs.Frame.Clone();           //do processinghere           
           pictureBox1.Image =img;   
           //pictureBox1.Image.
       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           label2.Text = "Device running... " +videoSource.FramesReceived.ToString() + " FPS";
       }

       private void Form1_FormClosed(object sender, FormClosedEventArgse)
       {
           CloseVideoSource();
       }

       private void btnPlay_Click(object sender, EventArgs e)
       {
           if (DeviceExist)
           {
               videoSource = newVideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
               videoSource.NewFrame += newNewFrameEventHandler(video_NewFrame);
               CloseVideoSource();
               videoSource.DesiredFrameSize = new Size(pictureBox1.Width,pictureBox1.Height);
               //videoSource.DesiredFrameSize = new Size(353, 290);
               //videoSource.DesiredFrameRate =10;                 
               videoSource.Start();
               label2.Text = "Device running...";
               timer1.Enabled = true;
           }
           else
           {
               label2.Text = "Error: No Device selected.";
           }
       }

       private void btnStop_Click(object sender, EventArgs e)
       {
           if (videoSource.IsRunning)
           {
               timer1.Enabled = false;
               CloseVideoSource();
               label2.Text = "Device stopped.";
           }
       }

       private void btnExit_Click(object sender, EventArgs e)
       {
           CloseVideoSource();
           this.Dispose();
       }

    }
}


  相关解决方案