Windows10下C# 使用EmguCV3.2 讀取本機攝像頭
參考路徑:https://blog.csdn.net/qq_36131739/article/details/77703299
前面一篇文章詳細介紹了如何在VS2013下配置EmguCV3.2。接下來,我們來編寫代碼進行攝像頭的讀取與顯示。
先新建一個Windows窗體程序項目,配置好EmguCV,具體過程請參照前一篇博客。
在設計模式下,拖一個ImageBox到窗體上。
並拖一個按鈕到窗體上,Name為startBtn,雙擊按鈕,編寫Click事件代碼。具體代碼如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Emgu.CV;
- namespace WindowsFormsApplication3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private VideoCapture capture;
- private bool isProcess = false;
- private void startBtn_Click(object sender, EventArgs e)
- {
- if(capture != null)
- {
- if(isProcess)
- {
- Application.Idle -= new EventHandler(ProcessFram);
- this.startBtn.Text = "Stop";
- }
- else
- {
- Application.Idle += new EventHandler(ProcessFram);
- this.startBtn.Text = "Start";
- }
- isProcess = !isProcess;
- }
- else
- {
- try
- {
- capture = new VideoCapture();
- }
- catch(NullReferenceException expt)
- {
- MessageBox.Show(expt.Message);
- }
- }
- }
- private void ProcessFram(object sender,EventArgs arg)
- {
- imageBox1.Image = capture.QueryFrame();
- }
- }
- }
點擊運行,可能會如下異常:System.TypeInitializationException”類型的未經處理的異常在 Emgu.CV.World.dll 中發生
其他信息: “Emgu.CV.CvInvoke”的類型初始值設定項引發異常。
解決方法:將EmguCV bin目錄下的x64 x86文件拷貝到項目下Debug目錄下如: WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug
這樣就可以解決了