Example usage for android.view Surface ROTATION_90

List of usage examples for android.view Surface ROTATION_90

Introduction

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

Prototype

int ROTATION_90

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

Click Source Link

Document

Rotation constant: 90 degree rotation.

Usage

From source file:Main.java

public static int setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera,
        int displayRotation) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int degrees = 0;
    switch (displayRotation) {
    case Surface.ROTATION_0:
        degrees = 0;//  w  w  w. j a  v a2s. c  o  m
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

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

    if (camera != null) {
        try {
            camera.setDisplayOrientation(camRotationDegree);
        } catch (RuntimeException e) {
            // java.lang.RuntimeException: set display orientation failed
            e.printStackTrace();
        }
    }
    return camRotationDegree;
}

From source file:Main.java

public static int getOrientationHint(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;/*ww w. j a v a 2  s.  c o  m*/
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

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

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  w w  .  j  av a2s. c  o m*/
        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  w  w.  j  a  va2  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, "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.  jav a 2 s.  c o  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 v  a 2  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:/*from  w ww.jav a 2s .c om*/
            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

/**
 * Set Camera orientation to correct one
 * @param context app context/*from w w w .j  ava2 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

/**
 * Locks specified activity's orientation until it is unlocked or recreated.
 *
 * @param activity activity// www  .ja va 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);
}

From source file:Main.java

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