安卓(Android) 怎么给定屏幕方向和相机(Camera)(索引),找到旋转相机以与当前方向对齐所需的度数

2019年10月27日22:34:14安卓(Android) 怎么给定屏幕方向和相机(Camera)(索引),找到旋转相机以与当前方向对齐所需的度数已关闭评论 515 1633字阅读5分26秒

1.简介

安卓(Android) 怎么给定屏幕方向和相机(Camera)(索引),找到旋转相机以与当前方向对齐所需的度数?——请看下文。

2.示例代码

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


import android.annotation.TargetApi;
import android.app.Activity;

import android.hardware.Camera;
import android.hardware.camera2.CameraCharacteristics;

import android.util.Log;

import android.view.Surface;

public class Main {
    public static final String TAG = "CameraUtils";

    public static int getRotationDegrees(Activity activity,
            Camera.CameraInfo info) {
        
        int cameraOrientation = info.orientation;

        int screenOrientation = activity.getWindowManager()
                .getDefaultDisplay().getRotation();

        
        if (screenOrientation == Surface.ROTATION_0) {
            screenOrientation = 0;
        } else if (screenOrientation == Surface.ROTATION_90) {
            screenOrientation = 90;
        } else if (screenOrientation == Surface.ROTATION_180) {
            screenOrientation = 180;
        } else {
            screenOrientation = 270;
        }

        
        int degreesToRotate = ((cameraOrientation - screenOrientation) + 360) % 360;

        return degreesToRotate;
    }

    @TargetApi(21)
    public static int getRotationDegrees(Activity activity,
            CameraCharacteristics characteristics, boolean isFront) {
        
        int cameraOrientation = characteristics
                .get(CameraCharacteristics.SENSOR_ORIENTATION);

        int screenOrientation = activity.getWindowManager()
                .getDefaultDisplay().getRotation();

        
        if (screenOrientation == Surface.ROTATION_0) {
            screenOrientation = 0;
        } else if (screenOrientation == Surface.ROTATION_90) {
            screenOrientation = 90;
        } else if (screenOrientation == Surface.ROTATION_180) {
            screenOrientation = 180;
        } else {
            screenOrientation = 270;
        }

        
        int degreesToRotate = ((cameraOrientation - screenOrientation) + 360) % 360;

        if (isFront) {
            if (degreesToRotate == 180) {
                degreesToRotate = 0;
            } else if (degreesToRotate == 0) {
                degreesToRotate = 180;
            }
        }
        return degreesToRotate;
    }

    public static void log(String msg) {
        Log.v(TAG, msg);
    }
}

 
继续阅读