安卓(Android) 怎么确定要应用于给定相机(Camera)预览的旋转

2019年10月27日22:41:29安卓(Android) 怎么确定要应用于给定相机(Camera)预览的旋转已关闭评论 370 868字阅读2分53秒

1.简介

安卓(Android) 怎么确定要应用于给定相机(Camera)预览的旋转?——请看下文。

2.示例代码

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


import android.app.Activity;
import android.hardware.Camera;
import android.view.Surface;

public class Main {
    
     
    public static int getRotationDegrees(Activity activity, int cameraId,
            boolean mirror) {
        int rotation = activity.getWindowManager().getDefaultDisplay()
                .getRotation();
        int degrees = 0;

        switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
        }

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

        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {

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

        return degrees;
    }
}

 
继续阅读
编程十万个怎么办