分享

winform 加载AForge.dll中的videoSourcePlayer。

 鸿蛟家平 2020-06-30

公司最近在做人脸识别,用到了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的方法中都有,就不赘叙了。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多