准备工作与这篇文章相同
新建Visual C# 控制台项目CameraCapture
然后添加引用(Emgu.CV.dll Emgu.CV.UI.dll Emgu.Util.dll)
完善代码(其中核心代码Line#12~#18,真的只有5行)
using System;
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using System.Drawing;
using System.Windows.Forms;
class CameraCapture
{
static void Main(string[] args)
{
ImageViewer viewer = new ImageViewer(); //创建图像视窗
Capture capture = new Capture(); //创建摄像头捕获
Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
{ // “Idle”处理循环的事件处理过程
viewer.Image = capture.QueryFrame(); //在视窗中显示抓取的帧图像
});
viewer.ShowDialog(); //显示图像视窗
}
}
运行效果
类似传说中的“几行代码就实现了某某强大的功能”,其实背后隐藏了许多实现细节
我们可以这样写一个类,封装许多强大的功能然后打包,比如Dancer类
然后新建项目,引用写好的类,编写如下6行代码,跳一段美妙的夏日华尔兹
using System;
using LibDance;
void Main(string[] args)
{
Dancer waltzDancer=new Dancer(DanceTypes.WALTZ_SLOW);
string bkSong="Waltz in 7 of 8.mp3";
while(waltzDancer.Dance(bkSong));
}
纯属娱乐,呵呵
上述(摄像头监视的)代码中Capture类就是一个功能丰富的类,可以实现很多功能,
读者如果感兴趣的话可以自行探索
另外上述代码中的EventHandler, Delegate是一些高级内容,在此暂且不深究
我可能会在后续文章中慢慢探讨