Emgucv(一)Aforge切换摄像头并调用摄像头属性

一、新建一个Windows窗体应用程序,在Form1窗体上添加一个PictureBox控件、一个ComboBox控件,命名为PictureBox1、cbCapture,还有两个Button控件,Text分别为切换和摄像头属性,Name分别为btnStart和btnConfig,其界面如下:

二、在该项目下的“引用”处右击选择“添加引用”,添加 AForge.Video.dll 和 AForge.Video.DirectShow.dll 两个程序集

三、双击两个Button按钮以及触发窗体的Load和Closing事件,代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using AForge.Video.DirectShow;
10 using AForge.Video;
11 using System.Diagnostics;
12 
13 namespace Aforge调用摄像头
14 {
15     public partial class Form1 : Form
16     {
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void Form1_Load(object sender, EventArgs e)
23         {
24             try
25             {
26                 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//枚举所有的视频输入设备
27                 if (videoDevices.Count == 0)
28                     throw new ApplicationException();
29                 foreach (FilterInfo device in videoDevices)
30                 {
31                     cbCapture.Items.Add(device.Name);//把所有的视频设备添加到下拉框中
32                 }
33                 cbCapture.SelectedIndex = 0;
34                 videoSource = new VideoCaptureDevice(videoDevices[cbCapture.SelectedIndex].MonikerString);//摄像头的名称
35                 videoSource.DesiredFrameSize = new Size(500, 300);//设置大小
36                 videoSource.DesiredFrameRate = 1;//设置帧率
37                 videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
38                 videoSource.Start();
39             }
40             catch (Exception ex)
41             {
42                 MessageBox.Show(ex.Message);
43             }
44         }
45         private FilterInfoCollection videoDevices;
46         private VideoCaptureDevice videoSource;
47         private void btnStart_Click(object sender, EventArgs e)
48         {
49             videoSource.Stop();
50             videoSource = new VideoCaptureDevice(videoDevices[cbCapture.SelectedIndex].MonikerString);//摄像头的名称
51             videoSource.DesiredFrameSize = new Size(500, 300);//设置大小
52             videoSource.DesiredFrameRate = 1;//设置帧率
53             videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);  
54             videoSource.Start();
55         }
56         private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
57         {
58             Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
59             pictureBox1.Image = bitmap;
60         }
61 
62         private void btnConfig_Click(object sender, EventArgs e)
63         {
64             if ((videoSource != null) && (videoSource is VideoCaptureDevice))
65             {
66                 try
67                 {
68                     ((VideoCaptureDevice)videoSource).DisplayPropertyPage(this.Handle);
69                 }
70                 catch (NotSupportedException ex)
71                 {
72                     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
73                 }
74             }
75         }
76 
77         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
78         {
79             Process p = Process.GetCurrentProcess();
80             if (p.HasExited == false)
81             {
82                 p.Kill();
83             }
84         }         
85     }
86 }
View Code

注意:如果在关闭窗体是不把这个进程给结束掉,那么下次再次调用的时候就会出现程序正在运行中的错误或者在窗体加载时不能调用处摄像头;此外,摄像头必须是无驱的

四、效果图

posted @ 2013-09-13 16:33  魏政  阅读(2223)  评论(0编辑  收藏  举报