From 6f5ff49c68245c4014be9547bcaeda64127055df Mon Sep 17 00:00:00 2001 From: Florian Rival Date: Mon, 7 Dec 2015 10:46:40 +0100 Subject: [PATCH] Fix resizing on Android --- .../bamlab/rnimageresizer/ImageResizer.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java index d631fe3..c67926b 100644 --- a/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java +++ b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java @@ -16,18 +16,27 @@ import java.util.Date; * Created by almouro on 11/19/15. */ class ImageResizer { - private static Bitmap resizeImage(String imagePath, int newWidth, int newHeight) { - try { - return ThumbnailUtils.extractThumbnail( - BitmapFactory.decodeFile(imagePath), - newWidth, - newHeight - ); - } catch (OutOfMemoryError ex) { - // We have no memory to rotate. Return the original bitmap. - } - return null; + private static Bitmap resizeImage(String imagePath, int maxWidth, int maxHeight) { + Bitmap image = BitmapFactory.decodeFile(imagePath); + if (maxHeight > 0 && maxWidth > 0) { + int width = image.getWidth(); + int height = image.getHeight(); + float ratioBitmap = (float) width / (float) height; + float ratioMax = (float) maxWidth / (float) maxHeight; + + int finalWidth = maxWidth; + int finalHeight = maxHeight; + if (ratioMax > 1) { + finalWidth = (int) ((float)maxHeight * ratioBitmap); + } else { + finalHeight = (int) ((float)maxWidth / ratioBitmap); + } + image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true); + return image; + } else { + return image; + } } public static Bitmap rotateImage(Bitmap b, float degrees)