Example usage for android.view Surface ROTATION_270

List of usage examples for android.view Surface ROTATION_270

Introduction

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

Prototype

int ROTATION_270

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

Click Source Link

Document

Rotation constant: 270 degree rotation.

Usage

From source file:Main.java

public static int getCameraDisplayOrientation(Activity activity, int cameraId) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;//from  w ww . ja va  2s. com
        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;
    }
    return result;
}

From source file:Main.java

static int calculateOrientation(Activity activity, int cameraId) {
    if (cameraId == NO_CAMERA)
        return 0;

    DisplayMetrics dm = new DisplayMetrics();
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int cameraRotationOffset = info.orientation;
    Log.w(TAG, "cameraRotationOffset = " + cameraRotationOffset);

    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int currentScreenRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    Log.w(TAG, "currentScreenRotation = " + currentScreenRotation);

    int degrees = 0;
    switch (currentScreenRotation) {
    case Surface.ROTATION_0:
        degrees = 0;/* w  ww .j  a va  2 s  .  c  om*/
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }
    Log.w(TAG, "degrees = " + degrees);

    int orientation;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        orientation = (cameraRotationOffset + degrees) % 360;
        orientation = (360 - orientation) % 360;
    } else {
        orientation = (cameraRotationOffset - degrees + 360) % 360;
    }
    Log.w(TAG, "orientation = " + orientation);

    return orientation;
}

From source file:Main.java

static int calculateOrientationHint(Activity activity, int cameraId) {
    if (cameraId == NO_CAMERA)
        return 0;

    DisplayMetrics dm = new DisplayMetrics();
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int cameraRotationOffset = info.orientation;
    Log.w(TAG, "OrientationHint cameraRotationOffset = " + cameraRotationOffset);

    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int currentScreenRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    Log.w(TAG, "OrientationHint currentScreenRotation = " + currentScreenRotation);

    int degrees = 0;
    switch (currentScreenRotation) {
    case Surface.ROTATION_0:
        degrees = 0;//from   w w w. ja  va  2  s.co m
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }
    Log.w(TAG, "OrientationHint degrees = " + degrees);

    int orientation;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        orientation = (cameraRotationOffset + degrees) % 360;
        if (degrees != 0) {
            orientation = (360 - orientation) % 360;
        }
    } else {
        orientation = (cameraRotationOffset - degrees + 360) % 360;
    }
    Log.w(TAG, "orientationHint = " + orientation);

    return orientation;
}

From source file:Main.java

/**
 * reference http://www.jianshu.com/p/1513134733d0
 *///from   w  w  w.j  a  va2 s . c o  m
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:/*www . j  av a 2  s  .  co  m*/
            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 int getSurfaceOrientation(Activity activity) {

    // Sanity check:
    if (activity == null) {
        return -1; // invalid value
    }//from  w w  w  . j ava  2  s  .  c  om

    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

/**
 * @Thach Feb 21, 2014//from   w w w.  j a v  a 2 s . c o m
 * @Desc lock Orientation
 * @param b
 */
@SuppressWarnings("deprecation")
public static void lockOrientation(Activity act, boolean isLock) {
    if (act == null) {
        return;
    }
    if (isLock) {
        Display display = act.getWindowManager().getDefaultDisplay();
        int rotation = display.getRotation();
        int height;
        int width;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
            height = display.getHeight();
            width = display.getWidth();
        } else {
            Point size = new Point();
            display.getSize(size);
            height = size.y;
            width = size.x;
        }
        switch (rotation) {
        case Surface.ROTATION_90:
            if (width > height)
                act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            else
                act.setRequestedOrientation(9/* reversePortait */);
            break;
        case Surface.ROTATION_180:
            if (height > width)
                act.setRequestedOrientation(9/* reversePortait */);
            else
                act.setRequestedOrientation(8/* reverseLandscape */);
            break;
        case Surface.ROTATION_270:
            if (width > height)
                act.setRequestedOrientation(8/* reverseLandscape */);
            else
                act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        default:
            if (height > width)
                act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            else
                act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    } else {
        act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
}

From source file:Main.java

/**
 * Set Camera orientation to correct one
 * @param context app context/* w w  w  .j av a 2 s.c  om*/
 * @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

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    if (!sIsTruncated) {
        try {//ww w. ja va  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

/**
 * Locks specified activity's orientation until it is unlocked or recreated.
 *
 * @param activity activity/*  w ww  . j  av a  2 s.  c  o  m*/
 */
public static void lockOrientation(Activity activity) {
    Configuration config = activity.getResources().getConfiguration();
    final int deviceOrientation = config.orientation;
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

    int orientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
    if (deviceOrientation == Configuration.ORIENTATION_PORTRAIT) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_180)
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
    } else if (deviceOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
    }

    activity.setRequestedOrientation(orientation);
}