【发布时间】:2019-02-05 18:06:50
【问题描述】:
我正在制作相机应用程序。因为我的图像没有以正确的方向保存。 我设置了
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
用于 AndroidMainfest 中的活动。 现在我正在尝试定位它总是给出 90 度。 我想通过 screenOrientation =portrait 获得移动方向。 如果我使用此代码获取方向:-
public int setPhotoOrientation(Activity activity, int cameraId) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
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;
}
int result;
// do something for phones running an SDK before lollipop
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
return result;
}
【问题讨论】:
标签: java android android-camera screen-orientation