• 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
  • 相关阅读:
    设计模式-命令模式
    Java大牛必会|分布式缓存实现方案之Spring Cache
    AUTOSAR汽车电子嵌入式编程精讲300篇-基于CAN总线的温度场测量装置的研究与设计
    SQL进阶(三):Join 小技巧:提升数据的处理速度
    167.二叉树:另一棵树的字树(力扣)
    探秘磁盘的奥秘:物理结构、缓存和虚拟内存的作用
    【附源码】计算机毕业设计SSM商超销售系统
    【导航】FreeRTOS学习专栏目录 【快速跳转】
    家用摄像头怎么选:实用性,功能性以及性价比是关键
    2022 需求工程综合论述题【太原理工大学】
  • 原文地址:https://blog.csdn.net/u013420428/article/details/134371245