properly rotate the photo at creating it

This commit is contained in:
tibbi 2016-06-16 13:48:37 +02:00
parent a9857c99f1
commit d12f6a7586
3 changed files with 18 additions and 72 deletions

View File

@ -39,17 +39,17 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@BindView(R.id.shutter) ImageView shutterBtn;
@BindView(R.id.video_rec_curr_timer) TextView recCurrTimer;
public static int orientation;
private static final int CAMERA_STORAGE_PERMISSION = 1;
private static final int AUDIO_PERMISSION = 2;
private static SensorManager sensorManager;
private Preview preview;
private int currCamera;
private boolean isFlashEnabled;
private boolean isInPhotoMode;
private boolean isAskingPermissions;
private boolean isCameraAvailable;
private int currVideoRecTimer;
private int orientation;
private int currCamera;
private Handler timerHandler;
@Override
@ -170,7 +170,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private void handleShutter() {
if (isInPhotoMode) {
preview.takePicture();
preview.takePicture(orientation);
} else {
final Resources res = getResources();
final boolean isRecording = preview.toggleRecording();

View File

@ -1,10 +1,6 @@
package com.simplemobiletools.camera;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.media.ExifInterface;
import android.os.AsyncTask;
import android.util.Log;
@ -16,11 +12,9 @@ import java.io.IOException;
public class PhotoProcessor extends AsyncTask<byte[], Void, Void> {
private static final String TAG = PhotoProcessor.class.getSimpleName();
private static Context mContext;
private static int mCameraId;
public PhotoProcessor(Context context, int cameraId) {
public PhotoProcessor(Context context) {
mContext = context;
mCameraId = cameraId;
}
@Override
@ -34,11 +28,6 @@ public class PhotoProcessor extends AsyncTask<byte[], Void, Void> {
final File photoFile = new File(photoPath);
final byte[] data = params[0];
final FileOutputStream fos = new FileOutputStream(photoFile);
/*Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
bitmap = setBitmapRotation(bitmap, photoFile.toString());
bitmap = checkLandscape(bitmap);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);*/
fos.write(data);
fos.close();
Utils.scanFile(photoPath, mContext);
@ -50,50 +39,4 @@ public class PhotoProcessor extends AsyncTask<byte[], Void, Void> {
return null;
}
private Bitmap setBitmapRotation(Bitmap bitmap, String path) throws IOException {
final ExifInterface exif = new ExifInterface(path);
final String orientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
float angle = 0f;
if (orientation.equalsIgnoreCase("6")) {
angle = 90;
} else if (orientation.equalsIgnoreCase("8")) {
angle = 270;
} else if (orientation.equalsIgnoreCase("3")) {
angle = 180;
} else if (orientation.equalsIgnoreCase("0")) {
angle = 90;
}
return rotateImage(bitmap, angle);
}
public static Bitmap rotateImage(Bitmap source, float angle) {
final Matrix matrix = new Matrix();
matrix.postRotate(angle);
final Camera.CameraInfo info = Utils.getCameraInfo(mCameraId);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
matrix.preScale(-1.f, 1.f);
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;
}
}

View File

@ -94,11 +94,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
int rotation = getPreviewRotation(cameraId);
final int rotation = getPreviewRotation(cameraId);
camera.setDisplayOrientation(rotation);
rotation = getPictureRotation(cameraId);
parameters.setRotation(rotation);
camera.setParameters(parameters);
if (canTakePicture) {
@ -124,12 +121,12 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
result = 360 - result;
} else {
result = (info.orientation - degrees + 360) % 360;
result = info.orientation - degrees + 360;
}
return result;
return result % 360;
}
private static int getPictureRotation(int cameraId) {
@ -158,7 +155,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
}
}
public void takePicture() {
public void takePicture(int orientation) {
if (canTakePicture) {
if (isFlashEnabled) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
@ -166,8 +163,16 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
int rotation = getPictureRotation(currCameraId);
if (orientation == Constants.ORIENT_LANDSCAPE_LEFT) {
rotation += 270;
} else if (orientation == Constants.ORIENT_LANDSCAPE_RIGHT) {
rotation += 90;
}
final Camera.Size maxSize = getPictureSize();
parameters.setPictureSize(maxSize.width, maxSize.height);
parameters.setRotation(rotation % 360);
MediaPlayer.create(getContext(), R.raw.camera_shutter).start();
camera.setParameters(parameters);
@ -190,9 +195,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
}
}, PHOTO_PREVIEW_LENGTH);
final Camera.CameraInfo info = Utils.getCameraInfo(currCameraId);
new PhotoProcessor(getContext(), info.facing).execute(data);
new PhotoProcessor(getContext()).execute(data);
if (isFlashEnabled) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);