/// <summary>
/// aforge摄像框控件:捕捉识别绘制人脸框
/// </summary>
/// <param name="sender"></param>
/// <param name="image"></param>
///
private void camerSourcePlayer_NewFrame(object sender, ref Bitmap image)
{
//解决左右镜像相反问题
if (image != null)
{
//顺时针旋转90度 RotateFlipType.Rotate90FlipNone
//逆时针旋转90度 RotateFlipType.Rotate270FlipNone
//水平翻转 RotateFlipType.Rotate180FlipY
//垂直翻转 RotateFlipType.Rotate180FlipX
//RotateFlipType.RotateNoneFlipX 按X轴翻转
image.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
if (isFaceAlgOK == false)
{ return; }
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
ms.Close();
//输入算法 检测人脸 为检测出的人脸数
int ret = TcFace.face_get_pos_from_image(bytes, bytes.Length, detectFaces);
//检测出人脸
if (ret > 0)
{
//计算原始人脸框
int x = detectFaces[0];
int y = detectFaces[1];
int width = detectFaces[2] - detectFaces[0];
int height = detectFaces[3] - detectFaces[1];
//按比例扩展人脸前端显示范围
int x_front = x - (int)(width * 0.1);
int y_front = y - (int)(height * 0.15);
int width_front = width + (int)(width * 0.2);
int height_front = height + (int)(height * 0.3);
//按比例扩展人脸后台比对范围
int x_back = x - (int)(width * 0.3);
int y_back = y - (int)(height * 0.8);
int width_back = width + (int)(width * 0.6);
int height_back = height + (int)(height * 1.2);
检测到读卡后 截取人脸比对 开始一轮比对
if (isStartVerify == true)
{
isStartVerify = false; //在进行一轮的比对中 不在接收新的验证 状态将改为false
resultFace = CaptureImage(image, width_back, height_back, x_back, y_back);
currentFrame = (Bitmap)image.Clone();
verifyThread = new Thread(FaceVerify);
verifyThread.IsBackground = true;
verifyThread.Start();
}
//画人脸框
try
{
Graphics g = Graphics.FromImage(image);
Rectangle faceRect = new Rectangle(x_front, y_front, width_front, height_front);
DrawFaceRect(g, faceRect);
g.Dispose();
}
catch (Exception ex)
{
//当人脸靠近边框时会卡住,屏蔽该异常
}
}
//直接画人脸框
try
{
Graphics g = Graphics.FromImage(image);
g.DrawImage(bmpFaceLine, 0, 0, camerWidth, camerHeight);
g.Dispose();
g = null;
}
catch (Exception ex)
{
//当人脸靠近边框时会卡住,屏蔽该异常
}
}