欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用AForge實現(xiàn)調(diào)用攝像頭的示例詳解

 更新時間:2023年11月14日 09:01:24   作者:SongYuLong的博客  
AForge是一個專門為開發(fā)者和研究者基于C#框架設(shè)計的,這個框架提供了不同的類庫和關(guān)于類庫的資源,本文為大家介紹了C#使用AForge實現(xiàn)調(diào)用攝像頭的相關(guān)教程,需要的可以了解下

AForge官網(wǎng)

安裝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#實現(xiàn)從位圖到布隆過濾器的方法

    C#實現(xiàn)從位圖到布隆過濾器的方法

    布隆過濾器(Bloom filter)是一種特殊的 Hash Table,能夠以較小的存儲空間較快地判斷出數(shù)據(jù)是否存在。常用于允許一定誤判率的數(shù)據(jù)過濾及防止緩存擊穿及等場景,本文將以 C# 語言來實現(xiàn)一個簡單的布隆過濾器,為簡化說明,設(shè)計得很簡單,需要的朋友可以參考下
    2022-06-06
  • C#解決多IfElse判斷語句和Switch語句問題的方法分享

    C#解決多IfElse判斷語句和Switch語句問題的方法分享

    這篇文章主要為大家介紹C#如何使用設(shè)計模式中的策略模式和委托來解決多個IfElse判斷語句和Switch語句,這種替換方式在其他語言也一樣可以做到,感興趣的可以了解一下
    2022-12-12
  • C#多線程的Join()方法

    C#多線程的Join()方法

    這篇文章介紹了C#多線程的Join()方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 支持多類型數(shù)據(jù)庫的c#數(shù)據(jù)庫模型示例

    支持多類型數(shù)據(jù)庫的c#數(shù)據(jù)庫模型示例

    本文為大家提供一個c#數(shù)據(jù)庫訪問模型,支持多類型數(shù)據(jù)庫,簡單抽取數(shù)據(jù)庫訪問函數(shù),大家參考使用吧
    2014-01-01
  • Unity實現(xiàn)跑馬燈效果的示例代碼

    Unity實現(xiàn)跑馬燈效果的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Unity實現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • c#操作sqlserver數(shù)據(jù)庫的簡單示例

    c#操作sqlserver數(shù)據(jù)庫的簡單示例

    這篇文章主要介紹了c#操作sqlserver數(shù)據(jù)庫的簡單示例,需要的朋友可以參考下
    2014-04-04
  • C#8.0 中開啟默認(rèn)接口實現(xiàn)方法

    C#8.0 中開啟默認(rèn)接口實現(xiàn)方法

    這篇文章主要介紹了C#8.0 中開啟默認(rèn)接口實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧的相關(guān)資料
    2019-05-05
  • C# 單元測試全解析

    C# 單元測試全解析

    這篇文章主要介紹了C# 單元測試的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C# WPF上位機實現(xiàn)和下位機TCP通訊的方法

    C# WPF上位機實現(xiàn)和下位機TCP通訊的方法

    這篇文章主要介紹了C# WPF上位機實現(xiàn)和下位機TCP通訊的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • C# 獲取屬性名的方法

    C# 獲取屬性名的方法

    C# 獲取屬性名的方法實例,需要的朋友可以參考一下
    2013-03-03

最新評論