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)用攝像頭的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于C# AForge調(diào)用攝像頭的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#解決多IfElse判斷語句和Switch語句問題的方法分享
這篇文章主要為大家介紹C#如何使用設(shè)計模式中的策略模式和委托來解決多個IfElse判斷語句和Switch語句,這種替換方式在其他語言也一樣可以做到,感興趣的可以了解一下2022-12-12支持多類型數(shù)據(jù)庫的c#數(shù)據(jù)庫模型示例
本文為大家提供一個c#數(shù)據(jù)庫訪問模型,支持多類型數(shù)據(jù)庫,簡單抽取數(shù)據(jù)庫訪問函數(shù),大家參考使用吧2014-01-01c#操作sqlserver數(shù)據(jù)庫的簡單示例
這篇文章主要介紹了c#操作sqlserver數(shù)據(jù)庫的簡單示例,需要的朋友可以參考下2014-04-04