get Camera to Preview Rotation - Android android.hardware

Android examples for android.hardware:Camera Preview

Description

get Camera to Preview Rotation

Demo Code

import android.app.Activity;
import android.view.Surface;

public class Main {

  /**//from  w w w. ja va 2  s  .  com
   * For some reason, only 270 and 90 need to be swapped, and only those
   *
   * @param activity
   * @return
   */
  public static int getCamera2PreviewRotation(Activity activity) {
    int screenOrientation = activity.getWindowManager().getDefaultDisplay().getRotation();

    // Translate rotation constants into degrees
    if (screenOrientation == Surface.ROTATION_0) {
      screenOrientation = 0;
    } else if (screenOrientation == Surface.ROTATION_90) {
      screenOrientation = 270;
    } else if (screenOrientation == Surface.ROTATION_180) {
      screenOrientation = 180;
    } else {
      screenOrientation = 90;
    }

    return screenOrientation;
  }

}

Related Tutorials