winform 加載AForge.dll中的videoSourcePlayer。


公司最近在做人臉識別,用到了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的方法中都有,就不贅敘了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM