1、Emgucv錄製視頻ide
Emgucv中的Capture類能夠完成視頻文件的讀取,利用EmguCV播放視頻的原理是:將視頻看做圖片,用capture獲取抓取通道,經過不斷的調用{frame =
capture.QueryFrame();imageBox1.Image = frame;}語句實現圖像的實時顯示。函數
錄製視頻時用到了VideoWriter()這個函數,它有兩種構造方法,分別爲VideoWriter(string fileName, int compressionCode, int fps, int width, int height, bool isColor)和VideoWriter(string fileName, int fps, int width, int height, bool isColor),其中:this
fileName:文件的保存路徑;編碼
compressionCode:視頻的編碼方式;spa
fps:錄製視頻的幀率;.net
width:視頻的寬度;插件
height:視頻的高度;3d
isColor:是不是彩色;code
最後調用WriteFrame()方法錄製視頻orm
首先咱們要作的第一步就是打開電腦自帶的攝像頭,其代碼爲:
1 private Capture capture; 2 private void btnStart_Click(object sender, EventArgs e) 3 { 4 capture = new Capture(); 5 Application.Idle += new EventHandler(processfram); 6 } 7 private void processfram(object sender, EventArgs arg) 8 { 9 frame = capture.QueryFrame(); 10 imageBox1.Image = frame; 11 }
接下來再錄製視頻,其關鍵代碼爲:
1 vw = new VideoWriter("E:\\1.avi", 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); 2 Application.Idle += new EventHandler(processfram);
其完整代碼爲:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using Emgu.CV; 10 using Emgu.CV.Structure; 11 12 namespace test 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 private Capture capture; 21 Image<Bgr, Byte> frame; 22 VideoWriter vw; 23 bool flag = false; 24 private void btnStart_Click(object sender, EventArgs e) 25 { 26 btnRecord.Enabled = true; 27 capture = new Capture(); 28 Application.Idle += new EventHandler(processfram); 29 } 30 private void processfram(object sender, EventArgs arg) 31 { 32 frame = capture.QueryFrame(); 33 imageBox1.Image = frame; 34 if(flag) 35 vw.WriteFrame(frame); 36 } 37 38 private void btnRecord_Click(object sender, EventArgs e) 39 { 40 if (btnRecord.Text == "錄製") 41 { 42 if (MessageBox.Show("開始錄製嗎?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) 43 { 44 flag = true; 45 vw = new VideoWriter("E:\\1.avi", 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); 46 Application.Idle += new EventHandler(processfram); 47 btnRecord.Text = "暫停"; 48 } 49 } 50 else 51 { 52 if (MessageBox.Show("中止錄製嗎?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) 53 { 54 flag = false; 55 Application.Idle -= new EventHandler(processfram); 56 vw.Dispose(); 57 btnRecord.Text = "錄製"; 58 } 59 } 60 } 61 } 62 }
2、Aforge錄製視頻(在上一篇博客的基礎上,因此在此以前先看完上一篇博客)
Aforge錄製視頻調用的是AVIWriter()函數,因此在此以前先要引入AForge.Video.VFW.dll程序集,其具體步驟以下:
(1)先new一個AVIWriter對象,如AVIWriter writer = new AVIWriter("wmv3"),此表明的是錄製的事avi格式的視頻,可是必須先要裝個插件,下載地址http://blog.csdn.net/wletv/article/details/9188417,惟一的缺點就是不能64位的系統下運行;若是不加「wmv3」則沒有這個問題,但是錄製幾秒就有一兩百兆了,十分的不可取(若是誰有更好的解決辦法請及時告訴我哦:));
(2)接着調用其Open方法,如writer.Open(string fileName, int width, int height),其中fileName爲路徑,width爲寬度,height爲高度;
(3)寫入視頻文件,調用AddFrame方法;
完整代碼爲:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using AForge.Video.DirectShow; 10 using AForge.Video; 11 using System.Diagnostics; 12 using AForge.Video.VFW; 13 14 namespace Aforge調用攝像頭 15 { 16 public partial class Form1 : Form 17 { 18 private FilterInfoCollection videoDevices; 19 private VideoCaptureDevice videoSource; 20 AVIWriter writer = new AVIWriter(); 21 private Bitmap bitmap,img; 22 public Form1() 23 { 24 InitializeComponent(); 25 } 26 27 private void Form1_Load(object sender, EventArgs e) 28 { 29 try 30 { 31 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//枚舉全部的視頻輸入設備 32 if (videoDevices.Count == 0) 33 throw new ApplicationException(); 34 foreach (FilterInfo device in videoDevices) 35 { 36 cbCapture.Items.Add(device.Name);//把全部的視頻設備添加到下拉框中 37 } 38 cbCapture.SelectedIndex = 0; 39 videoSource = new VideoCaptureDevice(videoDevices[cbCapture.SelectedIndex].MonikerString);//攝像頭的名稱 40 videoSource.DesiredFrameSize = new Size(500, 300);//設置大小 41 videoSource.DesiredFrameRate = 1;//設置幀率 42 videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); 43 videoSource.Start(); 44 } 45 catch (Exception ex) 46 { 47 MessageBox.Show(ex.Message); 48 } 49 } 50 private void btnStart_Click(object sender, EventArgs e) 51 { 52 videoSource.Stop(); 53 videoSource = new VideoCaptureDevice(videoDevices[cbCapture.SelectedIndex].MonikerString);//攝像頭的名稱 54 videoSource.DesiredFrameSize = new Size(500, 300);//設置大小 55 videoSource.DesiredFrameRate = 1;//設置幀率 56 videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); 57 videoSource.Start(); 58 } 59 private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) 60 { 61 bitmap = (Bitmap)eventArgs.Frame.Clone(); 62 pictureBox1.Image = bitmap; 63 } 64 65 private void btnConfig_Click(object sender, EventArgs e) 66 { 67 if ((videoSource != null) && (videoSource is VideoCaptureDevice)) 68 { 69 try 70 { 71 ((VideoCaptureDevice)videoSource).DisplayPropertyPage(this.Handle); 72 } 73 catch (NotSupportedException ex) 74 { 75 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 76 } 77 } 78 } 79 80 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 81 { 82 Process p = Process.GetCurrentProcess(); 83 if (p.HasExited == false) 84 { 85 p.Kill(); 86 } 87 } 88 private void btnRecord_Click(object sender, EventArgs e) 89 { 90 if (btnRecord.Text == "錄製") 91 { 92 if (MessageBox.Show("Start Recording?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) 93 { 94 writer = new AVIWriter("wmv3"); 95 writer.Open("E:\\1.avi", bitmap.Width, bitmap.Height); 96 videoSource.NewFrame += new NewFrameEventHandler(video_NewFrames); 97 btnRecord.Text = "中止"; 98 } 99 } 100 else 101 { 102 if (MessageBox.Show("Stop Recording?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) 103 { 104 writer.Dispose();//不dispose掉會報錯,不能是close 105 btnRecord.Text = "錄製"; 106 } 107 } 108 } 109 private void video_NewFrames(object sender, NewFrameEventArgs eventArgs) 110 { 111 img = (Bitmap)eventArgs.Frame.Clone(); 112 writer.AddFrame(img); 113 } 114 } 115 }