Get right Display Orientation - Android User Interface

Android examples for User Interface:Screen Orientation

Description

Get right Display Orientation

Demo Code


//package com.java2s;
import android.content.Context;

import android.view.Surface;
import android.view.WindowManager;

import static android.hardware.Camera.CameraInfo;

public class Main {
    public static int rightDisplayOrientation(Context context,
            CameraInfo info) {/*  www.j a  v  a  2s  . co  m*/

        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        int rotation = windowManager.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 == 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;
        }

        return result;
    }
}

Related Tutorials