1. 程式人生 > 其它 >無人地磅自助機開發總結(三)呼叫電腦本地攝像頭拍照抓拍,新增水印

無人地磅自助機開發總結(三)呼叫電腦本地攝像頭拍照抓拍,新增水印

1.這個抓拍為靜默抓拍,不展示抓拍畫面,直接拍照片存到本地

2.Nuget引用AForge.Controls.dll

3.private static VideoSourcePlayer sourcePlayer = new VideoSourcePlayer();

4.

   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(); } catch (Exception e) { MessageBox.Show(e.ToString()); } }