get Display Orientation For Camera - Android Camera

Android examples for Camera:Camera Orientation

Description

get Display Orientation For Camera

Demo Code


//package com.java2s;

import android.content.Context;
import android.hardware.Camera;

import android.view.Surface;

import android.view.WindowManager;

public class Main {
    public static int getDisplayOrientationForCamera(Context context,
            int cameraId) {
        final int DEGREES_IN_CIRCLE = 360;
        int temp = 0;
        int previewOrientation = 0;

        try {// w ww .  ja  v  a2s.com
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraId, cameraInfo);

            int deviceOrientation = getDeviceOrientationDegrees(context);
            switch (cameraInfo.facing) {
            case Camera.CameraInfo.CAMERA_FACING_BACK:
                temp = cameraInfo.orientation - deviceOrientation
                        + DEGREES_IN_CIRCLE;
                previewOrientation = temp % DEGREES_IN_CIRCLE;
                break;
            case Camera.CameraInfo.CAMERA_FACING_FRONT:
                temp = cameraInfo.orientation + deviceOrientation
                        % DEGREES_IN_CIRCLE;
                previewOrientation = (DEGREES_IN_CIRCLE - temp)
                        % DEGREES_IN_CIRCLE;
                break;
            }
        } catch (Exception e) {
            //Log.e(LOG_TAG, "Error in getDisplayOrientationForCamera: " + e.getMessage());
        }

        return previewOrientation;
    }

    public static int getDeviceOrientationDegrees(Context context) {
        int degrees = 0;

        try {
            WindowManager windowManager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            int rotation = windowManager.getDefaultDisplay().getRotation();

            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;
            }
        } catch (Exception e) {
            //Log.e(LOG_TAG, "Error in getDeviceOrientationDegrees: " + e.getMessage());
        }

        return degrees;
    }
}

Related Tutorials