Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

android / dayu

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • dayu
  • ..
  • view
  • IDCardIndicator.java
Find file
BlameHistoryPermalink
  • 罗翻's avatar
    增加人脸识别 · 5b0d1c4f
    罗翻 committed 7 years ago
    5b0d1c4f
IDCardIndicator.java 11.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
package com.megvii.idcardlib.view;

//import com.megvii.idcard.sdk.IDCard.Card;
//import com.megvii.idcard.sdk.IDCard.Faculae;
//import com.megvii.idcard.sdk.IDCard.IDCardQuality;
//import com.megvii.idcard.sdk.IDCard.PointF;
//import com.megvii.idcard.sdk.IDCard.Shadow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.megvii.idcardlib.R;
import com.megvii.idcardquality.bean.IDCardAttr;

/**
 * Created by binghezhouke on 15-8-12.
 */
public class IDCardIndicator extends View {
    private Rect mShowRect = null;
    private Rect mDrawRect = null;
    private Paint mDrawPaint = null;
    private float IDCARD_RATIO = 856.0f / 540.0f;
    private float CONTENT_RATIO = 1f;
    private float SHOW_CONTENT_RATIO = CONTENT_RATIO * 13.0f / 16.0f;
    private Rect mTmpRect = null;
    private int backColor = 0x00000000;

    private void init() {
        mShowRect = new Rect();
        mDrawRect = new Rect();
        mTmpRect = new Rect();
        mDrawPaint = new Paint();
        mDrawPaint.setDither(true);
        mDrawPaint.setAntiAlias(true);
        mDrawPaint.setStrokeWidth(10);
        mDrawPaint.setStyle(Paint.Style.STROKE);
        mDrawPaint.setColor(0xff0000ff);
    }

