Example usage for android.hardware Camera setDisplayOrientation

List of usage examples for android.hardware Camera setDisplayOrientation

Introduction

In this page you can find the example usage for android.hardware Camera setDisplayOrientation.

Prototype

public native final void setDisplayOrientation(int degrees);

Source Link

Document

Set the clockwise rotation of preview display in degrees.

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;//from  www  .ja  va 2s .c  om
        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

/**
 * Set Camera orientation to correct one
 * @param context app context// w w w  . j  a v 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:org.artoolkit.ar.base.camera.CaptureCameraPreview.java

private void setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);

    WindowManager wMgr = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);

    int rotation = wMgr.getDefaultDisplay().getRotation();

    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;/*from   www  .j a v  a2  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;
    }

    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:com.jasompeter.openalpr.CameraActivity.java

public void setCorrectOrientation(Camera camera) {
    int displayRotation = getWindowManager().getDefaultDisplay().getRotation();

    int degrees = 0;
    switch (displayRotation) {
    case Surface.ROTATION_0:
        degrees = 0;/*from  w  w w.  j  av  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;
    }

    Camera.CameraInfo cameraInfo = getCurrentCameraInfo();
    int resultDegrees;
    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        resultDegrees = (cameraInfo.orientation + degrees) % 360;
        resultDegrees = (360 - resultDegrees) % 360;
    } else {
        resultDegrees = (cameraInfo.orientation - degrees + 360) % 360;
    }

    camera.setDisplayOrientation(resultDegrees);

    Camera.Parameters parameters = camera.getParameters();
    parameters.setRotation(resultDegrees);
    camera.setParameters(parameters);
}