Browse Source

显示人脸框

xulh 7 months ago
parent
commit
18b6654868

+ 27 - 2
app/src/main/java/com/hh/bdface/ui/MainActivity.java

@@ -877,6 +877,7 @@ public class MainActivity extends BaseActivity implements View.OnClickListener,
         ConfigData.curTaskStatus = TASK_STATUS_INIT;
         SingleBaseConfig.getBaseConfig().setFeatureCheckMode(4);
 
+        isShowFrame = false;
 
     }
 
@@ -1324,13 +1325,22 @@ public class MainActivity extends BaseActivity implements View.OnClickListener,
     }
 
 
+    // 是否显示人脸框
+    boolean isShowFrame;
+
+
     public void showFrame(LivenessModel livenessModel) {
+
+
         if (livenessModel == null) {
             return;
         }
+
         camera_textureview_baidu.onGlDraw(livenessModel.getTrackFaceInfo(),
                 livenessModel.getBdFaceImageInstance(),
-                FaceOnDrawTexturViewUtil.drawFaceColor(score > FaceUtils.getInstance().getThreshold()));
+                FaceOnDrawTexturViewUtil.drawFaceColor(score > FaceUtils.getInstance().getThreshold()),
+                isShowFrame
+        );
     }
 
     private float score = 0;
@@ -1386,7 +1396,7 @@ public class MainActivity extends BaseActivity implements View.OnClickListener,
                                 public void onFaceDetectDarwCallback(LivenessModel livenessModel) {
 
                                     // 人脸框显示
-//                                    showFrame(livenessModel);
+                                    showFrame(livenessModel);
                                 }
                             });
 
@@ -1583,6 +1593,11 @@ public class MainActivity extends BaseActivity implements View.OnClickListener,
                                         }
 
 
+
+                                        // 关闭人脸框
+                                        isShowFrame = false;
+                                        showFrame(model);
+
                                         // 继电器--begin
                                         deviceUI.openElectricRelay(mActivity);
                                         AppLogUtils.e(
@@ -1664,6 +1679,9 @@ public class MainActivity extends BaseActivity implements View.OnClickListener,
                                         mPushEnterExitRecordPresenter.requestPushEnterExitRecord(pushbean);
 
 
+
+
+
                                     } else {
                                         // 人证核验未通过
                                         AppLogUtils.e("人证核验未通过");
@@ -1698,6 +1716,10 @@ public class MainActivity extends BaseActivity implements View.OnClickListener,
                                             GlideUtils.loadImageView(mActivity, mUserInfoResultBean.getAvatar(), iv_user);
                                         }
 
+                                        // 关闭人脸框
+                                        isShowFrame = false;
+                                        showFrame(model);
+
                                         // 移除刷脸计时
                                         mHandler.removeCallbacks(runnableScanFaceTime);
 
@@ -1887,6 +1909,9 @@ public class MainActivity extends BaseActivity implements View.OnClickListener,
         UIUtils.setText(tv_user_idcardNum, StringUtils.getDesensitization(resultBean.getIdNumber()));
         GlideUtils.loadImageView(mActivity, resultBean.getAvatar(), iv_user);
 
+        // 需要显示人脸框
+        isShowFrame = true;
+
 
         ThreadPoolManager.getInstance().executeIOTask(new Runnable() {
             @Override

+ 51 - 6
datalibrary/src/main/java/com/example/datalibrary/gl/view/GlMantleSurfacView.java

@@ -12,6 +12,7 @@ import android.os.Build;
 import android.os.Handler;
 import android.os.Looper;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.TextureView;
 import android.widget.RelativeLayout;
 
@@ -22,6 +23,10 @@ import com.example.datalibrary.utils.FaceOnDrawTexturViewUtil;
 import com.example.datalibrary.utils.ImageUtils;
 
 public class GlMantleSurfacView extends RelativeLayout {
+
+
+    String TAG = "GlMantleSurfacView";
+
     private boolean isDraw = false;
 
     private int drawLength = 200;
@@ -80,7 +85,7 @@ public class GlMantleSurfacView extends RelativeLayout {
             glFaceSurfaceView.onGlDraw();
         }
     }
-    public void onGlDraw(FaceInfo[] faceInfos , BDFaceImageInstance image , FaceColor colors ) {
+    public void onGlDraw(FaceInfo[] faceInfos , BDFaceImageInstance image , FaceColor colors) {
         if (glFaceSurfaceView != null){
             glFaceSurfaceView.onGlDraw(faceInfos , image , colors.getColors() , rgbRevert , isDraw || mIsRegister);
         } else {
@@ -104,11 +109,51 @@ public class GlMantleSurfacView extends RelativeLayout {
             // 检测图片的坐标和显示的坐标不一样,需要转换。
             FaceOnDrawTexturViewUtil.mapFromOriginalRect(rectF,
                     faceTexture, image);
-            // 人脸框颜色
-            paint.setColor(colors.getColor());
-            // 绘制人脸框
-            FaceOnDrawTexturViewUtil.drawRect(canvas,
-                    rectF, paint, 5f, 50f , 25f);
+
+                // 人脸框颜色
+                paint.setColor(colors.getColor());
+                // 绘制人脸框
+                FaceOnDrawTexturViewUtil.drawRect(canvas,
+                        rectF, paint, 5f, 50f , 25f);
+
+
+            // 清空canvas
+            faceTexture.unlockCanvasAndPost(canvas);
+        }
+    }
+
+
+    public void onGlDraw(FaceInfo[] faceInfos , BDFaceImageInstance image , FaceColor colors ,boolean isShowFrame) {
+        if (glFaceSurfaceView != null){
+            glFaceSurfaceView.onGlDraw(faceInfos , image , colors.getColors() , rgbRevert , isDraw || mIsRegister);
+        } else {
+            Canvas canvas = faceTexture.lockCanvas();
+            if (canvas == null) {
+                faceTexture.unlockCanvasAndPost(canvas);
+                return;
+            };
+            if (faceInfos == null || faceInfos.length == 0 || !isShowFrame) {
+                // 清空canvas
+                canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
+                faceTexture.unlockCanvasAndPost(canvas);
+                return;
+            }
+            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
+            FaceInfo faceInfo = faceInfos[0];
+
+            RectF rectF = new RectF();
+            rectF.set(FaceOnDrawTexturViewUtil.getFaceRectTwo(faceInfo));
+            // 检测图片的坐标和显示的坐标不一样,需要转换。
+            FaceOnDrawTexturViewUtil.mapFromOriginalRect(rectF,
+                    faceTexture, image);
+
+                // 人脸框颜色
+                paint.setColor(colors.getColor());
+                // 绘制人脸框
+                FaceOnDrawTexturViewUtil.drawRect(canvas,
+                        rectF, paint, 5f, 50f , 25f);
+
+
             // 清空canvas
             faceTexture.unlockCanvasAndPost(canvas);
         }