C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的示例詳解
安裝AForge
Visual Studio 2022=>項(xiàng)目=>管理NuGet程序包,搜索AForge并依次安裝作者為AForge.NET的多個(gè)關(guān)聯(lián)組件。
使用AForge控件
安裝AForge組件完成后,工具箱會(huì)新增AForge控件,將VideoSourcePlayer拖拽到Form控件區(qū)域即可。
1.定義變量
private FilterInfoCollection filterInfoCollection; // 攝像頭設(shè)備集合 private VideoCaptureDevice videoCaptureDevice; // 捕獲設(shè)備源 Bitmap bmp; // 處理圖片
2.獲取PC端所有攝像頭集合
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
3.設(shè)置視頻源并啟動(dòng)
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) { // 檢測(cè)PC端所有攝像頭 filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice); MessageBox.Show("識(shí)別到:" + filterInfoCollection.Count.ToString() +"個(gè)攝像頭"); 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實(shí)現(xiàn)調(diào)用攝像頭的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于C# AForge調(diào)用攝像頭的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)從位圖到布隆過(guò)濾器的方法
布隆過(guò)濾器(Bloom filter)是一種特殊的 Hash Table,能夠以較小的存儲(chǔ)空間較快地判斷出數(shù)據(jù)是否存在。常用于允許一定誤判率的數(shù)據(jù)過(guò)濾及防止緩存擊穿及等場(chǎng)景,本文將以 C# 語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的布隆過(guò)濾器,為簡(jiǎn)化說(shuō)明,設(shè)計(jì)得很簡(jiǎn)單,需要的朋友可以參考下2022-06-06C#解決多IfElse判斷語(yǔ)句和Switch語(yǔ)句問(wèn)題的方法分享
這篇文章主要為大家介紹C#如何使用設(shè)計(jì)模式中的策略模式和委托來(lái)解決多個(gè)IfElse判斷語(yǔ)句和Switch語(yǔ)句,這種替換方式在其他語(yǔ)言也一樣可以做到,感興趣的可以了解一下2022-12-12支持多類型數(shù)據(jù)庫(kù)的c#數(shù)據(jù)庫(kù)模型示例
本文為大家提供一個(gè)c#數(shù)據(jù)庫(kù)訪問(wèn)模型,支持多類型數(shù)據(jù)庫(kù),簡(jiǎn)單抽取數(shù)據(jù)庫(kù)訪問(wèn)函數(shù),大家參考使用吧2014-01-01c#操作sqlserver數(shù)據(jù)庫(kù)的簡(jiǎn)單示例
這篇文章主要介紹了c#操作sqlserver數(shù)據(jù)庫(kù)的簡(jiǎn)單示例,需要的朋友可以參考下2014-04-04C#8.0 中開(kāi)啟默認(rèn)接口實(shí)現(xiàn)方法
這篇文章主要介紹了C#8.0 中開(kāi)啟默認(rèn)接口實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧的相關(guān)資料2019-05-05C# WPF上位機(jī)實(shí)現(xiàn)和下位機(jī)TCP通訊的方法
這篇文章主要介紹了C# WPF上位機(jī)實(shí)現(xiàn)和下位機(jī)TCP通訊的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03