Example usage for android.view Surface ROTATION_0

List of usage examples for android.view Surface ROTATION_0

Introduction

In this page you can find the example usage for android.view Surface ROTATION_0.

Prototype

int ROTATION_0

To view the source code for android.view Surface ROTATION_0.

Click Source Link

Document

Rotation constant: 0 degree rotation (natural orientation)

Usage

From source file:Main.java

/** Returns the result of Display.getRotation, or Surface.ROTATION_0 if the getRotation method isn't available in
 * the current Android API.//from w  ww  .ja  v a2s.  c om
 */
public static int getDeviceRotation(Context context) {
    try {
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        Method rotationMethod = null;
        // look for getRotation or the deprecated getOrientation
        for (String methodName : new String[] { "getRotation", "getOrientation" }) {
            try {
                rotationMethod = Display.class.getMethod(methodName);
                break;
            } catch (Exception ignored) {
            }
        }
        if (rotationMethod != null) {
            return (Integer) rotationMethod.invoke(display);
        }
    } catch (Exception ignored) {
    }
    return Surface.ROTATION_0;
}

From source file:Main.java

/**
 * reference http://www.jianshu.com/p/1513134733d0
 *///from  w  w  w .  j  av a 2  s.com
public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera) {
    Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

From source file:Main.java

public static int getScreenOrientation(Activity activity) {
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width
            || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) {
        switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        case Surface.ROTATION_90:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_180:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        case Surface.ROTATION_270:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        default://  w  w w. j  a  v  a2 s  .com
            Log.e("getScreenOrientation", "Unknown screen orientation. Defaulting to portrait.");
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        }
    } else {
        // if the device's natural orientation is landscape or if the device
        // is square:

        switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_90:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        case Surface.ROTATION_180:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        case Surface.ROTATION_270:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        default:
            Log.e("getScreenOrientation", "Unknown screen orientation. Defaulting to landscape.");
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        }
    }

    return orientation;
}

From source file:Main.java

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    if (!sIsTruncated) {
        try {//from  w  ww.jav  a 2  s .  c o  m
            SensorManager.getRotationMatrixFromVector(sUIThreadTmp, event.values);
        } catch (Exception e) {
            // On some Samsung devices, SensorManager#getRotationMatrixFromVector throws an exception
            // if the rotation vector has more than 4 elements. Since only the four first elements are used,
            // we can truncate the vector without losing precision.
            Log.e(TAG, "maybe Samsung bug, will truncate vector");
            sIsTruncated = true;
        }
    }

    if (sIsTruncated) {
        System.arraycopy(event.values, 0, sTruncatedVector, 0, 4);
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, sTruncatedVector);
    }

    float[] values = event.values;
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrixFromVector(output, values);
        break;
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values);
        SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values);
        SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X,
                output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}

From source file:Main.java

public static int getSurfaceOrientation(Activity activity) {

    // Sanity check:
    if (activity == null) {
        return -1; // invalid value
    }//from ww  w . j  a  v  a2  s  .co m

    Configuration config = activity.getResources().getConfiguration();
    Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    int displayRotation;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        displayRotation = display.getRotation(); // only available from Froyo
    } else {
        displayRotation = display.getOrientation();
    }

    int activityOrientation = SCREEN_ORIENTATION_UNKNOWN;

    switch (config.orientation) {
    case Configuration.ORIENTATION_PORTRAIT:
    case Configuration.ORIENTATION_SQUARE:
        activityOrientation = ((displayRotation == Surface.ROTATION_0
                || displayRotation == Surface.ROTATION_270) ? SCREEN_ORIENTATION_PORTRAIT
                        : SCREEN_ORIENTATION_PORTRAITUPSIDEDOWN);
        break;

    case Configuration.ORIENTATION_LANDSCAPE:
        activityOrientation = ((displayRotation == Surface.ROTATION_0 || displayRotation == Surface.ROTATION_90)
                ? SCREEN_ORIENTATION_LANDSCAPELEFT
                : SCREEN_ORIENTATION_LANDSCAPERIGHT);
        break;

    case Configuration.ORIENTATION_UNDEFINED:
    default:
        break;
    }

    return activityOrientation;
}

