diff --git a/ICS/FaceCaptureForm.Designer.cs b/ICS/FaceCaptureForm.Designer.cs index 8663f09..8ed6b23 100644 --- a/ICS/FaceCaptureForm.Designer.cs +++ b/ICS/FaceCaptureForm.Designer.cs @@ -29,19 +29,18 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FaceCaptureForm)); - this.videoPlayer = new AForge.Controls.PictureBox(); - ((System.ComponentModel.ISupportInitialize)(this.videoPlayer)).BeginInit(); + this.videoPlayer = new AForge.Controls.VideoSourcePlayer(); this.SuspendLayout(); // // videoPlayer // - this.videoPlayer.Image = null; + this.videoPlayer.BorderColor = System.Drawing.SystemColors.Control; this.videoPlayer.Location = new System.Drawing.Point(0, 0); this.videoPlayer.Margin = new System.Windows.Forms.Padding(0); this.videoPlayer.Name = "videoPlayer"; this.videoPlayer.Size = new System.Drawing.Size(100, 100); - this.videoPlayer.TabIndex = 3; - this.videoPlayer.TabStop = false; + this.videoPlayer.TabIndex = 4; + this.videoPlayer.VideoSource = null; // // FaceCaptureForm // @@ -56,12 +55,12 @@ this.Name = "FaceCaptureForm"; this.ShowInTaskbar = false; this.Text = "拍照"; - ((System.ComponentModel.ISupportInitialize)(this.videoPlayer)).EndInit(); + this.TopMost = true; this.ResumeLayout(false); } #endregion - private AForge.Controls.PictureBox videoPlayer; + private AForge.Controls.VideoSourcePlayer videoPlayer; } } \ No newline at end of file diff --git a/ICS/FaceCaptureForm.Designer.cs b/ICS/FaceCaptureForm.Designer.cs index 8663f09..8ed6b23 100644 --- a/ICS/FaceCaptureForm.Designer.cs +++ b/ICS/FaceCaptureForm.Designer.cs @@ -29,19 +29,18 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FaceCaptureForm)); - this.videoPlayer = new AForge.Controls.PictureBox(); - ((System.ComponentModel.ISupportInitialize)(this.videoPlayer)).BeginInit(); + this.videoPlayer = new AForge.Controls.VideoSourcePlayer(); this.SuspendLayout(); // // videoPlayer // - this.videoPlayer.Image = null; + this.videoPlayer.BorderColor = System.Drawing.SystemColors.Control; this.videoPlayer.Location = new System.Drawing.Point(0, 0); this.videoPlayer.Margin = new System.Windows.Forms.Padding(0); this.videoPlayer.Name = "videoPlayer"; this.videoPlayer.Size = new System.Drawing.Size(100, 100); - this.videoPlayer.TabIndex = 3; - this.videoPlayer.TabStop = false; + this.videoPlayer.TabIndex = 4; + this.videoPlayer.VideoSource = null; // // FaceCaptureForm // @@ -56,12 +55,12 @@ this.Name = "FaceCaptureForm"; this.ShowInTaskbar = false; this.Text = "拍照"; - ((System.ComponentModel.ISupportInitialize)(this.videoPlayer)).EndInit(); + this.TopMost = true; this.ResumeLayout(false); } #endregion - private AForge.Controls.PictureBox videoPlayer; + private AForge.Controls.VideoSourcePlayer videoPlayer; } } \ No newline at end of file diff --git a/ICS/FaceCaptureForm.cs b/ICS/FaceCaptureForm.cs index 957f7e9..adede09 100644 --- a/ICS/FaceCaptureForm.cs +++ b/ICS/FaceCaptureForm.cs @@ -8,9 +8,6 @@ { public partial class FaceCaptureForm : Form { - // 显示照相机拍摄的图片 - private Bitmap cameraFrame; - // 摄像机设备 private VideoCaptureDevice videoDevice; @@ -30,22 +27,14 @@ // 设置画面显示的大小 videoPlayer.Width = width; videoPlayer.Height = height; + videoPlayer.Left = 0; + videoPlayer.Top = 0; } // 注册相机拍摄事件 - public void RegistCameraNewFrame() + public void RegistCameraPlaySource() { - videoDevice.NewFrame += new NewFrameEventHandler(OnNewFrameEventCallback); - } - - // 相机拍摄事件处置函数 - // 获取图片帧并赋值给图片显示控件 - private void OnNewFrameEventCallback(object sender, NewFrameEventArgs eventArgs) - { - cameraFrame = (Bitmap)eventArgs.Frame.Clone(); - videoPlayer.Image = cameraFrame; - - GC.Collect(); + videoPlayer.VideoSource = videoDevice; } // 打开相机开始视频预览 @@ -63,7 +52,7 @@ // 截图操作,返回当前的图像帧 public Bitmap TakePhoto() { - return this.cameraFrame; + return videoPlayer.GetCurrentVideoFrame(); } } } diff --git a/ICS/FaceCaptureForm.Designer.cs b/ICS/FaceCaptureForm.Designer.cs index 8663f09..8ed6b23 100644 --- a/ICS/FaceCaptureForm.Designer.cs +++ b/ICS/FaceCaptureForm.Designer.cs @@ -29,19 +29,18 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FaceCaptureForm)); - this.videoPlayer = new AForge.Controls.PictureBox(); - ((System.ComponentModel.ISupportInitialize)(this.videoPlayer)).BeginInit(); + this.videoPlayer = new AForge.Controls.VideoSourcePlayer(); this.SuspendLayout(); // // videoPlayer // - this.videoPlayer.Image = null; + this.videoPlayer.BorderColor = System.Drawing.SystemColors.Control; this.videoPlayer.Location = new System.Drawing.Point(0, 0); this.videoPlayer.Margin = new System.Windows.Forms.Padding(0); this.videoPlayer.Name = "videoPlayer"; this.videoPlayer.Size = new System.Drawing.Size(100, 100); - this.videoPlayer.TabIndex = 3; - this.videoPlayer.TabStop = false; + this.videoPlayer.TabIndex = 4; + this.videoPlayer.VideoSource = null; // // FaceCaptureForm // @@ -56,12 +55,12 @@ this.Name = "FaceCaptureForm"; this.ShowInTaskbar = false; this.Text = "拍照"; - ((System.ComponentModel.ISupportInitialize)(this.videoPlayer)).EndInit(); + this.TopMost = true; this.ResumeLayout(false); } #endregion - private AForge.Controls.PictureBox videoPlayer; + private AForge.Controls.VideoSourcePlayer videoPlayer; } } \ No newline at end of file diff --git a/ICS/FaceCaptureForm.cs b/ICS/FaceCaptureForm.cs index 957f7e9..adede09 100644 --- a/ICS/FaceCaptureForm.cs +++ b/ICS/FaceCaptureForm.cs @@ -8,9 +8,6 @@ { public partial class FaceCaptureForm : Form { - // 显示照相机拍摄的图片 - private Bitmap cameraFrame; - // 摄像机设备 private VideoCaptureDevice videoDevice; @@ -30,22 +27,14 @@ // 设置画面显示的大小 videoPlayer.Width = width; videoPlayer.Height = height; + videoPlayer.Left = 0; + videoPlayer.Top = 0; } // 注册相机拍摄事件 - public void RegistCameraNewFrame() + public void RegistCameraPlaySource() { - videoDevice.NewFrame += new NewFrameEventHandler(OnNewFrameEventCallback); - } - - // 相机拍摄事件处置函数 - // 获取图片帧并赋值给图片显示控件 - private void OnNewFrameEventCallback(object sender, NewFrameEventArgs eventArgs) - { - cameraFrame = (Bitmap)eventArgs.Frame.Clone(); - videoPlayer.Image = cameraFrame; - - GC.Collect(); + videoPlayer.VideoSource = videoDevice; } // 打开相机开始视频预览 @@ -63,7 +52,7 @@ // 截图操作,返回当前的图像帧 public Bitmap TakePhoto() { - return this.cameraFrame; + return videoPlayer.GetCurrentVideoFrame(); } } } diff --git a/ICS/Form1.cs b/ICS/Form1.cs index 7203a3a..81cd949 100644 --- a/ICS/Form1.cs +++ b/ICS/Form1.cs @@ -137,7 +137,7 @@ faceForm.SetPicboxSize(faceForm.Width, faceForm.Height); faceForm.SetVideoDevice(videoDevice); // 注册相机拍摄事件 - faceForm.RegistCameraNewFrame(); + faceForm.RegistCameraPlaySource(); } else { @@ -335,9 +335,14 @@ { // 打开相机并显示视频 faceForm.StartCameraCapture(); + + // 指定窗体弹出的位置 + Point locationPoint = new Point(Screen.PrimaryScreen.WorkingArea.Width - faceForm.Size.Width, 10); + faceForm.StartPosition = FormStartPosition.Manual; + faceForm.Location = locationPoint; faceForm.Show(); - faceForm.WindowState = FormWindowState.Normal; - faceForm.Activate(); + //faceForm.WindowState = FormWindowState.Normal; + //faceForm.Activate(); log.Info("摄像头打开"); @@ -355,21 +360,38 @@ // 人脸拍照 else if (reValue.Contains("TakePhoto")) { - // 执行截图操作并保存到TEMP目录下 - Bitmap img = faceForm.TakePhoto(); - long now = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; - string path = AppDomain.CurrentDomain.BaseDirectory + "TEMP\\" + now + ".jpg"; - img.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); + try + { + // 执行截图操作并保存到TEMP目录下 + Bitmap img = faceForm.TakePhoto(); + if (null != img) + { + long now = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; + string path = AppDomain.CurrentDomain.BaseDirectory + "TEMP\\" + now + ".jpg"; + img.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); - log.Info("拍照成功,缓存路径:" + path); + log.Info("拍照成功,缓存路径:" + path); - // 对图像进行base64编码并返回,JPEG格式 - string sendMsg = "{\"success\":\"true\", \"image\":\"data:image/jpg;base64," + ToBase64(img) + "\"}"; - SendMsgToClient(reValue, sendMsg); + // 对图像进行base64编码并返回,JPEG格式 + string sendMsg = "{\"success\":\"true\", \"image\":\"data:image/jpg;base64," + ToBase64(img) + "\"}"; + SendMsgToClient(reValue, sendMsg); - // 隐藏相机画面窗体 - faceForm.Hide(); - faceForm.StopCameraCapture(); + // 隐藏相机画面窗体 + faceForm.Hide(); + faceForm.StopCameraCapture(); + } + else + { + // 返回为空,提示拍照失败 + string sendMsg = "{\"success\":\"false\"}"; + SendMsgToClient(reValue, sendMsg); + } + } catch (Exception ex) + { + string sendMsg = "{\"success\":\"false\"}"; + SendMsgToClient(reValue, sendMsg); + } + } } catch (Exception ex)