Add ability to control size by only specifying width + made orientation optionnal. see #768

This commit is contained in:
Michaël Villeneuve 2018-02-23 14:54:45 +01:00
parent e064bab7db
commit 983337d9f8

View File

@ -61,18 +61,19 @@ public class ResolveTakenPictureAsyncTask extends AsyncTask<Void, Void, Writable
try {
if (inputStream != null) {
if (mOptions.hasKey("width") && mOptions.hasKey("height")) {
mBitmap = resizeBitmap(mBitmap, mOptions.getInteger("width"), mOptions.getInteger("height"));
}
ExifInterface exifInterface = new ExifInterface(inputStream);
// Get orientation of the image from mImageData via inputStream
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED
);
// Rotate the bitmap to the proper orientation if needed
if (orientation != ExifInterface.ORIENTATION_UNDEFINED) {
// Get orientation of the image from mImageData via inputStream
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
if (mOptions.hasKey("width")) {
mBitmap = resizeBitmap(mBitmap, mOptions.getInt("width"));
}
// Rotate the bitmap to the proper orientation if needed and asked
if (mOptions.hasKey("fixOrientation") && mOptions.getBoolean("fixOrientation")
&& orientation != ExifInterface.ORIENTATION_UNDEFINED) {
mBitmap = rotateBitmap(mBitmap, getImageRotation(orientation));
}
@ -85,6 +86,7 @@ public class ResolveTakenPictureAsyncTask extends AsyncTask<Void, Void, Writable
WritableMap exifData = RNCameraViewHelper.getExifData(exifInterface);
response.putMap("exif", exifData);
}
}
// Upon rotating, write the image's dimensions to the response
@ -140,18 +142,12 @@ public class ResolveTakenPictureAsyncTask extends AsyncTask<Void, Void, Writable
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
public Bitmap resizeBitmap(Bitmap bm, int newWidth, int newHeight) {
private Bitmap resizeBitmap(Bitmap bm, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
float scaleRatio = (float) newWidth / (float) width;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
return Bitmap.createScaledBitmap(bm, newWidth, (int) (height * scaleRatio), true);
}
private Bitmap flipHorizontally(Bitmap source) {
@ -205,7 +201,7 @@ public class ResolveTakenPictureAsyncTask extends AsyncTask<Void, Void, Writable
}
return outputPath;
}
}
@Override
protected void onPostExecute(WritableMap response) {