基于AForge實現(xiàn)C#攝像頭視頻錄制功能
本文為大家分享了AForge實現(xiàn)C#攝像頭視頻錄制功能的具體方法,供大家參考,具體內(nèi)容如下
1. 概述
最近由于興趣學習了下在C#上使用AForge錄制攝像頭視頻并壓縮編碼??傮w上來說這個第三方.net視覺開發(fā)庫還是比較穩(wěn)定的(AForge lib下載、離線幫助文檔下載)。但是由于這個第三方庫維護不怎么樣,導致會出現(xiàn)不兼容的問題。這里將這些與大家分享,希望對您有幫助。
在使用AForge第三方庫錄制本地視頻所要使用到的類主要有這幾個:FilterInfoCollection、VideoCaptureDevice、VideoSourcePlayer、VideoFileWriter。下面我就簡單的介紹一下這幾個類涉及到的方法等。
FilterInfoCollection:
該類主要是用于攝像頭視頻輸入設(shè)備列表檢測的。是繼承自C#中的System.Collections.CollectionBase類。
這是離線幫助文檔上對這個類的調(diào)用方式:
其中構(gòu)造函數(shù)傳遞進去的參數(shù)是需要采集的信號的類型,他還有其他的輸入類型(如聲音等):
VideoCaptureDevice:
這是該類中的一些成員函數(shù)和變量
這個類便是要選擇了視頻輸入的設(shè)備了,他的構(gòu)造函數(shù)是
在實際的使用過程中可能會存在多個設(shè)備的情況,便可以通過第二個參數(shù)進行輸入設(shè)備的指定。初始化是這樣的
在本例子的實際使用過程中對上面該類事件NewFrame函數(shù)進行了響應(yīng),然后提取出當前幀。
VideoSourcePlayer:
該類是AForge.Control中的類,是控件中調(diào)用的,這里將它添加進來是為了作為拍照功能使用的,這里就不做介紹了。
VideoFileWriter:
該類是視頻寫操作類,主要實現(xiàn)視頻文件的壓縮和寫入到文件中。
本例子中先使用VideoFileWriter.Open()函數(shù)設(shè)定錄制視頻的高度、寬度、幀率、編碼類型。
這是該第三方類庫支持的視頻編碼格式
然后使用下面這個函數(shù)就可以將當前幀寫入到視頻文件中了。
2. 實現(xiàn)
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 System.Timers;
//using AForge
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Controls;
namespace video_record
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//關(guān)閉窗口響應(yīng)函數(shù)
private void button2_Click(object sender, EventArgs e)
{
if (this.writer.IsOpen)
{
MessageBox.Show("視頻流還沒有寫完,請點擊結(jié)束錄制。", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
this.videoSource.SignalToStop();
this.videoSource.WaitForStop();
this.videoSourcePlayer.SignalToStop();
this.videoSourcePlayer.WaitForStop();
this.Hide();
this.Close();
this.Dispose();
}
private FilterInfoCollection videoDevices; //攝像頭設(shè)備
private VideoCaptureDevice videoSource; //視頻的來源選擇
private VideoSourcePlayer videoSourcePlayer; //AForge控制控件
private VideoFileWriter writer; //寫入到視頻
private bool is_record_video = false; //是否開始錄像
System.Timers.Timer timer_count;
int tick_num = 0;
//窗體初始化函數(shù)
private void Form1_Load(object sender, EventArgs e)
{
this.label5.Visible = false;
this.videoSourcePlayer = new AForge.Controls.VideoSourcePlayer();
this.videoSource = new VideoCaptureDevice();
this.writer = new VideoFileWriter();
//設(shè)置視頻編碼格式
this.comboBox_videoecode.Items.Add("Raw");
this.comboBox_videoecode.Items.Add("MPEG2");
this.comboBox_videoecode.Items.Add("FLV1");
this.comboBox_videoecode.Items.Add("H263p");
this.comboBox_videoecode.Items.Add("MSMPEG4v3");
this.comboBox_videoecode.Items.Add("MSMPEG4v2");
this.comboBox_videoecode.Items.Add("WMV2");
this.comboBox_videoecode.Items.Add("WMV1");
this.comboBox_videoecode.Items.Add("MPEG4");
this.comboBox_videoecode.SelectedIndex = 1;
//設(shè)置視頻來源
try
{
// 枚舉所有視頻輸入設(shè)備
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
throw new ApplicationException(); //沒有找到攝像頭設(shè)備
foreach (FilterInfo device in videoDevices)
{
this.comboBox_camera.Items.Add(device.Name);
}
//this.comboBox_camera.SelectedIndex = 0; //注釋掉,選擇攝像頭來源的時候才會才會觸發(fā)顯示攝像頭信息
}
catch (ApplicationException)
{
videoDevices = null;
MessageBox.Show("沒有找到攝像頭設(shè)備", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//秒表
this.timer_count = new System.Timers.Timer(); //實例化Timer類,設(shè)置間隔時間為10000毫秒;
this.timer_count.Elapsed += new System.Timers.ElapsedEventHandler(tick_count); //到達時間的時候執(zhí)行事件;
this.timer_count.AutoReset = true; //設(shè)置是執(zhí)行一次(false)還是一直執(zhí)行(true);
this.timer_count.Interval = 1000;
}
//視頻源選擇下拉框選擇之后的響應(yīng)函數(shù)
private void comboBox_camera_SelectedIndexChanged(object sender, EventArgs e)
{
int selected_index = this.comboBox_camera.SelectedIndex;
this.videoSource = new VideoCaptureDevice(videoDevices[selected_index].MonikerString);
// set NewFrame event handler
videoSource.NewFrame += new NewFrameEventHandler(show_video);
videoSource.Start();
videoSourcePlayer.VideoSource = videoSource;
videoSourcePlayer.Start();
this.label5.Text = "連接中...";
this.label5.Visible = true;
isshowed = true;
}
bool isshowed = true;
//新幀的觸發(fā)函數(shù)
private void show_video(object sender, NewFrameEventArgs eventArgs)
{
if (isshowed)
{
this.label5.Visible = false;
isshowed = false;
}
Bitmap bitmap = eventArgs.Frame; //獲取到一幀圖像
pictureBox1.Image = Image.FromHbitmap(bitmap.GetHbitmap());
if (is_record_video)
{
writer.WriteVideoFrame(bitmap);
}
}
//拍攝圖像按鈕響應(yīng)函數(shù)
private void button1_Click(object sender, EventArgs e)
{
if (this.videoSource.IsRunning && this.videoSourcePlayer.IsRunning)
{
Bitmap bitmap = this.videoSourcePlayer.GetCurrentVideoFrame();
bitmap.Save("img.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
MessageBox.Show("攝像頭沒有運行", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//開始錄像按鈕響應(yīng)函數(shù)
private void button_start_Click(object sender, EventArgs e)
{
int width = 640; //錄制視頻的寬度
int height = 480; //錄制視頻的高度
int fps = 9;
//創(chuàng)建一個視頻文件
String video_format = this.comboBox_videoecode.Text.Trim(); //獲取選中的視頻編碼
if (this.videoSource.IsRunning && this.videoSourcePlayer.IsRunning)
{
if (-1 != video_format.IndexOf("MPEG"))
{
writer.Open("test.avi", width, height, fps, VideoCodec.MPEG4);
}
else if (-1 != video_format.IndexOf("WMV"))
{
writer.Open("test.wmv", width, height, fps, VideoCodec.WMV1);
}
else
{
writer.Open("test.mkv", width, height, fps, VideoCodec.Default);
}
}
else
MessageBox.Show("沒有視頻源輸入,無法錄制視頻。", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
timer_count.Enabled = true;//是否執(zhí)行System.Timers.Timer.Elapsed事件;
this.label5.Visible = true;
this.label5.Text = "REC";
this.is_record_video = true;
}
//停止錄制視頻響應(yīng)函數(shù)
private void button_stop_Click(object sender, EventArgs e)
{
this.label5.Visible = false;
this.is_record_video = false;
this.writer.Close();
this.timer_count.Enabled = false;
tick_num = 0;
}
//暫停按鈕響應(yīng)函數(shù)
private void button3_Click(object sender, EventArgs e)
{
if (this.button3.Text.Trim() == "暫停錄像")
{
this.is_record_video = false;
this.label5.Visible = false;
this.button3.Text = "恢復錄像";
timer_count.Enabled = false; //暫停計時
return;
}
if (this.button3.Text.Trim() == "恢復錄像")
{
this.is_record_video = true;
timer_count.Enabled = true; //恢復計時
this.label5.Visible = true;
this.button3.Text = "暫停錄像";
}
}
//計時器響應(yīng)函數(shù)
public void tick_count(object source, System.Timers.ElapsedEventArgs e)
{
tick_num++;
int temp = tick_num;
int sec = temp % 60;
int min = temp / 60;
if (60 == min)
{
min = 0;
min++;
}
int hour = min / 60;
String tick = hour.ToString() + ":" + min.ToString() + ":" + sec.ToString();
this.label4.Text = tick;
}
}
}
3. 結(jié)果
4. 錯誤和注意事項
1. 在使用AForge這個軟件過程中需要的不僅僅是將Release文件夾下對應(yīng)的lib添加到項目的引用中,在進行視頻壓縮編碼的時候需要將External文件夾下的相關(guān)lib添加到程序運行的Debug目錄下
2. 在使用的時候會遇到這個錯誤“混合模式程序集是針對“v2.0.50727”版的運行時生成的,在沒有配置其他信息的情況”的錯誤。這是因為.net框架不兼容的問題,AForge使用的比較老好像是2.0的。。。-_-||。所以需要在App.config對其進行配置,使其對前版本的兼容,配置如下(這是我添加的配置):
<startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> <supportedRuntime version="v2.0.50727"/> </startup>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:Winform控件Picture實現(xiàn)圖片拖拽顯示效果
欄 目:C#教程
下一篇:c#生成自定義圖片方法代碼實例
本文標題:基于AForge實現(xiàn)C#攝像頭視頻錄制功能
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5053.html
您可能感興趣的文章
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠程關(guān)閉計算機或重啟計算機的方法
- 01-10C#自定義簽名章實現(xiàn)方法
- 01-10C#文件斷點續(xù)傳實現(xiàn)方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法


