diff --git a/app/src/main/java/com/polar/mirror/FreezeController.java b/app/src/main/java/com/polar/mirror/FreezeController.java index 36457b7..2dd62d9 100644 --- a/app/src/main/java/com/polar/mirror/FreezeController.java +++ b/app/src/main/java/com/polar/mirror/FreezeController.java @@ -6,18 +6,16 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.Camera; import android.graphics.ImageFormat; import android.graphics.Matrix; import android.media.Image; import android.util.Log; +import android.view.Surface; import android.view.View; import android.widget.ImageView; import androidx.annotation.NonNull; -import androidx.annotation.OptIn; import androidx.camera.core.CameraSelector; -import androidx.camera.core.ExperimentalGetImage; import androidx.camera.core.ImageCapture; import androidx.camera.core.ImageCaptureException; import androidx.camera.core.ImageProxy; @@ -27,7 +25,6 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton; -import java.io.File; import java.nio.ByteBuffer; /** @@ -35,7 +32,6 @@ */ public class FreezeController { private static final String TAG = "FreezeController"; - private final FloatingActionButton mFreezeButton; private final PreviewView mCameraView; private final ImageView mFreezeView; private final ImageCapture mImageCapture; @@ -44,7 +40,6 @@ public class FreezeController { FreezeController(Context context, FloatingActionButton freezeButton, PreviewView cameraView, ImageView freezeView){ - mFreezeButton = freezeButton; mCameraView = cameraView; mFreezeView = freezeView; mContext = context; @@ -66,6 +61,35 @@ public void onCameraInitialized(ProcessCameraProvider provider, LifecycleOwner l Log.d(TAG, "completed onCameraInitialized"); } + private int getRotationAngleFromOrientation(int orientation){ + int angle = 270; + switch (orientation) { + case Surface.ROTATION_90: + angle = 0; + break; + case Surface.ROTATION_180: + angle = 90; + break; + case Surface.ROTATION_270: + angle = 180; + break; + default: + break; + } + return angle; + } + + private Bitmap processFreezeImage(byte[] bytes){ + Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); + Matrix matrix = new Matrix(); + int rotation = getRotationAngleFromOrientation(Utils.getOrientation(mContext)); + matrix.postRotate(rotation); + bitmap = Bitmap.createBitmap( + bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true + ); + return bitmap; + } + private void setFrozenImage(){ mImageCapture.takePicture(getMainExecutor(mContext), @@ -88,12 +112,7 @@ public void onCaptureSuccess(@NonNull ImageProxy imageProxy){ ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); - Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); - Matrix matrix = new Matrix(); - matrix.postRotate(270); - bitmap = Bitmap.createBitmap( - bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true - ); + Bitmap bitmap = processFreezeImage(bytes); mFreezeView.setImageBitmap(bitmap); imageProxy.close(); } diff --git a/app/src/main/java/com/polar/mirror/Utils.java b/app/src/main/java/com/polar/mirror/Utils.java new file mode 100644 index 0000000..1b6c019 --- /dev/null +++ b/app/src/main/java/com/polar/mirror/Utils.java @@ -0,0 +1,19 @@ +package com.polar.mirror; + +import android.app.Activity; +import android.content.Context; +import android.util.Log; + +public class Utils { + private static final String TAG = "Utils"; + + /** + * @param context context for getting WindowManager + * @return orientation state as in Surface class + */ + public static int getOrientation(Context context){ + int rotation = ((Activity)context).getWindowManager().getDefaultDisplay().getRotation(); + Log.d(TAG, "Rotation: " + rotation); + return rotation; + } +}