Calculate the rotation for Camera.Parameters#setRotation - Android Camera

Android examples for Camera:Camera Attribute

Description

Calculate the rotation for Camera.Parameters#setRotation

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{
    /**/*from   www.j a va2 s .  c om*/
     * Calculate the rotation for Camera.Parameters#setRotation
     *
     * @param cameraId
     * @param deviceOrientation in degrees
     * @return the rotation for Camera.Parameters#setRotation, or
     * OrientationEventListener.ORIENTATION_UNKNOWN
     */
    public static int calculateCameraRotation(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);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            return (cameraInfo.orientation - rounded + 360) % 360;
        }
        return (cameraInfo.orientation + rounded) % 360;
    }
    public static Camera.CameraInfo getCameraInfo(int cameraId) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId, cameraInfo);
        return cameraInfo;
    }
}

Related Tutorials