a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

基于AForge實現C#攝像頭視頻錄制功能

2019-10-29 19:58:36
字體:
來源:轉載
供稿:網友

本文為大家分享了AForge實現C#攝像頭視頻錄制功能的具體方法,供大家參考,具體內容如下

1. 概述

最近由于興趣學習了下在C#上使用AForge錄制攝像頭視頻并壓縮編碼。總體上來說這個第三方.net視覺開發庫還是比較穩定的(AForge lib下載、離線幫助文檔下載)。但是由于這個第三方庫維護不怎么樣,導致會出現不兼容的問題。這里將這些與大家分享,希望對您有幫助。

在使用AForge第三方庫錄制本地視頻所要使用到的類主要有這幾個:FilterInfoCollection、VideoCaptureDevice、VideoSourcePlayer、VideoFileWriter。下面我就簡單的介紹一下這幾個類涉及到的方法等。

FilterInfoCollection:

該類主要是用于攝像頭視頻輸入設備列表檢測的。是繼承自C#中的System.Collections.CollectionBase類。

AForge,C#,攝像頭,視頻錄制

這是離線幫助文檔上對這個類的調用方式:

AForge,C#,攝像頭,視頻錄制

其中構造函數傳遞進去的參數是需要采集的信號的類型,他還有其他的輸入類型(如聲音等):

AForge,C#,攝像頭,視頻錄制

VideoCaptureDevice:

這是該類中的一些成員函數和變量

AForge,C#,攝像頭,視頻錄制AForge,C#,攝像頭,視頻錄制

AForge,C#,攝像頭,視頻錄制

這個類便是要選擇了視頻輸入的設備了,他的構造函數是

AForge,C#,攝像頭,視頻錄制

在實際的使用過程中可能會存在多個設備的情況,便可以通過第二個參數進行輸入設備的指定。初始化是這樣的

AForge,C#,攝像頭,視頻錄制

在本例子的實際使用過程中對上面該類事件NewFrame函數進行了響應,然后提取出當前幀。

VideoSourcePlayer:

該類是AForge.Control中的類,是控件中調用的,這里將它添加進來是為了作為拍照功能使用的,這里就不做介紹了。

VideoFileWriter:

該類是視頻寫操作類,主要實現視頻文件的壓縮和寫入到文件中。

本例子中先使用VideoFileWriter.Open()函數設定錄制視頻的高度、寬度、幀率、編碼類型。

AForge,C#,攝像頭,視頻錄制

這是該第三方類庫支持的視頻編碼格式

AForge,C#,攝像頭,視頻錄制

然后使用下面這個函數就可以將當前幀寫入到視頻文件中了。

AForge,C#,攝像頭,視頻錄制

2. 實現

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 AForgeusing 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();    }     //關閉窗口響應函數    private void button2_Click(object sender, EventArgs e)    {      if (this.writer.IsOpen)      {        MessageBox.Show("視頻流還沒有寫完,請點擊結束錄制。", "錯誤", 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; //攝像頭設備    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;     //窗體初始化函數    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();       //設置視頻編碼格式      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;       //設置視頻來源      try      {        // 枚舉所有視頻輸入設備        videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);         if (videoDevices.Count == 0)          throw new ApplicationException();  //沒有找到攝像頭設備         foreach (FilterInfo device in videoDevices)        {          this.comboBox_camera.Items.Add(device.Name);        }        //this.comboBox_camera.SelectedIndex = 0;  //注釋掉,選擇攝像頭來源的時候才會才會觸發顯示攝像頭信息      }      catch (ApplicationException)      {        videoDevices = null;        MessageBox.Show("沒有找到攝像頭設備", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);      }       //秒表      this.timer_count = new System.Timers.Timer();  //實例化Timer類,設置間隔時間為10000毫秒;      this.timer_count.Elapsed += new System.Timers.ElapsedEventHandler(tick_count);  //到達時間的時候執行事件;      this.timer_count.AutoReset = true;  //設置是執行一次(false)還是一直執行(true);      this.timer_count.Interval = 1000;    }     //視頻源選擇下拉框選擇之后的響應函數    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;    //新幀的觸發函數    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);      }    }     //拍攝圖像按鈕響應函數    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);    }     //開始錄像按鈕響應函數    private void button_start_Click(object sender, EventArgs e)    {      int width = 640;  //錄制視頻的寬度      int height = 480;  //錄制視頻的高度      int fps = 9;       //創建一個視頻文件      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;//是否執行System.Timers.Timer.Elapsed事件;      this.label5.Visible = true;      this.label5.Text = "REC";      this.is_record_video = true;    }      //停止錄制視頻響應函數    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;    }     //暫停按鈕響應函數    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 = "暫停錄像";      }    }     //計時器響應函數    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. 結果

AForge,C#,攝像頭,視頻錄制

4. 錯誤和注意事項

1. 在使用AForge這個軟件過程中需要的不僅僅是將Release文件夾下對應的lib添加到項目的引用中,在進行視頻壓縮編碼的時候需要將External文件夾下的相關lib添加到程序運行的Debug目錄下

AForge,C#,攝像頭,視頻錄制

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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久99这里只有精品 | 欧美性久久 | 国产精品永久免费自在线观看 | 98久久久 | 毛片毛片毛片毛片毛片毛片毛片毛片毛片毛片 | 国产乱码精品1区2区3区 | 精品国产乱码久久久久久1区二区 | 亚洲精品一区二区三区蜜桃久 | 中文字幕av网 | 涩涩视频在线 | 日本福利一区二区 | 国内精品久久久久久中文字幕 | 人善交video另类hd国产片 | av黄色在线 | 日韩av免费在线观看 | 不卡一区二区三区四区 | 中文在线a在线 | 欧美日韩卡一卡二 | 精品国产欧美一区二区三区成人 | 色999视频 | 99精品视频一区 | 精品一区二区三区在线观看 | 一区二区免费在线观看 | 精品一区二区三区在线视频 | 精品久久久久久久久久 | 精品一区二区三区中文字幕 | 一区中文字幕 | 精品一二三区在线观看 | 日韩电影在线 | 一本大道综合伊人精品热热 | 亚洲国产精品久久久久秋霞不卡 | 亚洲精品99久久久久中文字幕 | 热久久这里只有精品 | 日韩免费视频一区二区 | 青草视频在线观看免费 | 日韩中文字幕在线 | 福利片在线观看 | 国产精品久久久久久久久久久久久久 | 亚洲精品美女久久久 | 女男羞羞视频网站免费 | 午夜影院免费 |