/** * 绘制人脸框。 * */ private void showFrame(ImageFrame imageFrame, FaceInfo[] faceInfos) { Canvas canvas = textureView.LockCanvas(); if (canvas == null) { textureView.UnlockCanvasAndPost(canvas); return; } if (faceInfos == null || faceInfos.Length == 0) { // 清空canvas canvas.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureView.UnlockCanvasAndPost(canvas); return; } canvas.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); FaceInfo faceInfo = faceInfos[0]; rectF.Set(getFaceRect(faceInfo, imageFrame)); // 检测图片的坐标和显示的坐标不一样,需要转换。 previewView.mapFromOriginalRect(rectF); float yaw = Math.Abs(faceInfo.HeadPose[0]); float patch = Math.Abs(faceInfo.HeadPose[1]); float roll = Math.Abs(faceInfo.HeadPose[2]); if (yaw > 20 || patch > 20 || roll > 20) { // 不符合要求,绘制黄框 paint.Color = Color.Yellow; String text = "请正视屏幕"; float width = paint.MeasureText(text) + 50; float x = rectF.CenterX() - width / 2; paint.Color = Color.Red; paint.SetStyle(Paint.Style.Fill); canvas.DrawText(text, x + 25, rectF.Top - 20, paint); paint.Color = Color.Yellow; } else { // 符合检测要求,绘制绿框 paint.Color = Color.Green; } paint.SetStyle(Paint.Style.Stroke); // 绘制框 canvas.DrawRect(rectF, paint); textureView.UnlockCanvasAndPost(canvas); }
/** * 绘制人脸框。 */ private void showFrame2(LivenessModel model) { if (camemra1IsRgb) { Canvas canvas2 = textureViewOne.LockCanvas(); if (canvas2 == null) { textureViewOne.UnlockCanvasAndPost(canvas2); return; } if (model == null) { canvas2.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureViewOne.UnlockCanvasAndPost(canvas2); return; } FaceInfo[] faceInfos = model.getTrackFaceInfo(); ImageFrame imageFrame = model.getImageFrame(); if (faceInfos == null || faceInfos.Length == 0) { // 清空canvas canvas2.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureViewOne.UnlockCanvasAndPost(canvas2); return; } canvas2.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); FaceInfo faceInfo2 = faceInfos[0]; rectF.Set(getFaceRectTwo(faceInfo2, imageFrame)); // 检测图片的坐标和显示的坐标不一样,需要转换。 // mPreview[typeIndex].mapFromOriginalRect(rectF); float yaw2 = Java.Lang.Math.Abs(faceInfo2.HeadPose[0]); float patch2 = Java.Lang.Math.Abs(faceInfo2.HeadPose[1]); float roll2 = Java.Lang.Math.Abs(faceInfo2.HeadPose[2]); if (yaw2 > 20 || patch2 > 20 || roll2 > 20) { // 不符合要求,绘制黄框 paint.Color = (Color.Yellow); string text = "请正视屏幕"; float width = paint.MeasureText(text) + 50; float x = rectF.CenterX() - width / 2; paint.Color = (Color.Red); paint.SetStyle(Paint.Style.Fill); canvas2.DrawText(text, x + 25, rectF.Top - 20, paint); paint.Color = (Color.Yellow); } else { // 符合检测要求,绘制绿框 paint.Color = (Color.Green); } paint.SetStyle(Paint.Style.Stroke); // 绘制框 canvas2.DrawRect(rectF, paint); textureViewOne.UnlockCanvasAndPost(canvas2); Canvas canvas = textureView.LockCanvas(); if (canvas == null) { textureView.UnlockCanvasAndPost(canvas); return; } if (faceInfos == null || faceInfos.Length == 0) { // 清空canvas canvas.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureView.UnlockCanvasAndPost(canvas); return; } canvas.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureView.UnlockCanvasAndPost(canvas); } else { Canvas canvas = textureView.LockCanvas(); if (canvas == null) { textureView.UnlockCanvasAndPost(canvas); return; } if (model == null) { // 清空canvas canvas.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureView.UnlockCanvasAndPost(canvas); return; } FaceInfo[] faceInfos = model.getTrackFaceInfo(); ImageFrame imageFrame = model.getImageFrame(); if (faceInfos == null || faceInfos.Length == 0) { // 清空canvas canvas.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureView.UnlockCanvasAndPost(canvas); return; } canvas.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); FaceInfo faceInfo = faceInfos[0]; rectF.Set(getFaceRectTwo(faceInfo, imageFrame)); // 检测图片的坐标和显示的坐标不一样,需要转换。 // mPreview[typeIndex].mapFromOriginalRect(rectF); float yaw = Java.Lang.Math.Abs(faceInfo.HeadPose[0]); float patch = Java.Lang.Math.Abs(faceInfo.HeadPose[1]); float roll = Java.Lang.Math.Abs(faceInfo.HeadPose[2]); if (yaw > 20 || patch > 20 || roll > 20) { // 不符合要求,绘制黄框 paint.Color = (Color.Yellow); string text = "请正视屏幕"; float width = paint.MeasureText(text) + 50; float x = rectF.CenterX() - width / 2; paint.Color = (Color.Red); paint.SetStyle(Paint.Style.Fill); canvas.DrawText(text, x + 25, rectF.Top - 20, paint); paint.Color = (Color.Yellow); } else { // 符合检测要求,绘制绿框 paint.Color = (Color.Green); } paint.SetStyle(Paint.Style.Stroke); // 绘制框 canvas.DrawRect(rectF, paint); textureView.UnlockCanvasAndPost(canvas); Canvas canvas2 = textureViewOne.LockCanvas(); if (canvas2 == null) { textureViewOne.UnlockCanvasAndPost(canvas2); return; } if (faceInfos == null || faceInfos.Length == 0) { // 清空canvas canvas2.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureViewOne.UnlockCanvasAndPost(canvas2); return; } canvas2.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); textureViewOne.UnlockCanvasAndPost(canvas2); } }