/** * 绘制人脸框。 * */ 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); }
void SetupUserInterface() { activity = this.Context as Activity; view = activity.LayoutInflater.Inflate(Resource.Layout.CameraLayout, this, false); cameraType = CameraFacing.Back; textureView = view.FindViewById <TextureView>(Resource.Id.textureView); textureView.SurfaceTextureListener = this; OCR_textView = view.FindViewById <TextView>(Resource.Id.myOCR_TextView); OCR_Rectangle = view.FindViewById <ImageView>(Resource.Id.btn_capture_photo); OCR_Top_Left = view.FindViewById <ImageView>(Resource.Id.Top_Left_photo); OCR_Top_Right = view.FindViewById <ImageView>(Resource.Id.Top_Right_photo); OCR_Bottom_Left = view.FindViewById <ImageView>(Resource.Id.Bottom_Left_photo); OCR_Bottom_Right = view.FindViewById <ImageView>(Resource.Id.Bottom_Right_photo); Canvas canvas = textureView.LockCanvas(); Paint paint; paint = new Paint(); paint.Color = Android.Graphics.Color.Red; paint.SetStyle(Paint.Style.Stroke); paint.StrokeWidth = 2f; Rect r = new Rect((int)100, (int)100, (int)200, (int)200); // canvas.DrawRect(r, paint); var absolutePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath; if (!Directory.Exists(absolutePath + "/tessdata")) { Directory.CreateDirectory(absolutePath + "/tessdata"); } if (!File.Exists(absolutePath + "/tessdata/eng.traineddata")) { string destPath = absolutePath + "/tessdata/eng.traineddata"; using (var source = activity.Assets.Open("eng.traineddata")) using (var dest = File.OpenWrite(destPath)) source.CopyTo(dest); } }
/** * 绘制人脸框。 */ 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); } }