这篇文章主要介绍了C#调用usb摄像头的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
1、下载AForge类库,下载地址:https://code.google.com/archive/p/aforge/downloads,我下载的版本是:AForge.NET Framework-2.2.5.exe;
2、下载安装好后,将下载类库中的Release文件夹复制到C#项目的可执行文件文件夹,即Debug文件夹下;
3、在C#项目中添加引用,右击解决方案资源管理器下的引用上,点击添加引用,通过浏览找到Debug文件夹下的Release文件夹选择要添加的引用文件:AForge、AForge.Controls、AForge.Imaging、AForge.Video、AForge.Video.DirectShow;
4、在工具箱中添加AForge.Controls控件:先在工具箱中(单击右键)添加新的选项卡,命名为AForge;然后把Release文件夹下的AForge.Controls.dll文件拖到AForge中,AForge将添加新的控件,效果如下图:
5、在窗体中放置一个videoSourcePlayer控件,用于显示摄像头的数据;并放置一个comboBox来进行不同摄像头选择;并放置一个Button用来停止显示,便于切换不同摄像头画面;
6、代码:
using System;
using System.Windows.Forms;
using AForge.Video.DirectShow;
namespace usbcamera
{
public partial class Form1 : Form
{
private FilterInfoCollection videoDevices;//所有摄像设备
private VideoCaptureDevice videoDevice;//摄像设备
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//得到所有接入的摄像设备
if (videoDevices.Count != 0)
{
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);//把摄像设备添加到摄像列表中
}
}
else
{
MessageBox.Show("没有找到摄像头!");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
videoDevice = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSourcePlayer1.VideoSource = videoDevice;
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
videoSourcePlayer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Stop();
}
}
}
我这边是接了两个可用的usb摄像头,可以实现两者之间的选择切换。
到此这篇关于C#调用usb摄像头的实现方法的文章就介绍到这了,更多相关C#调用usb摄像头内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
本文标题为:C#调用usb摄像头的实现方法
基础教程推荐
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 如果条件可以为空 2022-01-01
- 将数据集转换为列表 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03