C#調用usb攝像頭的實現(xiàn)方法
1、下載AForge類庫,下載地址:https://code.google.com/archive/p/aforge/downloads,我下載的版本是:AForge.NET Framework-2.2.5.exe;
2、下載安裝好后,將下載類庫中的Release文件夾復制到C#項目的可執(zhí)行文件文件夾,即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控件,用于顯示攝像頭的數(shù)據(jù);并放置一個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攝像頭,可以實現(xiàn)兩者之間的選擇切換。
到此這篇關于C#調用usb攝像頭的實現(xiàn)方法的文章就介紹到這了,更多相關C#調用usb攝像頭內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#連接SQL Server數(shù)據(jù)庫的實例講解
在本篇文章里小編給大家整理了關于C#連接SQL Server數(shù)據(jù)庫的實例內(nèi)容,有需要的朋友們參考學習下。2020-01-01
Microsoft Expression Web 簡體中文正式版 官方下載地址
Microsoft Expression Web 簡體中文正式版 官方下載地址...2007-07-07

