|
楼主 |
发表于 2015-3-19 18:16:06
|
显示全部楼层
- 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
- {
- 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,
- Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS);
- 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();
- }
- }
- }
- }
- }
复制代码 |
|