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摄像头内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。