天天看點

C#使用AForge連接配接攝像頭并且拍照AForge連接配接攝像頭

AForge連接配接攝像頭

引入相關AForge庫。

C#使用AForge連接配接攝像頭并且拍照AForge連接配接攝像頭

在工具箱引入controls裡的控件。

繪圖

其中視訊框使用videoSourcePlayer控件。照片使用pictureBox控件。

C#使用AForge連接配接攝像頭并且拍照AForge連接配接攝像頭

編輯代碼

這裡有三個需要掌握的類或成員

public FilterInfoCollection(System.Guid category)
AForge.Video.DirectShow.FilterInfoCollection 的成員
摘要:
Initializes a new instance of the AForge.Video.DirectShow.FilterInfoCollection class.
參數:
category: Guid of DirectShow filter category. See AForge.Video.DirectShow.FilterCategory.
注解:
Build collection of filters' information objects for the specified filter category.
           
public VideoCaptureDevice(string deviceMoniker)
    AForge.Video.DirectShow.VideoCaptureDevice 的成員

摘要:
Initializes a new instance of the AForge.Video.DirectShow.VideoCaptureDevice class.
參數:
deviceMoniker: Moniker string of video capture device.
           
public AForge.Video.DirectShow.VideoCapabilities[] VideoCapabilities { get; }
    AForge.Video.DirectShow.VideoCaptureDevice 的成員

摘要:
Video capabilities of the device.
注解:
The property provides list of device's video capabilities.
It is recomended not to call this property immediately after AForge.Video.DirectShow.VideoCaptureDevice.Start method, since device may not start yet and provide its information. It is better to call the property before starting device or a bit after (but not immediately after).
VideoCapabilities[]這個屬性會傳回攝像頭支援哪些配置,并把這些配置存放在成員組裡。
           

接下來是常用的方法:

FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
可以擷取攝像頭清單
此時參數videoDevices.Count傳回裝置數。
FilterInfo device可以定義裝置變量來周遊videoDevices裝置清單。

           
VideoCaptureDevice Camera = new VideoCaptureDevice(videoDevices[0].MonikerString);
可以執行個體化裝置控制類,參數從上方的裝置清單選,這裡是第一個。
分辨率擷取:Camera.VideoCapabilities[0].FrameSize.Width
		   Camera.VideoCapabilities[0].FrameSize.Height
           
videoSourcePlayer有個屬性VideoSource是用來選擇裝置的。
可以先選擇好用哪個裝置,再設定相關屬性,最後将Camera(本例)指派給VideoSource。
Camera.start();開啟裝置。
Camera.Stop();斷開裝置
最後VideoSource=NULL;
           
Bitmap img = videoSourcePlayer1.GetCurrentVideoFrame();
可以拍照。
PictureBox.Image可以擷取或設定所顯示的圖像。
pictureBox1.Image = img;在PictureBox控件内顯示照片。
           

繼續閱讀