• C# 使用AForge调用摄像头


    C# 使用AForge调用摄像头

    AForge官网

    安装AForge

    Visual Studio 2022=>项目=>管理NuGet程序包,搜索AForge并依次安装作者为AForge.NET的多个关联组件。
    在这里插入图片描述
    在这里插入图片描述

    使用AForge控件

    安装AForge组件完成后,工具箱会新增AForge控件,将VideoSourcePlayer拖拽到Form控件区域即可。

    1. 定义变量
      private FilterInfoCollection filterInfoCollection; // 摄像头设备集合
      private VideoCaptureDevice videoCaptureDevice; // 捕获设备源
      Bitmap bmp; // 处理图片
    2. 获取PC端所有摄像头集合
      filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    3. 设置视频源并启动
      videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[0].MonikerString);
      videoSourcePlayer1.VideoSource = videoCaptureDevice;
      videoSourcePlayer1.Start();
    4. 获取一帧摄像头数据
      bmp = videoSourcePlayer1.GetCurrentVideoFrame(); // 拍照
    5. 关闭视频源
      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; // 摄像头设备集合
            private VideoCaptureDevice videoCaptureDevice;  // 捕获设备源
            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();
            }
    
            /// 
            /// 拍照
            /// 
            /// 
            /// 
            private void button1_Click(object sender, EventArgs e)
            {
                bmp = videoSourcePlayer1.GetCurrentVideoFrame(); // 拍照
                pictureBox1.Image = bmp;            
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
  • 相关阅读:
    请实现一个链式的哈希表
    校园网页设计成品 学校班级网页制作模板 dreamweaver网页作业 简单网页课程成品 大学生静态HTML网页源码
    【广州华锐互动】石油钻井井控VR互动实训系统
    ipad协议超稳定
    阿里巴巴Java方向面试题汇总(含答案)
    WebRTC研究:丢包与抖动
    Flink -- window(窗口)
    Rest和Http什么关系?
    【C语言初级】0 准备工作
    百数报表——帮助企业数据化重要工具
  • 原文地址:https://blog.csdn.net/u013420428/article/details/134371245