VS2010+C#+EmguCV读取和录制视频

标签:
cemgu视频录制视频播放 |
分类: C#学习 |
本人EmguCV和C#新手,参考网上的例子,经过艰苦的查错过程,终于编写完成了该程序。利用EmguCV播放视频的原理是:将视频看作图片,用capture获取抓取通道,通过不断的调用{frame
= capture.QueryFrame();VideoBox.Image = frame;}语句实现图像的实时显示。
事先要安装EmguCV库并配置环境变量,具体方法网搜。
public partial class
Form1 : Form
{
private Capture capture;
Image frame;
VideoWriter videowriter;//
int VideoFps;//视频帧率
public Form1()
{
InitializeComponent();
}
private void Record_Click(object sender,
EventArgs e)
{
if
(Record.Text=="录制")
{//打开摄像头并开始录制
try
{
SaveFileDialog saveFileDialog = new
SaveFileDialog();
saveFileDialog.Filter = "AVI|*.avi";
saveFileDialog.AddExtension = true;
if (saveFileDialog.ShowDialog() ==
DialogResult.OK)
{
capture =
new Capture();
videowriter = new VideoWriter(saveFileDialog.FileName, //文件名
CvInvoke.CV_FOURCC('M', 'J',
'P', 'G'), //编码格式
25, //帧率
(int)CvInvoke.cvGetCaptureProperty(capture,
Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH), //视频宽度
(int)CvInvoke.cvGetCaptureProperty(capture,
Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT), //视频高度
true);//彩色
Application.Idle += new EventHandler(ProcessFrame);
Record.Text = "停止";
Play.Enabled = false;
}
}
catch (NullReferenceException
excpt)
{
MessageBox.Show(excpt.Message);
}
}
else
{//关闭摄像头,停止录制
capture.Dispose();
Application.Idle -= new
EventHandler(ProcessFrame);
videowriter.Dispose();
Record.Text = "录制";
Play.Enabled = true;
}
}
private void Play_Click(object sender, EventArgs
e)
{
if
(Play.Text == "播放")
{//开启播放模式
OpenFileDialog openFileDialog
= new OpenFileDialog();
openFileDialog.Filter =
"AVI文件|*.avi|RMVB文件|*.rmvb|WMV文件|*.wmv|MKV文件|*.mkv|所有文件|*.*";
if
(openFileDialog.ShowDialog() == DialogResult.OK)
{
Application.Idle += new
EventHandler(ProcessFrame);
capture = new
Capture(openFileDialog.FileName);
VideoFps =
(int)CvInvoke.cvGetCaptureProperty(capture,
Play.Text = "停止";
VideoBox.Image = null;
Record.Enabled = false;
}
}
else
{
capture.Dispose();
Application.Idle -= new
EventHandler(ProcessFrame);
Play.Text = "播放";
Record.Enabled = true;
}
}
private void ProcessFrame(object sender,
EventArgs arg)
{
if
(Play.Text != "播放")//正在播放视频
{
frame =
capture.QueryFrame();
if (frame != null)
{
//为使播放顺畅,添加以下延时
System.Threading.Thread.Sleep((int)(1000.0 /
VideoFps - 5));
VideoBox.Image = frame;
}
else
{
Play.Text = "播放";
Record.Enabled = true;
VideoBox.Image = null;
}
}
else if
(Record.Text != "录制")//正在录制
{
try
{
frame = capture.QueryFrame();
VideoBox.Image = frame;
videowriter.WriteFrame(frame);
}
catch
{
System.Diagnostics.Process.Start(Application.ExecutablePath);
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
}
}
}
1.新建Visual C#下的Windows窗体项目MyEmgu。
http://s12/mw690/9d102bd7t7c71010b694b&690
2.工程窗口点击->工具->选择工具箱项,浏览并到EmguCV的安装路径中添加Emgu.CV.UI.dll文件,在\安装路径\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin目录下。添加后效果如下:
2.工程窗口点击->工具->选择工具箱项,浏览并到EmguCV的安装路径中添加Emgu.CV.UI.dll文件,在\安装路径\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin目录下。添加后效果如下:
出现了HistogramBox和ImageBox等选项。
3.解决方案->工程名->引用,右键“添加引用”->浏览,到EmguCV安装路径选择添加Emgu.CV.dll,Emgu.CV.ML.dll,Emgu.CV.UI.dll,Emgu.Util.dll,ZedGraph.dll等文件:
http://s12/mw690/9d102bd7tdc6a45154a2b&690
4.在Form1中拖入两个按钮控件和一个ImageBox控件(在工具箱最后的“常规”选项中),并分别命名为“Record”、“Play”和“VideoBox”,VideoBox的SizeMode设为StretchImage。
4.在Form1中拖入两个按钮控件和一个ImageBox控件(在工具箱最后的“常规”选项中),并分别命名为“Record”、“Play”和“VideoBox”,VideoBox的SizeMode设为StretchImage。
5.分别双击两个按钮,为其添加事件处理程序。全部的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//添加的头文件
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using Emgu.Util;
namespace MyEmgu
{
Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS);
}
6.视频播放效果
7.录像效果