From 9b8a8777f6be202e6a497074dcc357d5b8266bb1 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 11 Jun 2016 13:11:30 +0200 Subject: [PATCH] properly rotate landscape photos --- .../simplemobiletools/camera/Constants.java | 7 ++++ .../camera/MainActivity.java | 35 ++++++++++++++++++- .../camera/PhotoProcessor.java | 20 +++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/simplemobiletools/camera/Constants.java diff --git a/app/src/main/java/com/simplemobiletools/camera/Constants.java b/app/src/main/java/com/simplemobiletools/camera/Constants.java new file mode 100644 index 00000000..6f2f2ee5 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/camera/Constants.java @@ -0,0 +1,7 @@ +package com.simplemobiletools.camera; + +public class Constants { + public static final int ORIENT_PORTRAIT = 0; + public static final int ORIENT_LANDSCAPE_LEFT = 1; + public static final int ORIENT_LANDSCAPE_RIGHT = 2; +} diff --git a/app/src/main/java/com/simplemobiletools/camera/MainActivity.java b/app/src/main/java/com/simplemobiletools/camera/MainActivity.java index 3dec05c3..4903090d 100644 --- a/app/src/main/java/com/simplemobiletools/camera/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/camera/MainActivity.java @@ -2,6 +2,10 @@ package com.simplemobiletools.camera; import android.content.Intent; import android.hardware.Camera; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.SurfaceView; @@ -16,11 +20,13 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity implements SensorEventListener { @BindView(R.id.viewHolder) RelativeLayout viewHolder; @BindView(R.id.toggle_camera) ImageView toggleCameraBtn; @BindView(R.id.toggle_flash) ImageView toggleFlashBtn; + public static int orientation; + private static SensorManager sensorManager; private Preview preview; private int currCamera; private boolean isFlashEnabled; @@ -37,6 +43,7 @@ public class MainActivity extends AppCompatActivity { preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView)); preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); viewHolder.addView(preview); + sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } @OnClick(R.id.toggle_camera) @@ -95,11 +102,37 @@ public class MainActivity extends AppCompatActivity { } preview.setCamera(currCamera); hideNavigationBarIcons(); + + if (sensorManager != null) { + final Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); + sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); + } } @Override protected void onPause() { super.onPause(); preview.releaseCamera(); + + if (sensorManager != null) + sensorManager.unregisterListener(this); + } + + @Override + public void onSensorChanged(SensorEvent event) { + if (event.values[1] < 6.5 && event.values[1] > -6.5) { + if (event.values[0] > 0) { + orientation = Constants.ORIENT_LANDSCAPE_LEFT; + } else { + orientation = Constants.ORIENT_LANDSCAPE_RIGHT; + } + } else { + orientation = Constants.ORIENT_PORTRAIT; + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + } } diff --git a/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.java b/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.java index f43bbadb..85f88bc2 100644 --- a/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.java +++ b/app/src/main/java/com/simplemobiletools/camera/PhotoProcessor.java @@ -41,7 +41,9 @@ public class PhotoProcessor extends AsyncTask { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); bitmap = setBitmapRotation(bitmap, photoFile.toString()); bitmap = setAspectRatio(bitmap); + bitmap = checkLandscape(bitmap); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); + fos.close(); scanPhoto(photoFile); } catch (FileNotFoundException e) { @@ -81,6 +83,7 @@ public class PhotoProcessor extends AsyncTask { } else if (orientation.equalsIgnoreCase("0")) { angle = 90; } + return rotateImage(bitmap, angle); } @@ -95,6 +98,23 @@ public class PhotoProcessor extends AsyncTask { return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); } + private Bitmap checkLandscape(Bitmap bitmap) { + int angle = 0; + if (MainActivity.orientation == Constants.ORIENT_LANDSCAPE_LEFT) { + angle = -90; + } else if (MainActivity.orientation == Constants.ORIENT_LANDSCAPE_RIGHT) { + angle = 90; + } + + if (angle != 0) { + Matrix matrix = new Matrix(); + matrix.setRotate(angle); + return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); + } + + return bitmap; + } + private Bitmap setAspectRatio(Bitmap bitmap) { final double wantedAspect = (double) 16 / (double) 9; final double bmpWidth = bitmap.getWidth();