基于AForge、Accord实现C#摄像头画面显示以及视频录制

一、右键项目—管理NuGet程序包
在这里插入图片描述
二、安装AForge、AForge.Imaging、AForge.Controls、AForge.Math、AForge.Video、AForge.Video.DirectShow、Accord、Accord.Video、Accord.Video.FFMPEG包。
三、引用

using System.Drawing;
using System.Diagnostics;
using System.IO;
using AForge;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using Accord.Video.FFMPEG;

四、画面显示,视频录制以及按下“esc”关闭摄像头,结束录制,退出程序。

        //检测摄像头视频输入设备列表
        private FilterInfoCollection videodevices;
        //定义视频流对象
        private VideoCaptureDevice[] videoSource=new VideoCaptureDevice[4];
        //写入到视频
        private VideoFileWriter[] videoWriter=new VideoFileWriter[4];
        //视频宽和高
        private int vedioWidth = 1280;
        private int vedioHeight = 720;

        public MainWindow()
        {
            InitializeComponent();
            InitCamera();
            InitCamera1();
            this.KeyDown += MainWindow_KeyDown;
        }
        private void InitCamera()
        {
            //初始化摄像头对象
            videodevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            //判断是否识别到摄像头设备
            if (videodevices.Count == 0)
            {
                videodevices = null;
                return;
            }
            //通过摄像头名字(设备编号),实例化视频源
            videoSource[0] = new VideoCaptureDevice(videodevices[0].MonikerString);
            videoSource[0].Start();

            string dirpath=System.IO.Path.Combine(Environment.CurrentDirectory,"相机");
            if (!Directory.Exists(dirpath))
            {
                Directory.CreateDirectory(dirpath);
            }

            string dirpath2 = System.IO.Path.Combine(dirpath, DateTime.Now.ToString("yyyyMMdd_HH_mm_ss"));
            if (!Directory.Exists(dirpath2))
            {
                Directory.CreateDirectory(dirpath2);
            }
            //string fileName = System.IO.Path.Combine($"{Environment.CurrentDirectory}/相机/{DateTime.Now.ToString("yyyyMMdd")}/DesktopRecord_{DateTime.Now.ToString("yyyyMMddHHmmss")}.mp4");
            string fileName = System.IO.Path.Combine(dirpath2, "video1.mp4");
            //string outputDirectory = AppDomain.CurrentDomain.BaseDirectory; // 输出目录,这里使用程序的基目录
            //string outputVideoPath = System.IO.Path.Combine(outputDirectory, "outputVideo.mp4"); // 输出视频文件路径
            videoWriter[0] = new VideoFileWriter();
            videoWriter[0].Open(fileName, vedioWidth, vedioHeight, 30, VideoCodec.MPEG4);

            vedioPlayer.VideoSource = videoSource[0];
            vedioPlayer.NewFrame += VideoSource_NewFrame;          
        }

        private void InitCamera1()
        {
            //初始化摄像头对象
            videodevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            //判断是否识别到摄像头设备
            if (videodevices.Count == 0)
            {
                videodevices = null;
                return;
            }
            //通过摄像头名字(设备编号),实例化视频源
            videoSource[1]= new VideoCaptureDevice(videodevices[1].MonikerString);
            videoSource[1].Start();

            string dirpath = System.IO.Path.Combine(Environment.CurrentDirectory, "相机");
            if (!Directory.Exists(dirpath))
            {
                Directory.CreateDirectory(dirpath);
            }

            string dirpath2 = System.IO.Path.Combine(dirpath, DateTime.Now.ToString("yyyyMMdd_HH_mm_ss"));
            if (!Directory.Exists(dirpath2))
            {
                Directory.CreateDirectory(dirpath2);
            }
            //string fileName = System.IO.Path.Combine($"{Environment.CurrentDirectory}/相机/{DateTime.Now.ToString("yyyyMMdd")}/DesktopRecord_{DateTime.Now.ToString("yyyyMMddHHmmss")}.mp4");
            string fileName = System.IO.Path.Combine(dirpath2, "video2.mp4");
            // string outputDirectory = AppDomain.CurrentDomain.BaseDirectory; // 输出目录,这里使用程序的基目录
            //string outputVideoPath = System.IO.Path.Combine(outputDirectory, "outputVideo1.mp4"); // 输出视频文件路径
            videoWriter[1] = new VideoFileWriter();
            videoWriter[1].Open(fileName, vedioWidth, vedioHeight, 15, VideoCodec.MPEG4);

            vedioPlayer1.VideoSource = videoSource[1];
            vedioPlayer1.NewFrame += VideoSource_NewFrame1;

        }


        private void VideoSource_NewFrame(object sender, ref System.Drawing.Bitmap image)
        {
            //image.RotateFlip(RotateFlipType.Rotate180FlipNone);
            image.RotateFlip(RotateFlipType.Rotate180FlipY);
            vedioWidth = image.Width;
            vedioHeight = image.Height;
            try
            {
                //写入到视频
                videoWriter[0].WriteVideoFrame(image);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        private void VideoSource_NewFrame1(object sender, ref System.Drawing.Bitmap image)
        {
            //image.RotateFlip(RotateFlipType.Rotate180FlipNone);
            image.RotateFlip(RotateFlipType.Rotate180FlipY);
            vedioWidth = image.Width;
            vedioHeight = image.Height;
            try
            {
                //写入到视频
                videoWriter[1].WriteVideoFrame(image);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                for(int i = 0; i < 4; i++)
                {
                    if (videoSource[i] != null && videoSource[i].IsRunning)
                    {
                        videoSource[i].SignalToStop();
                        videoSource[i] = null;
                        videoWriter[i].Close();
                        videoWriter[i].Dispose();
                        videoWriter[i] = null;
                    }
                }
                this.KeyDown -= MainWindow_KeyDown;
                this.Close();//关闭当前窗口
            }
        }
  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值