圖1-1是程序的主界面:
圖1-1
操作攝像頭以及實現拍照功能整個過程主要都是通過一個第三方的組件實現的,名字叫做 AForge ,是國外的組件,所以打開起來有點慢,但是要有耐心啊,目前已經更新到2.2.5版本了。如果不願意從官網上下載,文章末尾處也給出了相應的下載地址,需要的話直接拿來用就ok了。程序也很簡單,一個WinForm頁面,添加一下對Aforge的引用就可以了,但是這個過程中會引用一些其他的dll,有些不是太常用,所以在這里對引用的dll也做了截圖(圖1-2),在自己做的過程中參照一下即可。
圖1-2
上圖1-1中間部分是用來實時顯示攝像頭開啟后獲取到的內容,是一個自定義控件。這里得說明一下,將AForge.Controls.dll拖拽到左側的工具箱區域,然后就出來自定義控件了。這里順便說明一下,我們平時自己開發的自定義控件也可以通過這種方式來給別人用。前台都准備好了之后我們來開始分析后台代碼。
整個的思路是先找到電腦上的攝像頭設備,然后選擇我們需要操作的設備,然后在拍照或者攝像。今天由於時間關系僅僅只是實現拍照,下次再做攝像的功能,實現之后也會一樣分享在這里的,希望有需要的朋友關注。
當Form加載的時候,我們監聽一下其Load事件,將檢測到的攝像頭設備添加到后邊的ComboBox中供用戶選擇,關鍵代碼如下:
private void Form1_Load(object sender, EventArgs e) { try { // 枚舉所有視頻輸入設備 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count == 0) throw new ApplicationException(); foreach (FilterInfo device in videoDevices) { tscbxCameras.Items.Add(device.Name); } tscbxCameras.SelectedIndex = 0; } catch (ApplicationException) { tscbxCameras.Items.Add("No local capture devices"); videoDevices = null; } }
當用戶選擇某一攝像頭設備再點擊連接的時候,我們打開攝像頭,並對其進行初始化,關鍵代碼:
//連接攝像頭 private void CameraConn() { VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString); videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240); videoSource.DesiredFrameRate = 1; videoSourcePlayer.VideoSource = videoSource; videoSourcePlayer.Start(); }
當用戶關閉點擊關閉攝像頭的時候,我們做關閉的處理,代碼:
//關閉攝像頭 private void btnClose_Click(object sender, EventArgs e) { videoSourcePlayer.SignalToStop(); videoSourcePlayer.WaitForStop(); }
當用戶點擊拍照的時候,我們獲取攝像頭當前的畫面,並保存到設定的路徑下,然后關閉當前窗口。關鍵代碼:
//拍照 private void Photograph_Click(object sender, EventArgs e) { try { if (videoSourcePlayer.IsRunning) { BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); PngBitmapEncoder pE = new PngBitmapEncoder(); pE.Frames.Add(BitmapFrame.Create(bitmapSource)); string picName = GetImagePath() + "\\" + "xiaosy" + ".jpg"; if (File.Exists(picName)) { File.Delete(picName); } using (Stream stream = File.Create(picName)) { pE.Save(stream); } //拍照完成后關攝像頭並刷新同時關窗體 if (videoSourcePlayer != null && videoSourcePlayer.IsRunning) { videoSourcePlayer.SignalToStop(); videoSourcePlayer.WaitForStop(); } this.Close(); } } catch (Exception ex) { MessageBox.Show("攝像頭異常:" + ex.Message); } } private string GetImagePath() { string personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) + Path.DirectorySeparatorChar.ToString() + "PersonImg"; if (!Directory.Exists(personImgPath)) { Directory.CreateDirectory(personImgPath); } return personImgPath; }
關閉之后,在bin目錄下的PersonImg中即可找到保存的圖片,當然,在程序中把該圖片展示出來效果會更好,但是由於時間關系,就不添加了。需要的朋友可以自己實現,有問題的歡迎一起交流。
至此,這樣一個簡單的拍照功能就完成了,在這里,附上源碼下載地址。
源碼下載:http://download.csdn.net/detail/jrlxsy/6927833
原文連接:https://www.cnblogs.com/xsyblogs/p/3551986.html?tdsourcetag=s_pctim_aiomsg