Convert coordination of face rect to screen rect that will be draw on canvas - Android Graphics

Android examples for Graphics:Canvas

Description

Convert coordination of face rect to screen rect that will be draw on canvas

Demo Code


//package com.java2s;
import android.graphics.Matrix;

import android.graphics.Rect;
import android.graphics.RectF;

public class Main {
    public static Rect convertFaceRect(Rect faceRect, boolean frontCamera,
            float displayOrientation, float viewWidth, float viewHeight) {

        RectF tmp = new RectF(faceRect);
        Matrix matrix = createConvertMatrix(frontCamera,
                displayOrientation, viewWidth, viewHeight);
        matrix.mapRect(tmp);/*from www. ja  va2  s  . co  m*/
        return new Rect((int) tmp.left, (int) tmp.top, (int) tmp.right,
                (int) tmp.bottom);
    }
    private static Matrix createConvertMatrix(boolean frontCamera,
            float displayOrientation, float viewWidth, float viewHeight) {
        Matrix matrix = new Matrix();
        boolean mirror = frontCamera;
        matrix.setScale(mirror ? -1 : 1, 1);
        matrix.postRotate(displayOrientation);
        matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
        matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
        return matrix;
    }
}

Related Tutorials