    public void setBackColor(Activity activity, int backColor) {
        if (this.backColor != backColor) {
            this.backColor = backColor;
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    IDCardIndicator.this.invalidate();
                }
            });
        }
    }

    public IDCardIndicator(Context context) {
        super(context);
        init();
    }

    public IDCardIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public IDCardIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int centerX = width >> 1;
        int centerY = height >> 1;
        int content_width = 0;
        int content_height = 0;
        if (width / (float) (height) < IDCARD_RATIO) // the view is to high
        {
            content_width = (int) (width * SHOW_CONTENT_RATIO);
            content_height = (int) (content_width / IDCARD_RATIO);
        } else { // the view is too wide
            content_height = (int) (height * SHOW_CONTENT_RATIO);
            content_width = (int) (content_height * IDCARD_RATIO);
        }

        mShowRect.left = centerX - content_width / 2;
        mShowRect.top = centerY - content_height / 2;
        mShowRect.right = centerX + content_width / 2;
        mShowRect.bottom = centerY + content_height / 2;
        getActualRect(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    private boolean mIsVertical;
    private IDCardAttr.IDCardSide mIdCardSide;

    public void setCardSideAndOrientation(boolean mIsVertical, IDCardAttr.IDCardSide mIDCardSide) {
        this.mIsVertical = mIsVertical;
        this.mIdCardSide = mIDCardSide;
    }


    public void setContentRatio(boolean mIsVertical) {
        this.mIsVertical = mIsVertical;
        if (mIsVertical)
            CONTENT_RATIO = 1.0f;
        else
            CONTENT_RATIO = 0.8f;

        SHOW_CONTENT_RATIO = CONTENT_RATIO * 13.0f / 16.0f;
        invalidate();
    }

    /**
     * 实际给算法的大小
     */
    private void getActualRect(int width, int height) {

        int content_width = 0;
        int content_height = 0;
        int centerX = width >> 1;
        int centerY = height >> 1;

        if (width / (float) (height) < IDCARD_RATIO) // the view is to high
        {
            content_width = (int) (width * CONTENT_RATIO);
            content_height = (int) (content_width / IDCARD_RATIO);
        } else { // the view is too wide
            content_height = (int) (height * CONTENT_RATIO);
            content_width = (int) (content_height * IDCARD_RATIO);
        }

        mDrawRect.left = centerX - content_width / 2;
        mDrawRect.top = centerY - content_height / 2;
        mDrawRect.right = centerX + content_width / 2;
        mDrawRect.bottom = centerY + content_height / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // background
        mDrawPaint.setStyle(Paint.Style.FILL);
        mDrawPaint.setColor(backColor);

        // top
        mTmpRect.set(0, 0, getWidth(), mShowRect.top);
        canvas.drawRect(mTmpRect, mDrawPaint);
        // bottom
        mTmpRect.set(0, mShowRect.bottom, getWidth(), getHeight());
        canvas.drawRect(mTmpRect, mDrawPaint);
        // left
        mTmpRect.set(0, mShowRect.top, mShowRect.left, mShowRect.bottom);
        canvas.drawRect(mTmpRect, mDrawPaint);
        // right
        mTmpRect.set(mShowRect.right, mShowRect.top, getWidth(), mShowRect.bottom);
        canvas.drawRect(mTmpRect, mDrawPaint);

        drawViewfinder(canvas);

        drawFaculae(canvas);
        super.onDraw(canvas);
    }

//	public void setiCardQuality(IDCardQuality iCardQuality) {
//		this.iCardQuality = iCardQuality;
//		faculaeWidth = mDrawRect.width();
//		faculaeHeight = mDrawRect.height();
//
//		faculaePaint = new Paint();
//		faculaePaint.setStrokeWidth(5);
//
//		this.postInvalidate();
//
//	}

    //	private IDCardQuality iCardQuality;
    private Paint faculaePaint;
    private int faculaeWidth, faculaeHeight;

    private void drawFaculae(Canvas canvas) {
//		if (faculaePaint == null || iCardQuality == null) {
//			return;
//		}
//		Shadow[] Shadows = iCardQuality.Shadows;
//		Faculae[] faculaes = iCardQuality.faculaes;
//		Card[] cards = iCardQuality.cards;
//		faculaePaint.setColor(0xffaa0000);
//		int drawOffsetH = mDrawRect.top;
//		int drawOffsetW = mDrawRect.left;
//		if (mIsVertical) {
//			drawOffsetH = mDrawRect.top;
//			drawOffsetW = mDrawRect.left;
//		}
//
//		Log.w("ceshi", "drawOffset++++++====" + drawOffsetH + ", " + mIsVertical + ", " + mDrawRect);
//
//		for (int i = 0; i < Shadows.length; i++) {
//			PointF[] vertex = Shadows[i].vertex;
//			for (int j = 0; j < vertex.length; j++) {
//				float startx = vertex[j].x * faculaeWidth;
//				float starty = vertex[j].y * faculaeHeight;
//				float endx = vertex[(j + 1) % vertex.length].x * faculaeWidth;
//				float endy = vertex[(j + 1) % vertex.length].y * faculaeHeight;
//				canvas.drawLine(startx + drawOffsetW, starty + drawOffsetH, endx + drawOffsetW, endy + drawOffsetH, faculaePaint);
//			}
//		}
//		faculaePaint.setColor(0xff00aa00);
//		for (int i = 0; i < faculaes.length; i++) {
//			PointF[] vertex = faculaes[i].vertex;
//			for (int j = 0; j < vertex.length; j++) {
//				float startx = vertex[j].x * faculaeWidth;
//				float starty = vertex[j].y * faculaeHeight;
//				float endx = vertex[(j + 1) % vertex.length].x * faculaeWidth;
//				float endy = vertex[(j + 1) % vertex.length].y * faculaeHeight;
//				canvas.drawLine(startx + drawOffsetW, starty + drawOffsetH, endx + drawOffsetW, endy + drawOffsetH, faculaePaint);
//			}
//		}
//
//		faculaePaint.setColor(0xff0000aa);
//		for (int i = 0; i < cards.length; i++) {
//			PointF[] vertex = cards[i].vertex;
//			for (int j = 0; j < vertex.length; j++) {
//				float startx = vertex[j].x * faculaeWidth;
//				float starty = vertex[j].y * faculaeHeight;
//				float endx = vertex[(j + 1) % vertex.length].x * faculaeWidth;
//				float endy = vertex[(j + 1) % vertex.length].y * faculaeHeight;
//				canvas.drawLine(startx + drawOffsetW, starty + drawOffsetH, endx + drawOffsetW, endy + drawOffsetH, faculaePaint);
//			}
//		}
    }

    private void drawViewfinder(Canvas canvas) {
        int finderColor = 0XFF00D3FF;
        mDrawPaint.setStyle(Paint.Style.STROKE);
        mDrawPaint.setColor(finderColor);
        mDrawPaint.setStrokeWidth(4);
        int length = mShowRect.height() / 16;
        // 四个蓝色的角
        // left top
        canvas.drawLine(mShowRect.left, mShowRect.top, mShowRect.left + length, mShowRect.top, mDrawPaint);
        canvas.drawLine(mShowRect.left, mShowRect.top, mShowRect.left, mShowRect.top + length, mDrawPaint);

        // right top
        canvas.drawLine(mShowRect.right, mShowRect.top, mShowRect.right - length, mShowRect.top, mDrawPaint);
        canvas.drawLine(mShowRect.right, mShowRect.top, mShowRect.right, mShowRect.top + length, mDrawPaint);

        // left bottom
        canvas.drawLine(mShowRect.left, mShowRect.bottom, mShowRect.left + length, mShowRect.bottom, mDrawPaint);
        canvas.drawLine(mShowRect.left, mShowRect.bottom, mShowRect.left, mShowRect.bottom - length, mDrawPaint);

        // right bottom
        canvas.drawLine(mShowRect.right, mShowRect.bottom, mShowRect.right - length, mShowRect.bottom, mDrawPaint);
        canvas.drawLine(mShowRect.right, mShowRect.bottom, mShowRect.right, mShowRect.bottom - length, mDrawPaint);

        // 两个角中间的线
        mDrawPaint.setColor(0XBBFFFFFF);
        mDrawPaint.setStrokeWidth(2);
        // top
        canvas.drawLine(mShowRect.left + length, mShowRect.top, mShowRect.right - length, mShowRect.top, mDrawPaint);
        // left
        canvas.drawLine(mShowRect.left, mShowRect.top + length, mShowRect.left, mShowRect.bottom - length, mDrawPaint);
        // right
        canvas.drawLine(mShowRect.right, mShowRect.top + length, mShowRect.right, mShowRect.bottom - length,
                mDrawPaint);
        // bottom
        canvas.drawLine(mShowRect.left + length, mShowRect.bottom, mShowRect.right - length, mShowRect.bottom,
                mDrawPaint);

        int bitmapId = 0;
        if (mIdCardSide == IDCardAttr.IDCardSide.IDCARD_SIDE_FRONT)
            bitmapId = R.drawable.sfz_front;
        else if (mIdCardSide == IDCardAttr.IDCardSide.IDCARD_SIDE_BACK)
            bitmapId = R.drawable.sfz_back;
        Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), bitmapId);
        Rect mSrcRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        Rect mDesRect = new Rect(mShowRect.left, mShowRect.top, mShowRect.left + mShowRect.width(), mShowRect.top + mShowRect.height());
        canvas.drawBitmap(bitmap, mSrcRect, mDesRect, null);
    }

    public RectF getShowPosition() {
        RectF rectF = new RectF();
        rectF.left = mShowRect.left / (float) getWidth();
        rectF.top = mShowRect.top / (float) getHeight();
        rectF.right = mShowRect.right / (float) getWidth();
        rectF.bottom = mShowRect.bottom / (float) getHeight();
        return rectF;
    }

    public RectF getPosition() {
        RectF rectF = new RectF();
        rectF.left = mDrawRect.left / (float) getWidth();
        rectF.top = mDrawRect.top / (float) getHeight();
        rectF.right = mDrawRect.right / (float) getWidth();
        rectF.bottom = mDrawRect.bottom / (float) getHeight();
        return rectF;
    }

    public Rect getMargin() {
        Rect rect = new Rect();
        rect.left = mDrawRect.left;
        rect.top = mDrawRect.top;
        rect.right = getWidth() - mDrawRect.right;
        rect.bottom = getHeight() - mDrawRect.bottom;
        return rect;
    }
}