move some helper functions in Utils

This commit is contained in:
tibbi 2016-11-24 20:57:51 +01:00
parent f48334ea2d
commit 0223867412
2 changed files with 51 additions and 59 deletions

View File

@ -17,7 +17,6 @@ import android.support.v4.provider.DocumentFile;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
@ -139,7 +138,7 @@ public class Preview extends ViewGroup
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
final int rotation = getPreviewRotation(cameraId);
final int rotation = Utils.Companion.getPreviewRotation(mActivity, cameraId);
mCamera.setDisplayOrientation(rotation);
mCamera.setParameters(mParameters);
@ -220,47 +219,6 @@ public class Preview extends ViewGroup
});
}
private static int getPreviewRotation(int cameraId) {
final Camera.CameraInfo info = Utils.Companion.getCameraInfo(cameraId);
final int degrees = getRotationDegrees();
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = 360 - result;
} else {
result = info.orientation - degrees + 360;
}
return result % 360;
}
private static int getMediaRotation(int cameraId) {
final int degrees = getRotationDegrees();
final Camera.CameraInfo info = Utils.Companion.getCameraInfo(cameraId);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
return (360 + info.orientation + degrees) % 360;
}
return (360 + info.orientation - degrees) % 360;
}
private static int getRotationDegrees() {
int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
default:
return 0;
}
}
public void takePicture() {
if (mCanTakePicture) {
if (mIsFlashEnabled) {
@ -269,8 +227,8 @@ public class Preview extends ViewGroup
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
int rotation = getMediaRotation(mCurrCameraId);
rotation += compensateDeviceRotation();
int rotation = Utils.Companion.getMediaRotation(mActivity, mCurrCameraId);
rotation += Utils.Companion.compensateDeviceRotation(mCurrCameraId, mCallback.getCurrentOrientation());
final Camera.Size maxSize = getOptimalPictureSize();
mParameters.setPictureSize(maxSize.width, maxSize.height);
@ -401,21 +359,9 @@ public class Preview extends ViewGroup
}
}
private int compensateDeviceRotation() {
int degrees = 0;
boolean isFrontCamera = (mCurrCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT);
int deviceOrientation = mCallback.getCurrentOrientation();
if (deviceOrientation == Constants.INSTANCE.getORIENT_LANDSCAPE_LEFT()) {
degrees += isFrontCamera ? 90 : 270;
} else if (deviceOrientation == Constants.INSTANCE.getORIENT_LANDSCAPE_RIGHT()) {
degrees += isFrontCamera ? 270 : 90;
}
return degrees;
}
private int getFinalRotation() {
int rotation = getMediaRotation(mCurrCameraId);
rotation += compensateDeviceRotation();
int rotation = Utils.Companion.getMediaRotation(mActivity, mCurrCameraId);
rotation += Utils.Companion.compensateDeviceRotation(mCurrCameraId, mCallback.getCurrentOrientation());
return rotation % 360;
}

View File

@ -8,6 +8,7 @@ import android.content.res.Resources
import android.graphics.Point
import android.hardware.Camera
import android.support.v4.content.ContextCompat
import android.view.Surface
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.toast
@ -104,5 +105,50 @@ class Utils {
fun needsStupidWritePermissions(context: Context, path: String) = context.needsStupidWritePermissions(path)
fun getFileDocument(context: Context, path: String, treeUri: String) = context.getFileDocument(path, treeUri)
fun getRotationDegrees(activity: Activity): Int {
return when (activity.windowManager.defaultDisplay.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> 0
}
}
fun getPreviewRotation(activity: Activity, cameraId: Int): Int {
val info = Utils.getCameraInfo(cameraId)
val degrees = Utils.getRotationDegrees(activity)
var result: Int
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360
result = 360 - result
} else {
result = info.orientation - degrees + 360
}
return result % 360
}
fun getMediaRotation(activity: Activity, cameraId: Int): Int {
val degrees = Utils.getRotationDegrees(activity)
val info = Utils.getCameraInfo(cameraId)
return if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
(360 + info.orientation + degrees) % 360
} else
(360 + info.orientation - degrees) % 360
}
fun compensateDeviceRotation(currCameraId: Int, deviceOrientation: Int): Int {
var degrees = 0
val isFrontCamera = currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT
if (deviceOrientation == Constants.ORIENT_LANDSCAPE_LEFT) {
degrees += if (isFrontCamera) 90 else 270
} else if (deviceOrientation == Constants.ORIENT_LANDSCAPE_RIGHT) {
degrees += if (isFrontCamera) 270 else 90
}
return degrees
}
}
}