基于Aforge摄像头调用简单实例
作者:Snow Yang 时间:2022-07-23 03:44:20
基于C#的Aforge类调用简单示例,供大家参考,具体内容如下
由题,本程序是使用Aforge类库调用摄像头的demo。
功能:
1.预览
2.前后摄像头切换
1.判断是否有摄像头,无则提示
2.有,判断摄像头个数,有1个则直接打开预览
3.有2个或以上,提示选择后打开相应摄像头预览
3.拍照功能
控件:
1.摄像头列表下拉框:用于选择摄像头,如无摄像头则不能下拉
2.拍照按钮:用于拍照,照片默认存储在当前程序运行目录,名称为“Aforge.bmp”。如无摄像头则不能点击
3.预览按钮:用于打开当前选择摄像头预览,如当前选择摄像头不变,则不进行重新预览。如没有摄像头则不能点击
4.提示标签:用于给出软件的一些提示
5.预览控件videoSourcePlayer:用于预览拍照界面以及实现拍照功能
该控件需要右击工具箱-选择项-.NET选项卡中-浏览,找到Aforge.Controls.dll库添加后才会出现
程序设计
1.加载窗体
查找所有摄像头设备
2.预览按钮:
根据选中摄像头打开该摄像头并预览
3.拍照按钮:
记录当前帧保存为图像
代码
程序主要代码:
添加引用 using AForge.Video.DirectShow;
///查找所有摄像头设备
private void loadCameraList()
{
VideoCaptureDevice cameraDevices =
new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (cameraDevices.Count == 0)
{
capture_btn.Enabled = false;
cameraId_cob.Enabled = false;
preview_btn.Enabled = false;
guide_lab.Text = noCameraDevice;
cameraDevices = null;
}else if(cameraDevices.Count == 1)
{
isSingleCamera = true;
preview_btn.Enabled = false;
guide_lab.Visible = false;
}
foreach (FilterInfo cameraDevice in cameraDevices)
{
cameraId_cob.Items.Add(cameraDevice.Name);
cameraId_cob.SelectedIndex = 0;
}
}
///根据选中摄像头打开该摄像头并预览
private VideoCaptureDevice cameraDevice;
private void preview()
{
if (null != cameraDevice)
{//在2个或以上摄像头进行切换时执行
preview_player.SignalToStop();
preview_player.WaitForStop();
}
cameraDevice =
new VideoCaptureDevice(cameraDevices[cameraId_cob.SelectedIndex].MonikerString);
cameraDevice.DesiredFrameSize = new Size(320, 240);
cameraDevice.DesiredFrameRate = 1;
preview_player.VideoSource = cameraDevice;
preview_player.Start();
}
///记录当前帧保存为图像
private void takePhoto()
{
if (cameraDevice == null)
return;
Bitmap bitmap = preview_player.GetCurrentVideoFrame();
string fullPath = Application.StartupPath + "\\";
if (!Directory.Exists(fullPath))
Directory.CreateDirectory(fullPath);
string img = fullPath + "Aforge.jpg";
bitmap.Save(img);
guide_lab.Text = img;
guide_lab.Visible = true;
}
备注
1.关闭窗体时需要关闭控件:
private void aforgeForm_FormClosing(object sender, FormClosingEventArgs e)
{
preview_player.SignalToStop();
preview_player.WaitForStop();
}
2.调用preview_player.GetCurrentVideoFrame()方法提示没有该方法
由于使用的Aforge.Controls.dll库版本过低导致,根据官方记录,该方法需要 (in AForge.Controls.dll) Version: 2.2.5.0 (2.2.5.0)版本支持
3.在添加预览控件videoSourcePlayer时提示没有可以加入的控件
目前笔者遇到的这个问题比较奇葩,使用AForge.Controls.dll Version: 2.2.5.0 库文件添加会报这个错误,看过各种解法,比如直接把库文件拖到工具面板上,还是没能解决。说奇葩是因为目前亲测到的解决方法步骤是:
1).引用那边先不要添加AForge.Controls.dll文件
2).找到AForge.Controls.dll Version: 2.0.0.0 注意版本号是低版本的。将这个低版本的加入工具面板就可以看到预览控件videoSourcePlayer。
3).但是,此时代码里会出现第二个错误(调用preview_player.GetCurrentVideoFrame()方法提示没有该方法)。原因就不解释了,解决方法是:在引用里面的AForge.Controls.dll(这个时候可以看下属性是Version: 2.0.0.0)删除这个旧版本,添加新的版本的AForge.Controls.dll Version: 2.2.5.0。
至此,大功告成。可以正常编译了。
来源:https://blog.csdn.net/weixin_36957825/article/details/53636430
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
轻松学习C#的方法
![](https://img.aspxhome.com/file/2023/3/66573_0s.jpg)
浅谈Java 继承接口同名函数问题
![](https://img.aspxhome.com/file/2023/0/57720_0s.jpg)
浅谈Java编程之if-else的优化技巧总结
java开发之Jdbc分页源码详解
Java中lambda表达式的基本运用
![](https://img.aspxhome.com/file/2023/4/83134_0s.png)
Java数组越界问题实例解析
SpringBoot+Prometheus+Grafana实现应用监控和报警的详细步骤
![](https://img.aspxhome.com/file/2023/6/67486_0s.jpg)
java判断某个点是否在所画多边形/圆形内
Java如何去掉指定字符串的开头的指定字符
详解用Spring Boot Admin来监控我们的微服务
详解C#编程获取资源文件中图片的方法
Java扩展库RxJava的基本结构与适用场景小结
详解path和classpath的区别
springboot项目启动的时候参数无效的解决
mybatis原理概述入门教程
Java properties 和 yml 的区别解析
![](https://img.aspxhome.com/file/2023/4/63014_0s.png)
Java开启JMX远程监控服务配置
![](https://img.aspxhome.com/file/2023/2/72332_0s.png)
使用Spring Data Redis实现数据缓存的方法
![](https://img.aspxhome.com/file/2023/6/71706_0s.png)
Java 实战范例之线上婚纱摄影预定系统的实现
![](https://img.aspxhome.com/file/2023/4/61384_0s.png)