get Preview Orientation Degrees - Android android.hardware

Android examples for android.hardware:Camera Preview

Description

get Preview Orientation Degrees

Demo Code

import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main{

    private static final String LOG_TAG = "CameraUtilities";
    public static int getPreviewOrientationDegrees(
            int screenOrientationDegrees, Integer cameraOrientationDegrees,
            boolean usingFrontCamera) {

        int previewOrientationDegrees = 0;

        if (cameraOrientationDegrees != null) {
            if (usingFrontCamera) { // compensate for the mirror of the front camera
                previewOrientationDegrees = (cameraOrientationDegrees + screenOrientationDegrees) % 360;
                previewOrientationDegrees = (360 - previewOrientationDegrees) % 360;
            } else { // back-facing
                previewOrientationDegrees = (cameraOrientationDegrees
                        - screenOrientationDegrees + 360) % 360;
            }//from  w  w w  . j  a  va2 s . co m
        } else {
            // TODO: can we detect camera orientation somehow?
            Log.d(LOG_TAG,
                    "Unable to detect camera orientation - setting to 0");
            previewOrientationDegrees = 0;// (90 - screenOrientationDegrees + 360) % 360;
        }

        return previewOrientationDegrees;
    }

}

Related Tutorials