C# 調用AForge類庫操作攝像頭


如有雷同,不勝榮幸,若轉載,請注明

最近做項目需要操作攝像頭,在網上百度了很多資料,很多都是C#調用window API 發送SendMessage,實現操作攝像頭,但是C#調用window API的時候因為驅動的問題,總是彈出視頻選擇對話框,讓人很是無語,看到大牛們有的截獲到了window消息,然后模擬點擊確定按鈕,這是在是不敢恭維啊,還有的大牛根據API原型重寫了,至於我是一只IT小小鳥了,然后在繼續百度,找到了一個AForge強大的C#類庫,最后終於搞定了,接下來將我拙劣的代碼部分貼出來,以便同行或者需要的朋友學習交流,

首先用到AForge類庫下載地址:http://www.aforgenet.com/

然后引用AForge,AForge.Controls(這個是控件,可以添加到工具箱中),AForge.Imaging,AForge.Video,AForge.Video.DirectShow;

然后直接上代碼

[csharp]  view plain copy
 
  1. private FilterInfoCollection videoDevices;  
  2.         private VideoCaptureDevice videoSource;  
  3.         public int selectedDeviceIndex = 0;  


 

下面是獲取設備

[csharp]  view plain copy
 
  1. public FilterInfoCollection GetDevices()  
  2.         {  
  3.             try  
  4.             {  
  5.                 //枚舉所有視頻輸入設備  
  6.                 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);  
  7.                 if (videoDevices.Count != 0)  
  8.                 {  
  9.                     LogClass.WriteFile("已找到視頻設備.");  
  10.                     return videoDevices;  
  11.                 }  
  12.                 else  
  13.                     return null;  
  14.             }  
  15.             catch (Exception ex)  
  16.             {  
  17.                 LogClass.WriteFile("error:沒有找到視頻設備!具體原因:" + ex.Message);  
  18.                 return null;  
  19.             }  
  20.         }  


選擇設備,然后連接攝像頭

[csharp]  view plain copy
 
  1. <p> /// <summary>  
  2.         /// 連接視頻攝像頭  
  3.         /// </summary>  
  4.         /// <param name="deviceIndex"></param>  
  5.         /// <param name="resolutionIndex"></param>  
  6.         /// <returns></returns>  
  7.         public VideoCaptureDevice VideoConnect(int deviceIndex = 0, int resolutionIndex = 0)  
  8.         {  
  9.             if (videoDevices.Count <= 0)  
  10.                 return null;  
  11.             selectedDeviceIndex = deviceIndex;  
  12.             videoSource = new VideoCaptureDevice(videoDevices[deviceIndex].MonikerString);</p><p>            return videoSource;  
  13.         }</p>  
[csharp]  view plain copy
 
  1. //抓圖,拍照,單幀  
  2.   
  3. public void GrabBitmap(string path)  
  4.         {  
  5.             if (videoSource == null)  
  6.                 return;  
  7.             g_Path = path;  
  8.             videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);  
  9.         }  


 

[csharp]  view plain copy
 
  1. void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)  
  2.         {  
  3.             Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();  
  4.             string fullPath = path + "temp\\";  
  5.             if (!Directory.Exists(fullPath))  
  6.                 Directory.CreateDirectory(fullPath);  
  7.             string img = fullPath + DateTime.Now.ToString("yyyyMMdd hhmmss") + ".bmp";  
  8.             bmp.Save(img);  
[csharp]  view plain copy
 
  1. //如果這里不寫這個,一會兒會不停的拍照,  
  2.             videoSource.NewFrame -= new NewFrameEventHandler(videoSource_NewFrame);  
  3.         }  


這樣就完成了操作攝像頭的工作

但是發現一個問題,如果要拍照得到的照片先要處理在保存,這里就有問題了,所以需要在界面前台中添加控件,醫用AForge.Controls,然后添加到工具箱,然后將VideoSourcePlayer控件拖到窗體中,想要得到單張圖像處理:

Bitmap bmp = videoSourcePlayer1.GetCurrentFrame();

這樣就可以拿來處理了,AForge類庫是非常的強大,這里只是冰山一角,文章不足之處還請大家多多指正,歡迎提出寶貴意見和建議。謝謝。。。

轉自:http://blog.csdn.net/chenhongwu666/article/details/40594365


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM