Skip to content

Commit

Permalink
Impelement proper rotation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf104a committed Nov 18, 2023
1 parent f239591 commit 8042628
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
43 changes: 31 additions & 12 deletions app/src/main/java/com/polar/mirror/FreezeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,15 +25,13 @@

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.io.File;
import java.nio.ByteBuffer;

/**
* Controls freezing camera view
*/
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;
Expand All @@ -44,7 +40,6 @@ public class FreezeController {

FreezeController(Context context, FloatingActionButton freezeButton, PreviewView cameraView,
ImageView freezeView){
mFreezeButton = freezeButton;
mCameraView = cameraView;
mFreezeView = freezeView;
mContext = context;
Expand All @@ -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),
Expand All @@ -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();
}
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/polar/mirror/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 8042628

Please sign in to comment.