亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

AForge官網(wǎng)

安裝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ò)濾器的方法

    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-06
  • C#解決多IfElse判斷語(yǔ)句和Switch語(yǔ)句問(wèn)題的方法分享

    C#解決多IfElse判斷語(yǔ)句和Switch語(yǔ)句問(wèn)題的方法分享

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

    C#多線程的Join()方法

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

    支持多類型數(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-01
  • Unity實(shí)現(xiàn)跑馬燈效果的示例代碼

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

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

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

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

    C#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-05
  • C# 單元測(cè)試全解析

    C# 單元測(cè)試全解析

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

    C# 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
  • C# 獲取屬性名的方法

    C# 獲取屬性名的方法

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

最新評(píng)論