/**
* 自定义虹软人脸识别框/人脸抓拍框/人脸追踪框
* Created by HDL on 2018/7/31.
*/
public class FaceRectView extends View {
private Rect rect;
private int screenWidth;
private int screenHeight;
public FaceRectView(Context context) {
this(context, null);
}
public FaceRectView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FaceRectView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
screenWidth = wm.getDefaultDisplay().getWidth();
screenHeight = wm.getDefaultDisplay().getHeight();
initPaint(context);
}
private void initPaint(Context context) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(3);
mPaint.setColor(context.getResources().getColor(R.color.color_face_rect));
}
private Paint mPaint;
/**
* 开始画矩形框
*
* @param rect1
*/
public void drawFaceRect(Rect rect1) {
this.rect = rect1;
//将屏幕人脸框转换为视频区域的人脸框
rect.left = rect.left * getWidth() / screenHeight+20;
rect.right = rect.right * getWidth() / screenHeight+40;
rect.top = rect.top * getHeight() / screenHeight+30;
rect.bottom = rect.bottom * getHeight() / screenHeight+75;
//在主线程发起绘制请求
postInvalidate();
}
public void clearRect() {
rect = null;
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (rect != null) {
/**
* 左上角的竖线
*/
canvas.drawLine(rect.left, rect.top, rect.left, rect.top + 20, mPaint);
/**
* 左上角的横线
*/
canvas.drawLine(rect.left, rect.top, rect.left + 20, rect.top, mPaint);
/**
* 右上角的竖线
*/
canvas.drawLine(rect.right, rect.top, rect.right - 20, rect.top, mPaint);
/**
* 右上角的横线
*/
canvas.drawLine(rect.right, rect.top, rect.right, rect.top + 20, mPaint);
/**
* 左下角的竖线
*/
canvas.drawLine(rect.left, rect.bottom, rect.left, rect.bottom - 20, mPaint);
/**
* 左下角的横线
*/
canvas.drawLine(rect.left, rect.bottom, rect.left + 20, rect.bottom, mPaint);
/**
* 右下角的竖线
*/
canvas.drawLine(rect.right, rect.bottom, rect.right, rect.bottom-20, mPaint);
/**
* 右下角的横线
*/
canvas.drawLine(rect.right, rect.bottom, rect.right-20, rect.bottom , mPaint);
}
}
}