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控件,用于显示c#教程摄像头的数据;并放置一个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摄像头的实现方法的文章就介绍到这了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值