zoukankan      html  css  js  c++  java
  • 使用aforg.net 录制摄像头 附源码

    这一篇在上一篇 使用aforg.net 捕获摄像头 的基础上稍加修改 增加录制功能

    录制功能使用AForge.Video.FFMPEG 需要添加对 AForge.Video.FFMPEG.dll的引用 并且拷贝AForge.NETFrameworkExternalsffmpegin路径下的全部dll到Debug目录下

    直接上代码了 对上一篇的代码稍有修改

    using AForge.Video;
    using AForge.Video.DirectShow;
    using AForge.Video.FFMPEG;
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace CameraCapture
    {
        public partial class Form1 : Form
        {
            private FilterInfoCollection filterInfoCollection;
            private VideoCaptureDevice captureDevice;
    
            //AForge.Video.FFMPEG 提供的视频写入类
            private VideoFileWriter videoFileWriter = new VideoFileWriter();
    
            private VideoCapabilities[] videoCapabilities;
            private string fileName;
            private Size frameSize;
            private int framRate;
    
            public Form1()
            {
                InitializeComponent();
                filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                foreach (FilterInfo item in filterInfoCollection)
                {
                    this.comboBox1.Items.Add(item.Name);
                }
                this.comboBox1.SelectedIndex = 0;
                //先初始化一下 否则在下面判断是否已运行时会报错
                captureDevice = new VideoCaptureDevice();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                GetVideoCapabilities();
                if (this.checkBox1.Checked)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.AddExtension = true;
                    sfd.DefaultExt = ".avi";
                    sfd.Filter = "视频文件|*.avi";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        fileName = sfd.FileName;
                    }
    
                    //文件名 宽度 高度 帧率 编码
                    videoFileWriter.Open(fileName, frameSize.Width, frameSize.Height, framRate, VideoCodec.MPEG4);
                }
                if (captureDevice.IsRunning)
                    captureDevice.Stop();
    
                captureDevice.NewFrame += captureDevice_NewFrame;
                captureDevice.Start();
            }
    
            private void captureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
            {
                this.pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
                if (this.checkBox1.Checked)
                {
                    videoFileWriter.WriteVideoFrame((Bitmap)eventArgs.Frame);
                }
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                GetVideoCapabilities();
            }
    
            /// <summary>
            ///获取摄像头分辨率
            /// </summary>
            private void GetVideoCapabilities()
            {
                captureDevice = new VideoCaptureDevice(filterInfoCollection[comboBox1.SelectedIndex].MonikerString);
                try
                {
                    videoCapabilities = captureDevice.VideoCapabilities;
    
                    foreach (VideoCapabilities capabilty in videoCapabilities)
                    {
                        if (!this.comboBox2.Items.Contains(capabilty.FrameSize))
                        {
                            this.comboBox2.Items.Add(capabilty.FrameSize);
                        }
                    }
                    if (videoCapabilities.Length == 0)
                    {
                        this.comboBox2.Items.Add("Not supported");
                    }
                    this.comboBox2.SelectedIndex = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.frameSize = captureDevice.VideoCapabilities[comboBox2.SelectedIndex].FrameSize;
                this.framRate = captureDevice.VideoCapabilities[comboBox2.SelectedIndex].FrameRate;
            }
    
            /// <summary>
            /// 关闭后结束捕获 释放资源
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (captureDevice.IsRunning)
                {
                    captureDevice.Stop();
                }
                if (videoFileWriter.IsOpen)
                {
                    videoFileWriter.Close();
                }
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
    
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    

      

    源代码下载地址:http://files.cnblogs.com/DragonX/CameraCapture2.zip

  • 相关阅读:
    session0穿透-server降权打开程序
    解决pyinstaller在单一文件时无法正确添加权限清单问题,(UAC,uac_admin,manifest,asInvoker,python,requireAdministrator)
    [随笔][Tools][在Debian9上安装Nginx]
    [随笔][乱七八糟][WebServer]
    [随笔][乱七八糟][咖啡][咖啡分类]
    [随笔][乱七八糟][正向代理与反向代理]
    [随笔][Golang][nil]
    Go语言基础之Gin框架的热启动
    BeautifulSoup的使用
    Selenium之WebDriver高级等待
  • 原文地址:https://www.cnblogs.com/DragonX/p/3751305.html
Copyright © 2011-2022 走看看