properly rotate landscape photos
This commit is contained in:
parent
e06b98c825
commit
9b8a8777f6
|
@ -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;
|
||||
}
|
|
@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,9 @@ public class PhotoProcessor extends AsyncTask<byte[], Void, Void> {
|
|||
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<byte[], Void, Void> {
|
|||
} else if (orientation.equalsIgnoreCase("0")) {
|
||||
angle = 90;
|
||||
}
|
||||
|
||||
return rotateImage(bitmap, angle);
|
||||
}
|
||||
|
||||
|
@ -95,6 +98,23 @@ public class PhotoProcessor extends AsyncTask<byte[], Void, Void> {
|
|||
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();
|
||||
|
|
Loading…
Reference in New Issue