C#使用AForge實現(xiàn)調(diào)用攝像頭的示例詳解
安裝AForge
Visual Studio 2022=>項目=>管理NuGet程序包,搜索AForge并依次安裝作者為AForge.NET的多個關(guān)聯(lián)組件。


使用AForge控件
安裝AForge組件完成后,工具箱會新增AForge控件,將VideoSourcePlayer拖拽到Form控件區(qū)域即可。
1.定義變量
private FilterInfoCollection filterInfoCollection; // 攝像頭設(shè)備集合 private VideoCaptureDevice videoCaptureDevice; // 捕獲設(shè)備源 Bitmap bmp; // 處理圖片
2.獲取PC端所有攝像頭集合
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
3.設(shè)置視頻源并啟動
videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[0].MonikerString); videoSourcePlayer1.VideoSource = videoCaptureDevice; videoSourcePlayer1.Start();
4.獲取一幀攝像頭數(shù)據(jù)
bmp = videoSourcePlayer1.GetCurrentVideoFrame(); // 拍照
5.關(guān)閉視頻源
if (videoSourcePlayer1.VideoSource != null)
{
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
videoSourcePlayer1.VideoSource = null;
}
示例代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
namespace CameraDemo
{
public partial class Form1 : Form
{
private FilterInfoCollection filterInfoCollection; // 攝像頭設(shè)備集合
private VideoCaptureDevice videoCaptureDevice; // 捕獲設(shè)備源
Bitmap bmp; // 處理圖片
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 檢測PC端所有攝像頭
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
MessageBox.Show("識別到:" + filterInfoCollection.Count.ToString() +"個攝像頭");
comboBox1.Items.Add(filterInfoCollection[0].MonikerString);
}
private void CloseCamera()
{
if (videoSourcePlayer1.VideoSource != null)
{
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
videoSourcePlayer1.VideoSource = null;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
TimeSpan now = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long t = Convert.ToInt64(now.TotalMilliseconds)/1000;
bmp.Save(string.Format(@"D:\{0}.jpg", t.ToString()));
MessageBox.Show("保存成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
CloseCamera();
if (comboBox1.SelectedIndex == 0 && filterInfoCollection.Count > 0)
{
videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[0].MonikerString);
}
else if (comboBox1.SelectedIndex == 1 && filterInfoCollection.Count > 1)
{
videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[1].MonikerString);
}
else {
MessageBox.Show("攝像頭選擇有誤");
return;
}
videoSourcePlayer1.VideoSource = videoCaptureDevice;
videoSourcePlayer1.Start();
button1.Enabled = true;
button2.Enabled = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
CloseCamera();
}
/// <summary>
/// 拍照
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
bmp = videoSourcePlayer1.GetCurrentVideoFrame(); // 拍照
pictureBox1.Image = bmp;
}
}
}以上就是C#使用AForge實現(xiàn)調(diào)用攝像頭的示例詳解的詳細內(nèi)容,更多關(guān)于C# AForge調(diào)用攝像頭的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實現(xiàn)Winform序在系統(tǒng)托盤顯示圖標(biāo)和開機自啟動
這篇文章主要為大家詳細介紹了C#如何實現(xiàn)Winform序在系統(tǒng)托盤顯示圖標(biāo)和開機自啟動功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2025-01-01
C#實現(xiàn)Access通用訪問類OleDbHelper完整實例
這篇文章主要介紹了C#實現(xiàn)Access通用訪問類OleDbHelper,結(jié)合完整實例形式分析了C#針對access數(shù)據(jù)庫的連接、查詢、遍歷、分頁顯示等相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
C#控制Excel Sheet使其自適應(yīng)頁寬與列寬的方法
這篇文章主要介紹了C#控制Excel Sheet使其自適應(yīng)頁寬與列寬的方法,涉及C#操作Excel的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
C#基于QRCode實現(xiàn)動態(tài)生成自定義二維碼圖片功能示例
這篇文章主要介紹了C#基于QRCode實現(xiàn)動態(tài)生成自定義二維碼圖片功能,結(jié)合實例形式分析了C#使用QRCode動態(tài)生成二維碼圖片相關(guān)操作技巧,需要的朋友可以參考下2019-02-02

