force image ratio 16:9

This commit is contained in:
tibbi 2016-06-16 12:11:10 +02:00
parent aaf87d49dc
commit a9857c99f1
2 changed files with 15 additions and 16 deletions

View File

@ -36,7 +36,6 @@ public class PhotoProcessor extends AsyncTask<byte[], Void, Void> {
final FileOutputStream fos = new FileOutputStream(photoFile);
/*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);*/
@ -97,17 +96,4 @@ public class PhotoProcessor extends AsyncTask<byte[], Void, Void> {
return bitmap;
}
private Bitmap setAspectRatio(Bitmap bitmap) {
final double wantedAspect = (double) 16 / (double) 9;
final double bmpWidth = bitmap.getWidth();
final double bmpHeight = bitmap.getHeight();
if (bmpHeight / bmpWidth < wantedAspect) {
final double extraWidth = bmpWidth - (bmpHeight / wantedAspect);
final int startX = (int) (extraWidth / 2);
return Bitmap.createBitmap(bitmap, startX, 0, (int) (bmpWidth - extraWidth), (int) bmpHeight);
}
return bitmap;
}
}

View File

@ -26,6 +26,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
private static final String TAG = Preview.class.getSimpleName();
private static final int FOCUS_AREA_SIZE = 200;
private static final int PHOTO_PREVIEW_LENGTH = 1000;
private static final float RATIO_TOLERANCE = 0.1f;
private static SurfaceHolder surfaceHolder;
private static Camera camera;
@ -199,12 +200,13 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
}
};
// limit the max picture size at 8 megapixels
private Camera.Size getPictureSize() {
List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
Camera.Size maxSize = sizes.get(0);
for (Camera.Size size : sizes) {
if (size.width * size.height < 9000000) {
final boolean isEightMegapixelsMax = isEightMegapixelsMax(size);
final boolean isSixteenToNine = isSixteenToNine(size);
if (isEightMegapixelsMax && isSixteenToNine) {
maxSize = size;
break;
}
@ -212,6 +214,17 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
return maxSize;
}
private boolean isEightMegapixelsMax(Camera.Size size) {
return size.width * size.height < 9000000;
}
private boolean isSixteenToNine(Camera.Size size) {
final float currRatio = (float) size.height / size.width;
final float wantedRatio = (float) 9 / 16;
final float diff = Math.abs(currRatio - wantedRatio);
return diff < RATIO_TOLERANCE;
}
private void focusArea() {
if (camera == null)
return;