get Is Camera Sideways - Android android.hardware

Android examples for android.hardware:Camera State

Description

get Is Camera Sideways

Demo Code

import java.util.List;

import android.app.Activity;
import android.hardware.Camera;
import android.view.Display;

public class Main {

  /**//from   w w  w  .j  ava2  s . c  o  m
   * tell if the camera is sideways without taking a picture, based on whether the
   * camera's preview sizes are mostly the same as our current orientation.
   *
   * @param cameraPreviewSizes
   * @return
   */
  public static boolean getIsCameraSideways(List<Camera.Size> cameraPreviewSizes, Activity activity) {
    // Get screen dimensions
    Display display = activity.getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();

    boolean isPortrait = (height >= width);
    int numSidewaysOrientations = 0;

    for (Camera.Size size : cameraPreviewSizes) {
      if (isPortrait == (size.height <= size.width)) {
        numSidewaysOrientations++;
      }
    }

    return (numSidewaysOrientations > cameraPreviewSizes.size() / 2);
  }

}

Related Tutorials