Android----自定义人脸框

/**
 * 自定义虹软人脸识别框/人脸抓拍框/人脸追踪框
 * 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);
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

土狗的想法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值