安卓(Android) 怎么计算应用到相机(Camera)的顺时针旋转程度,以使图片与屏幕方向对齐

2019年10月27日22:28:45安卓(Android) 怎么计算应用到相机(Camera)的顺时针旋转程度,以使图片与屏幕方向对齐已关闭评论 300 1277字阅读4分15秒

1.简介

安卓(Android) 怎么计算应用到相机(Camera)的顺时针旋转程度,以使图片与屏幕方向对齐?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


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;

    
    public static int getCameraScreenOrientation(Activity activity,
            int cameraId) {
        int cameraScreenOrientation = CAMERA_SCREEN_ORIENTATION_0;

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

        
        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;
        }

        
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            
            cameraScreenOrientation = (info.orientation + degrees) % 360;

            
            cameraScreenOrientation = (360 - cameraScreenOrientation) % 360;
        } else {
            
            cameraScreenOrientation = (info.orientation - degrees + 360) % 360;
        }

        return cameraScreenOrientation;
    }
}

 
继续阅读