calculate Camera Display Orientation For Current Device Rotation - Android Camera

Android examples for Camera:Camera Orientation

Description

calculate Camera Display Orientation For Current Device Rotation

Demo Code


import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.util.Log;
import android.view.OrientationEventListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main{
    public static int calculateCameraDisplayOrientationForCurrentDeviceRotation(
            Context context, int cameraId) {
        int currentRotation = DeviceUtil
                .getCurrentRotationInDegrees(context);
        return calculateCameraDisplayOrientation(cameraId, currentRotation);
    }// w  w  w. j  ava 2 s  .c  o  m
    public static int calculateCameraDisplayOrientation(int cameraId,
            int deviceOrientation) {
        if (deviceOrientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
            return OrientationEventListener.ORIENTATION_UNKNOWN;
        }
        Camera.CameraInfo cameraInfo = getCameraInfo(cameraId);
        final int rounded = Rounding.roundToMultiplesOf(90,
                deviceOrientation);
        int result;
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (cameraInfo.orientation + rounded) % 360;
            result = (360 - result) % 360;
        } else {
            result = (cameraInfo.orientation - rounded + 360) % 360;
        }
        return result;
    }
    public static Camera.CameraInfo getCameraInfo(int cameraId) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId, cameraInfo);
        return cameraInfo;
    }
}

Related Tutorials