码迷,mamicode.com
首页 > Windows程序 > 详细

winform 加载AForge.dll中的videoSourcePlayer。

时间:2019-11-16 18:07:50      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:item   img   show   使用   mon   count()   send   cts   moni   

公司最近在做人脸识别,用到了videoSourcePlayer 但是引入了AForge.dll之后,工具箱中并没有videoSourcePlayer,这时就需要自己添加选项卡了。很简单几步就能完成。

首先右键工具箱--->添加选项卡

技术图片

 

 

 名字随便起,我里就叫AForge,然后选中选项卡右键选择项

技术图片

 

 

 选择dll,点击OK 

技术图片

 

 

 再次点击添加到选项卡中

技术图片

 

 

 就OK了。

然后是控件的使用。将videoSourcePlayer拖到winform中。进入后台代码中

 

 private FilterInfoCollection videoDevices;

//获取摄像头

private void Form1_Load(object sender, EventArgs e)
{
try
{
// 枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

if (videoDevices.Count == 0)
throw new ApplicationException();

foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
//下拉框用来更换摄像头
comboBox1.SelectedIndex = 0;

}
catch (ApplicationException)
{
comboBox1.Items.Add("未发现摄像头");
comboBox1.SelectedIndex = 0;
videoDevices = null;
}
}

 

 //关闭摄像头
        private void button2_Click(object sender, EventArgs e)
        {
            if (videoSourcePlayer != null && videoSourcePlayer.IsRunning)
            {
                videoSourcePlayer.SignalToStop();
                videoSourcePlayer.WaitForStop();
            }

        }
        //打开摄像头
        private void button1_Click(object sender, EventArgs e)
        {
            button2_Click(null, null);
            if (comboBox1.SelectedItem.ToString() == "未发现摄像头")
            {
                MessageBox.Show("未发现摄像头", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            videoSource.VideoResolution = videoSource.VideoCapabilities[comboBox2.SelectedIndex];

            videoSourcePlayer.VideoSource = videoSource;
            videoSourcePlayer.Start();
        }

 //换摄像头
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "未发现摄像头")
            {
                comboBox2.Items.Add("未发现摄像头");
                comboBox2.SelectedIndex = 0;
                return;
            }
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            if (videoSource.VideoCapabilities.Count() == 0)
            {
                comboBox2.Items.Add("摄像头异常");
                comboBox2.SelectedIndex = 0;
                return;
            }
            comboBox2.Items.Clear();
            foreach (AForge.Video.DirectShow.VideoCapabilities FBL in videoSource.VideoCapabilities)
            {
                comboBox2.Items.Add(FBL.FrameSize.Width + "*" + FBL.FrameSize.Height);
            }

            comboBox2.SelectedIndex = 0;
            button1_Click(null, null);

        }

总共就4个方法,首先获取摄像头,然后 打开摄像头 ,关闭摄像头,在更换摄像头中有获取分辨率的方法,这样就能打摄像头并在videoSourcePlayer中看到视频了

需要获取当前画面,就调用

Bitmap bitmap = videoSourcePlayer.GetCurrentVideoFrame();

基本上在videoSourcePlayer的方法中都有,就不赘叙了。

winform 加载AForge.dll中的videoSourcePlayer。

标签:item   img   show   使用   mon   count()   send   cts   moni   

原文地址:https://www.cnblogs.com/helloliu/p/11872311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!