-
Notifications
You must be signed in to change notification settings - Fork 7
/
CaptureForm.cs
215 lines (169 loc) · 6.64 KB
/
CaptureForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using Accord.Imaging.Filters;
using Accord.Vision.Detection;
using Accord.Vision.Detection.Cascades;
using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace StudentPhotoCollection
{
// 目前没有使用此窗体
public partial class CaptureForm : Form
{
private VideoCaptureDevice videoDevice;
private Bitmap currentPicture;
private HaarObjectDetector detector;
/*
public CaptureForm()
{
InitializeComponent();
}
*/
public CaptureForm(VideoCaptureDevice videoDevice)
{
InitializeComponent();
this.videoDevice = videoDevice;
}
public CaptureForm(VideoCaptureDevice videoDevice, int width, int height)
{
InitializeComponent();
this.videoDevice = videoDevice;
// 设置窗体大小
this.Width = width;
this.Height = height;
}
public CaptureForm(VideoCaptureDevice videoDevice, int width, int height, string formName)
{
InitializeComponent();
this.videoDevice = videoDevice;
// 设置窗体标题
this.Text = formName;
//这个区域不包括任务栏的
Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);
//这个区域包括任务栏,就是屏幕显示的物理范围
//Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
int screenWidth = ScreenArea.Width; //屏幕宽度
int screenHeight = ScreenArea.Height; //屏幕高度
if (height > screenHeight)
{
height = screenHeight;
}
if(width > screenWidth / 2)
{
width = screenWidth / 2;
}
// 设置窗体大小
this.Width = width;
this.Height = height;
// 指定窗体显示在左上角
this.Location = new System.Drawing.Point(0, 0);
}
private void CaptureForm_Load(object sender, EventArgs e)
{
// FORM初始化
// 初始化videoSourcePlayer长宽
//this.videoSourcePlayer.SetBounds(0, 0, this.width, this.height);
// 拍照图片圆角
//setPictureBoxRegion(this.pictureBox_shoot);
// 打开摄像头
if (this.videoDevice != null)
{
this.videoSourcePlayer.VideoSource = this.videoDevice;
this.videoSourcePlayer.Start();
//AForge控件中的NewFrame事件获取要显示的每一帧的图像,做人脸标记处理
this.videoSourcePlayer.NewFrame += VideoSourcePlayer_NewFrame;
}
}
private void CaptureForm_FormClosing(object sender, FormClosingEventArgs e)
{
// FORM关闭
DisConnect();
}
private void CaptureForm_Resize(object sender, EventArgs e)
{
// 设置拍照按钮图片始终左右居中
int x = (int)(0.5 * (this.Width - this.pictureBox_shoot.Width));
int y = (int)(0.9 * (this.Height - this.pictureBox_shoot.Height));
//int y = this.pictureBox_shoot.Location.Y;
this.pictureBox_shoot.Location = new System.Drawing.Point(x, y);
}
private void setPictureBoxRegion(PictureBox pictureBox)
{
// 拍照图片圆角,圆角有锯齿
try
{
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(pictureBox.ClientRectangle);
Region region = new Region(gp);
pictureBox.Region = region;
gp.Dispose();
region.Dispose();
}
catch (Exception)
{
}
}
private void PictureBox_shoot_Click(object sender, EventArgs e)
{
//拍照
Bitmap img = currentPicture ?? videoSourcePlayer.GetCurrentVideoFrame();
//父窗体
MainForm mainForm = (MainForm)this.Owner;
//设置并保存照片
mainForm.SaveShootPhoto(img);
}
private void VideoSourcePlayer_NewFrame(object sender, ref Bitmap image)
{
//AForge控件中的NewFrame回调事件获取要显示的每一帧的图像
//人脸检测并标记
image = FacePicDetect(image); //必须将返回值重新赋给image,否则摄像界面不会回显人脸标记框
}
private Bitmap FacePicDetect(Bitmap bitmap)
{
lock (this)
{
//留存原始的照片,如果正在拍照,则保存此未添加人脸识别框的照片
currentPicture = (Bitmap)bitmap.Clone();
if (detector == null)
{
//先实例化用于检测人脸的对象detector
detector = new HaarObjectDetector(new FaceHaarCascade(), 100)
{
SearchMode = ObjectDetectorSearchMode.Single, //搜索模式
ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller, //缩放模式
ScalingFactor = 1.5f, //在搜索期间重新缩放搜索窗口时要使用的重新缩放因子
UseParallelProcessing = true
};//面部级联对象 + 搜索对象时使用的最小窗口大小
}
// 开始对检测区域进行检测并返回结果数组
Rectangle[] regions = detector.ProcessFrame(bitmap);
if (regions != null && regions.Length > 0)
{
//人脸标记
RectanglesMarker marker = new RectanglesMarker(regions, Color.Orange);
regions = null;
return marker.Apply(bitmap);
}
regions = null;
return bitmap;
}
}
private void DisConnect()
{
//窗体关闭前设置
if (videoSourcePlayer.VideoSource != null)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
videoSourcePlayer.VideoSource = null;
}
detector = null;
currentPicture = null;
videoDevice = null;
// 更新组件状态
MainForm mainForm = (MainForm)this.Owner;
mainForm.EnableControlStatus(true);
}
}
}