mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-02-16 19:30:40 +01:00
use 16:9 aspect ratio
This commit is contained in:
parent
e774c08ec9
commit
9e584ee2d0
@ -18,7 +18,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Bind(R.id.viewHolder) RelativeLayout viewHolder;
|
@Bind(R.id.viewHolder) RelativeLayout viewHolder;
|
||||||
|
|
||||||
private static final String TAG = Preview.class.getSimpleName();
|
private static final String TAG = Preview.class.getSimpleName();
|
||||||
private static final int REQUEST_IMAGE_CAPTURE = 1;
|
|
||||||
private Preview preview;
|
private Preview preview;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -72,32 +72,19 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback {
|
|||||||
private Camera.PictureCallback takePictureCallback = new Camera.PictureCallback() {
|
private Camera.PictureCallback takePictureCallback = new Camera.PictureCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onPictureTaken(byte[] data, Camera camera) {
|
public void onPictureTaken(byte[] data, Camera camera) {
|
||||||
final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
|
final File photoFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
|
||||||
if (pictureFile == null) {
|
if (photoFile == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final FileOutputStream fos = new FileOutputStream(pictureFile);
|
final FileOutputStream fos = new FileOutputStream(photoFile);
|
||||||
final ExifInterface exif = new ExifInterface(pictureFile.toString());
|
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
|
||||||
Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
|
bitmap = setBitmapRotation(bitmap, photoFile.toString());
|
||||||
|
bitmap = setAspectRatio(bitmap);
|
||||||
if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")) {
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
|
||||||
realImage = rotateImage(realImage, 90);
|
|
||||||
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")) {
|
|
||||||
realImage = rotateImage(realImage, 270);
|
|
||||||
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")) {
|
|
||||||
realImage = rotateImage(realImage, 180);
|
|
||||||
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")) {
|
|
||||||
realImage = rotateImage(realImage, 90);
|
|
||||||
}
|
|
||||||
|
|
||||||
realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
|
|
||||||
|
|
||||||
fos.close();
|
fos.close();
|
||||||
|
scanPhoto(photoFile);
|
||||||
final String[] picturePath = {pictureFile.getAbsolutePath()};
|
|
||||||
MediaScannerConnection.scanFile(context, picturePath, null, null);
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
Log.d(TAG, "onPictureTaken file not found: " + e.getMessage());
|
Log.d(TAG, "onPictureTaken file not found: " + e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -124,12 +111,44 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Bitmap setBitmapRotation(Bitmap bitmap, String path) throws IOException {
|
||||||
|
final ExifInterface exif = new ExifInterface(path);
|
||||||
|
if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")) {
|
||||||
|
bitmap = rotateImage(bitmap, 90);
|
||||||
|
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")) {
|
||||||
|
bitmap = rotateImage(bitmap, 270);
|
||||||
|
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")) {
|
||||||
|
bitmap = rotateImage(bitmap, 180);
|
||||||
|
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")) {
|
||||||
|
bitmap = rotateImage(bitmap, 90);
|
||||||
|
}
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
public static Bitmap rotateImage(Bitmap source, float angle) {
|
public static Bitmap rotateImage(Bitmap source, float angle) {
|
||||||
final Matrix matrix = new Matrix();
|
final Matrix matrix = new Matrix();
|
||||||
matrix.postRotate(angle);
|
matrix.postRotate(angle);
|
||||||
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
|
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scanPhoto(File photo) {
|
||||||
|
final String[] photoPath = {photo.getAbsolutePath()};
|
||||||
|
MediaScannerConnection.scanFile(context, photoPath, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
public void releaseCamera() {
|
public void releaseCamera() {
|
||||||
if (camera != null) {
|
if (camera != null) {
|
||||||
camera.stopPreview();
|
camera.stopPreview();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user