C# 使用AForge调用笔记本摄像头拍照及录像

1、添加引用

1、官网下载链接:http://www.aforgenet.com/framework/downloads.html
2、通过管理Nuget程序包可直接添加以下引用,当然这里并没有全部用上,而只是用上了一部分
在这里插入图片描述
在这里插入图片描述
在添加完这些引用之后VS的工具箱当中会多出AForge.NET相关的一些控件,而实现调用摄像头就需要使用到这些控件
在这里插入图片描述

2、AForge相关类库介绍

AForge.dll 是框架的核心基础类库,为其他类库提供服务。
AForge.Controls.dll 包含AForge.Net的UI控件,主要用于页面显示。
AForge.Imaging.dll 主要是框架中用于图像处理的类库,主要负责图像的处理
AForge.Video.dll 主要是框架中对视频处理的类库。
AForge.Video.DirectShow.dll 主要是通过DirectShow接口访问视频资源的类库。
AForge.Video.FFMPEG.dll 是一个还未正式发布的类库,通过FFMPEG类库对视频进行读写。

3、WinForm界面实现

在这里插入图片描述

这里解释以下界面组成及控件使用
此界面分上下两部分
上中半部分由一个GroupBox包裹一个AForge.NET工具包VideoSourcePlayer控件命名为vsp_Panel组成
中间部分由一个Label,一个ComBox下拉框cb_CameraSelect,两个Button按钮btn_Connect、btn_Close组成
下半部分由分左右俩部分
下左部分由一个GroupBox包裹一个AForge.NET工具包PictureBox控件命名为pb_BoxPanel组成
下右部分由由一个GroupBox包裹5个Button按钮btn_StartVideo、btn_PauseVideo、btn_EndVideo、btn_Capture、btn_Save。以及两个label,其中录制时间: 这个label命名无所谓后续用不到,而另外一个默认显示为00:00:00的label命名为lab_Time

4、属性

       #region 属性
        /// <summary>
        /// 设备源
        /// 用来操作摄像头
        /// </summary>
        private AForge.Video.DirectShow.VideoCaptureDevice videoCapture;
        /// <summary>
        /// 摄像头设备集合
        /// </summary>
        private AForge.Video.DirectShow.FilterInfoCollection infoCollection;
        /// <summary>
        /// 图片
        /// </summary>
        private System.Drawing.Bitmap imgMap;
        /// <summary>
        /// 文件保存路径
        /// </summary>
        private readonly string filePath = $"D:\\VS主题\\图片\\";
        /// <summary>
        /// 文件名称
        /// </summary>
        private readonly string fileName = $"{
     DateTime.Now.ToString("yyyyMMddhhmmssffff")}";
        /// <summary>
        /// 用与把每一帧图像写入到视频文件
        /// </summary>
        private AForge.Video.FFMPEG.VideoFileWriter videoWriter;
        /// <summary>
        /// 是否开始录像
        /// </summary>
        private bool IsStartVideo = false;
        /// <summary>
        /// 写入次数
        /// </summary>
        private int tick_num = 0;
        /// <summary>
        /// 录制多少小时,只是为了定时间计算录制时间使用
        /// </summary>
        private int Hour = 0;
        /// <summary>
        /// 计时器
        /// </summary>
        private System.Timers.Timer timer_count;
        #endregion

5、窗口加载与关闭

   	 	#region 窗口加载与关闭
        /// <summary>
        /// 讲题加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
   
            InitCamera();
            InitCameraSelect();
            this.btn_Capture.Enabled = false;
            this.btn_Save.Enabled = false;
            this.pb_BoxPanel.SizeMode = PictureBoxSizeMode.Zoom;//图片大小按比例适应控件,不加的话图片显示问题太大
            this.gb_CapturePanel.Visible = false;//默认不显示照片区域,拍照时在显示
                                                 //秒表
            timer_count = new System.Timers.Timer();   //实例化Timer类,设置间隔时间为1000毫秒;
            timer_count.Elapsed += new System.Timers.ElapsedEventHandler(tick_count);   //到达时间的时候执行事件;
            timer_count.AutoReset = true;   //设置是执行一次(false)还是一直执行(true);
            timer_count.Interval = 1000;
        }
        /// <summary>
        /// 窗口关闭时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
   
            EndCamera();
        }
        #endregion

6、实例化

    	#region 实例化
        /// <summary>
        /// 实例化摄像头
        /// </summary>
        public void InitCamera()
        {
   
            try
            {
   
                //检测电脑上的摄像头设备
                infoCollection = new AForge.Video.DirectShow.FilterInfoCollection(AForge.Video.DirectShow.FilterCategory.VideoInputDevice);
            }
            catch (Exception ex)
            {
   
                MessageBox.Show($"没有找到设备:{
     ex.Message}", "Error");
            }

        }
        //加载摄像头选择
        public void InitCameraSelect()
        {
   
            this.cb_CameraSelect.DropDownStyle = ComboBoxStyle.DropDownList;
            //根据读取到的摄像头加载选择项
            foreach (AForge.Video.DirectShow.FilterInfo item in infoCollection)
            {
   
                this.cb_CameraSelect.Items.Add(item.Name);
            }
            //for (int i = 1; i <= infoCollection.Count; i++)
            //{
   
            //    this.cb_CameraSelect.Items.Add("摄像头" + i);
            //}
        }
        #endregion

7、 摄像头的连接读取与关闭

 		#region  摄像头的连接读取与关闭
        /// <summary>
        /// 连接按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Connect_Click(object sender, EventArgs e)
        {
   
            var CameraSelect = this.cb_CameraSelect.Text;
            var selectIndex = this.cb_CameraSelect.SelectedIndex;
            //已有连接的摄像头时先关闭
            if (videoCapture != null)
            {
   
                EndCamera();
            }
            videoCapture = new AForge.Video.DirectShow.VideoCaptureDevice(infoCollection[selectIndex].
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值