Emgucv(一)Aforge切换摄像头并调用摄像头属性
2024-09-20 17:17:51
一、新建一个Windows窗体应用程序,在Form1窗体上添加一个PictureBox控件、一个ComboBox控件,命名为PictureBox1、cbCapture,还有两个Button控件,Text分别为切换和摄像头属性,Name分别为btnStart和btnConfig,其界面如下:
二、在该项目下的“引用”处右击选择“添加引用”,添加 AForge.Video.dll 和 AForge.Video.DirectShow.dll 两个程序集
三、双击两个Button按钮以及触发窗体的Load和Closing事件,代码如下:
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;
using System.Diagnostics; namespace Aforge调用摄像头
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//枚举所有的视频输入设备
if (videoDevices.Count == )
throw new ApplicationException();
foreach (FilterInfo device in videoDevices)
{
cbCapture.Items.Add(device.Name);//把所有的视频设备添加到下拉框中
}
cbCapture.SelectedIndex = ;
videoSource = new VideoCaptureDevice(videoDevices[cbCapture.SelectedIndex].MonikerString);//摄像头的名称
videoSource.DesiredFrameSize = new Size(, );//设置大小
videoSource.DesiredFrameRate = ;//设置帧率
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
videoSource.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;
private void btnStart_Click(object sender, EventArgs e)
{
videoSource.Stop();
videoSource = new VideoCaptureDevice(videoDevices[cbCapture.SelectedIndex].MonikerString);//摄像头的名称
videoSource.DesiredFrameSize = new Size(, );//设置大小
videoSource.DesiredFrameRate = ;//设置帧率
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
videoSource.Start();
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bitmap;
} private void btnConfig_Click(object sender, EventArgs e)
{
if ((videoSource != null) && (videoSource is VideoCaptureDevice))
{
try
{
((VideoCaptureDevice)videoSource).DisplayPropertyPage(this.Handle);
}
catch (NotSupportedException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Process p = Process.GetCurrentProcess();
if (p.HasExited == false)
{
p.Kill();
}
}
}
}
注意:如果在关闭窗体是不把这个进程给结束掉,那么下次再次调用的时候就会出现程序正在运行中的错误或者在窗体加载时不能调用处摄像头;此外,摄像头必须是无驱的
四、效果图
最新文章
- error LNK2019 无法解析的外部符号 __imp__accept@12
- FireDac 与数据库连接时字符集及对应的字段类型问题
- Java_注解_01_注解(Annotation)详解
- Js DOM 详解
- SpringBoot入门:Spring Data JPA 和 JPA(理论)
- Linux的sleep()和usleep()
- 水晶报表中";已达到系统管理员配置的最大报表处理作业数限制";错误的处理
- 查表法解决calendar中月份及星期初始值为0的情况。
- js字符串String提取方法比较
- 《程序猿闭门造车》之NBPM工作流引擎 - 项目整体架构
- 【22】访问者模式(Visitor Pattern)
- cookie之三天免登录代码
- 解决:springmvc中接收date数据问题
- python-flask-SQLAlchemy
- mysql数据库主从同步读写分离(一)主从同步
- 其他ip无法访问Yii的gii,配置ip就可以
- kill -9 a postgres process
- mysql数据库从删库到跑路之mysql库操作
- Camera &; Render
- django使用celery实现异步操作
热门文章
- 《ASP.NET MVC 5 破境之道》:第一境 ASP.Net MVC5项目初探 — 第三节:View层简单改造
- hdu 4325 Flowers(区间离散化)
- 20164317《网络对抗技术》Exp1 PC平台逆向破解
- openvswitch 驱动卸载失败(Module openvswitch is in use)
- ModelForm错误验证自定义钩子和全局钩子
- hello lua
- Django(wsgiref、jinja2模块使用介绍)
- MySQL远程登陆解决
- my SQL Workbench创建数据库
- 结合 RunLoop 和 Instrument 定位卡顿