1.這個抓拍為靜默抓拍,不展示抓拍畫面,直接拍照片存到本地
2.Nuget引用AForge.Controls.dll
3.CameraHelper.cs
public static class CameraHelper { private static FilterInfoCollection _cameraDevices; private static VideoCaptureDevice div = null; private static VideoSourcePlayer sourcePlayer = new VideoSourcePlayer(); private static bool _isDisplay = false; //指示_isDisplay設置為true后,是否設置了其他的sourcePlayer,若未設置則_isDisplay重設為false private static bool isSet = false; /// <summary> /// 獲取或設置攝像頭設備,無設備為null /// </summary> public static FilterInfoCollection CameraDevices { get { return _cameraDevices; } set { _cameraDevices = value; } } /// <summary> /// 指示是否顯示攝像頭視頻畫面 /// 默認false /// </summary> public static bool IsDisplay { get { return _isDisplay; } set { _isDisplay = value; } } /// <summary> /// 獲取或設置VideoSourcePlayer控件, /// 只有當IsDisplay設置為true時,該屬性才可以設置成功 /// </summary> public static VideoSourcePlayer SourcePlayer { get { return sourcePlayer; } set { if (_isDisplay) { sourcePlayer = value; isSet = true; } } } /// <summary> /// 更新攝像頭設備信息 /// </summary> public static void UpdateCameraDevices() { _cameraDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); } /// <summary> /// 設置使用的攝像頭設備 /// </summary> /// <param name="index">設備在CameraDevices中的索引</param> /// <returns><see cref="bool"/></returns> public static bool SetCameraDevice(int index) { if (!isSet) _isDisplay = false; //無設備,返回false if (_cameraDevices.Count <= 0 || index < 0) return false; if (index > _cameraDevices.Count - 1) return false; // 設定初始視頻設備 div = new VideoCaptureDevice(_cameraDevices[index].MonikerString); sourcePlayer.VideoSource = div; div.Start(); sourcePlayer.Start(); return true; } /// <summary> /// 截取一幀圖像並保存 /// </summary> /// <param name="filePath">圖像保存路徑</param> /// <param name="fileName">保存的圖像文件名</param> public static void CaptureImage() { ////連接攝像頭 //CameraHelper.UpdateCameraDevices(); //if (CameraHelper.CameraDevices.Count > 0) //{ // CameraHelper.SetCameraDevice(0); //} if (sourcePlayer.VideoSource == null) return; string strDir = AppDomain.CurrentDomain.BaseDirectory + @"抓拍人臉_WMS\" + @"\" + DateTime.Now.ToString("yyyyMMdd"); if (!Directory.Exists(strDir)) { Directory.CreateDirectory(strDir); } try { //sourcePlayer.Start(); Image bitmap = sourcePlayer.GetCurrentVideoFrame(); if (bitmap != null) { //添加水印 using (var g = Graphics.FromImage(bitmap)) using (var brush = new SolidBrush(Color.White)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //var sizeF = g.MeasureString(text, font); g.ResetTransform(); //g.TranslateTransform(p.X, p.Y); //g.RotateTransform(44); Font drawFont = new Font("Arial", 12); float x = 20.0F; float y = 20.0F; // Set format of string. StringFormat drawFormat = new StringFormat(); drawFormat.FormatFlags = StringFormatFlags.DisplayFormatControl; g.DrawString(DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss"), drawFont, brush, x, y, drawFormat); } } string imgname = DateTime.Now.ToString("yyyyMMddHHmm") + Global.Carnum + ".jpg"; string jpgPath = strDir + @"\" + imgname; Global.DriverjpgPath = jpgPath; bitmap.Save(jpgPath); bitmap.Dispose(); //sourcePlayer.Stop(); //CloseDevice(); } catch (Exception e) { MessageBox.Show(e.ToString()); Global.Notice = "拍攝設備故障,請聯系工作人員:"+Global.Phone; SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(); if (Global.Times != null && Global.Times != "") { for (int i = 0; i < int.Parse(Global.Times); i++) { speechSynthesizer.Speak(Global.Notice); } } } } /// <summary> /// 關閉攝像頭設備 /// </summary> public static void CloseDevice() { if (div != null && div.IsRunning) { sourcePlayer.Stop(); div.SignalToStop(); div = null; _cameraDevices = null; } } }
4.調用拍照類抓拍
//連接攝像頭
CameraHelper.UpdateCameraDevices();
if (CameraHelper.CameraDevices.Count > 0)
{
CameraHelper.SetCameraDevice(0);
}
//抓拍人臉
CameraHelper.CaptureImage();