From source file:Main.java

/**
 * Set Camera orientation to correct one
 * @param context app context/*from  w  ww  . j av a 2s. co  m*/
 * @param camera camera to set orientation
 */
public static void setCameraDisplayOrientation(Context context, Camera camera) {
    if (camera == null) {
        return;
    }

    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(getCameraInformationId(), info);

    WindowManager winManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int rotation = winManager.getDefaultDisplay().getRotation();

    int degrees = 0;

    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

From source file:Main.java

@SuppressLint("NewApi")
public static void lockScreenOrientation(Activity activity) {
    switch (activity.getResources().getConfiguration().orientation) {
    case Configuration.ORIENTATION_PORTRAIT:
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {/*w w  w.ja  v  a 2s.  co  m*/
            int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_180) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            } else {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
        break;

    case Configuration.ORIENTATION_LANDSCAPE:
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
        }
        break;
    }
}

From source file:Main.java

/**
 * Calculates the default orientation of the device.<br>
 * The default orientation of most phones is portrait, and the default orientation of most tablets is landscape.
 * //from  w w w .j  ava 2 s .  c o  m
 * @param activity Activity.
 * @return {@link Configuration#ORIENTATION_PORTRAIT} or {@link Configuration#ORIENTATION_LANDSCAPE}
 */
public static int getDefaultOrientationOfDevice(Activity activity) {
    Configuration configuration = activity.getResources().getConfiguration();
    boolean orientationLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);

    Display display = activity.getWindowManager().getDefaultDisplay();
    int rotation = display.getRotation();
    boolean parallelToDefaultVerticalAxis = (rotation == Surface.ROTATION_0)
            || (rotation == Surface.ROTATION_180);

    boolean defaultOrientationLandscape = (parallelToDefaultVerticalAxis && orientationLandscape)
            || (!parallelToDefaultVerticalAxis && !orientationLandscape);

    if (defaultOrientationLandscape) {
        return Configuration.ORIENTATION_LANDSCAPE;
    }
    return Configuration.ORIENTATION_PORTRAIT;
}

From source file:Main.java

/**
 * Calculates the clockwise rotation applied to the camera such that the picture will be aligned with the screen
 * orientation./*from  w  ww.  jav  a  2 s.  c o m*/
 *
 * @param activity the {@link Activity}.
 * @param cameraId id of the camera.
 * @return the clockwise rotation in degrees.
 */
public static int getCameraScreenOrientation(Activity activity, int cameraId) {
    int cameraScreenOrientation = CAMERA_SCREEN_ORIENTATION_0;

    // Get camera info.
    CameraInfo info = new CameraInfo();
    Camera.getCameraInfo(cameraId, info);

    // Get screen orientation.
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = SCREEN_ROTATION_0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = SCREEN_ROTATION_0;
        break;
    case Surface.ROTATION_90:
        degrees = SCREEN_ROTATION_90;
        break;
    case Surface.ROTATION_180:
        degrees = SCREEN_ROTATION_180;
        break;
    case Surface.ROTATION_270:
        degrees = SCREEN_ROTATION_270;
        break;
    }

    /*
     * Calculate result based on camera and screen orientation.
     */
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        // Calculate relative rotation between camera and screen.
        cameraScreenOrientation = (info.orientation + degrees) % 360;

        // Account for mirroring.
        cameraScreenOrientation = (360 - cameraScreenOrientation) % 360;
    } else {
        // Calculate relative rotation between camera and screen.
        cameraScreenOrientation = (info.orientation - degrees + 360) % 360;
    }

    return cameraScreenOrientation;
}

From source file:org.chromium.ChromeSystemDisplay.java

private int getRotation(final Display display) {
    final int rotation = display.getRotation();
    final int DEFAULT_ROTATION = 0;

    if (rotation == Surface.ROTATION_0) {
        return 0;
    } else if (rotation == Surface.ROTATION_90) {
        return 90;
    } else if (rotation == Surface.ROTATION_180) {
        return 180;
    } else if (rotation == Surface.ROTATION_270) {
        return 270;
    }// w w w  .j ava2 s  .c o  m
    return DEFAULT_ROTATION;
}