Calculates the clockwise rotation applied to the camera such that the picture will be aligned with the screen orientation. - Android Camera

Android examples for Camera:Camera Orientation

Description

Calculates the clockwise rotation applied to the camera such that the picture will be aligned with the screen orientation.

Demo Code

/*//from  w  w w .j ava 2s  . c o m
 * This file is part of Flying PhotoBooth.
 * 
 * Flying PhotoBooth is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Flying PhotoBooth is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Flying PhotoBooth.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;

import android.view.Surface;

public class Main {
    public static final int CAMERA_SCREEN_ORIENTATION_0 = 0;
    private static final int SCREEN_ROTATION_0 = 0;
    private static final int SCREEN_ROTATION_90 = 90;
    private static final int SCREEN_ROTATION_180 = 180;
    private static final int SCREEN_ROTATION_270 = 270;

    /**
     * Calculates the clockwise rotation applied to the camera such that the picture will be aligned with the screen
     * orientation.
     *
     * @param activity the {@link Activity}.
     * @param cameraId id of the camera.
     * @return the clockwise rotation in degrees.
     */
    public static int getCameraScreenOrientation(Activity activity,
            int cameraId) {
        int cameraScreenOrientation = CAMERA_SCREEN_ORIENTATION_0;

        // Get camera info.
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(cameraId, info);

        // Get screen orientation.
        int rotation = activity.getWindowManager().getDefaultDisplay()
                .getRotation();
        int degrees = SCREEN_ROTATION_0;
        switch (rotation) {
        case Surface.ROTATION_0:
            degrees = SCREEN_ROTATION_0;
            break;
        case Surface.ROTATION_90:
            degrees = SCREEN_ROTATION_90;
            break;
        case Surface.ROTATION_180:
            degrees = SCREEN_ROTATION_180;
            break;
        case Surface.ROTATION_270:
            degrees = SCREEN_ROTATION_270;
            break;
        }

        /*
         * Calculate result based on camera and screen orientation.
         */
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            // Calculate relative rotation between camera and screen.
            cameraScreenOrientation = (info.orientation + degrees) % 360;

            // Account for mirroring.
            cameraScreenOrientation = (360 - cameraScreenOrientation) % 360;
        } else {
            // Calculate relative rotation between camera and screen.
            cameraScreenOrientation = (info.orientation - degrees + 360) % 360;
        }

        return cameraScreenOrientation;
    }
}

Related Tutorials