convert Face Rect for Camera - Android android.hardware

Android examples for android.hardware:Camera State

Description

convert Face Rect for Camera

Demo Code

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

public class Main {

  /**/*from  w  w w  .ja va2s. c om*/
   * Convert coordination of face rect to screen rect that will be draw on
   * canvas<br>
   */
  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);
    return new Rect((int) tmp.left, (int) tmp.top, (int) tmp.right, (int) tmp.bottom);
  }

  /**
   * create a coordination convert matrix <br>
   * See also {@link android.hardware.Camera.Face#rect}
   */
  private static Matrix createConvertMatrix(boolean frontCamera, float displayOrientation, float viewWidth,
      float viewHeight) {
    Matrix matrix = new Matrix();
    // Need mirror for front camera.
    boolean mirror = frontCamera;
    matrix.setScale(mirror ? -1 : 1, 1);
    // This is the value for android.hardware.Camera.setDisplayOrientation.
    matrix.postRotate(displayOrientation);
    // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
    // UI coordinates range from (0, 0) to (width, height).
    matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
    matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
    return matrix;
  }

}

Related Tutorials