get Display Orientation by Camera ID - Android Camera

Android examples for Camera:Camera Orientation

Description

get Display Orientation by Camera ID

Demo Code


//package com.java2s;
import android.app.Activity;
import android.hardware.Camera;
import android.util.Log;
import android.view.Surface;

public class Main {
    public static int getDisplayOrientation(Activity activity,
            int mCameraId, boolean isFrontCamera) {
        int mDisplayRotation = getDisplayRotation(activity);
        int mDisplayOrientation = getDisplayOrientation(mDisplayRotation,
                mCameraId);//from  w ww.j  av  a 2  s . c  o  m
        int mCameraDisplayOrientation = getDisplayOrientation(0, mCameraId);
        int previewDegrees = (!isFrontCamera) ? mDisplayOrientation
                : mDisplayOrientation + 180;
        previewDegrees = previewDegrees % 360;
        Log.e("degree", "previewDegrees" + previewDegrees);
        return previewDegrees;
    }

    public static int getDisplayOrientation(int degrees, int cameraId) {
        // See android.hardware.Camera.setDisplayOrientation for
        // documentation.
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId, info);
        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;
    }

    private static int getDisplayRotation(Activity activity) {
        int rotation = activity.getWindowManager().getDefaultDisplay()
                .getRotation();
        switch (rotation) {
        case Surface.ROTATION_0:
            return 0;
        case Surface.ROTATION_90:
            return 90;
        case Surface.ROTATION_180:
            return 180;
        case Surface.ROTATION_270:
            return 270;
        }
        return 0;
    }
}

Related Tutorials