From e9b1dec7b010ef6a9bda93d571900bb46cbce996 Mon Sep 17 00:00:00 2001 From: Florian Rival Date: Sun, 6 Dec 2015 18:03:33 +0100 Subject: [PATCH] Add optional rotation argument on Android --- .../bamlab/rnimageresizer/ImageResizer.java | 45 ++++++++++++++++--- .../rnimageresizer/ImageResizerModule.java | 10 ++--- index.android.js | 4 +- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java index 642c890..d631fe3 100644 --- a/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java +++ b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java @@ -3,6 +3,7 @@ package fr.bamlab.rnimageresizer; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; import android.media.ThumbnailUtils; import java.io.ByteArrayOutputStream; @@ -16,16 +17,45 @@ import java.util.Date; */ class ImageResizer { private static Bitmap resizeImage(String imagePath, int newWidth, int newHeight) { - return ThumbnailUtils.extractThumbnail( - BitmapFactory.decodeFile(imagePath), - newWidth, - 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; + } + + public static Bitmap rotateImage(Bitmap b, float degrees) + { + if (degrees != 0 && b != null) { + Matrix m = new Matrix(); + m.setRotate(degrees, + (float) b.getWidth() / 2, (float) b.getHeight() / 2); + try { + Bitmap b2 = Bitmap.createBitmap( + b, 0, 0, b.getWidth(), b.getHeight(), m, true); + if (b != b2) { + b.recycle(); + b = b2; + } + } catch (OutOfMemoryError ex) { + // We have no memory to rotate. Return the original bitmap. + } + } + return b; } private static String saveImage(Bitmap bitmap, File saveDirectory, String fileName, Bitmap.CompressFormat compressFormat, int quality) throws IOException { + if (bitmap == null) { + throw new IOException("The bitmap couldn't be resized"); + } File newFile = new File(saveDirectory, fileName + "." + compressFormat.name()); if(!newFile.createNewFile()) { @@ -49,8 +79,9 @@ class ImageResizer { public static String createResizedImage(Context context, String imagePath, int newWidth, int newHeight, Bitmap.CompressFormat compressFormat, - int quality) throws IOException { - Bitmap resizedImage = ImageResizer.resizeImage(imagePath, newWidth, newHeight); + int quality, int rotation) throws IOException { + + Bitmap resizedImage = ImageResizer.rotateImage(ImageResizer.resizeImage(imagePath, newWidth, newHeight), rotation); return ImageResizer.saveImage(resizedImage, context.getCacheDir(), Long.toString(new Date().getTime()), compressFormat, quality); } diff --git a/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizerModule.java b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizerModule.java index fc86d28..7187944 100644 --- a/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizerModule.java +++ b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizerModule.java @@ -32,23 +32,23 @@ class ImageResizerModule extends ReactContextBaseJavaModule { @ReactMethod public void createResizedImage(String imagePath, int newWidth, int newHeight, String compressFormat, - int quality, final Callback successCb, final Callback failureCb) { + int quality, int rotation, final Callback successCb, final Callback failureCb) { try { createResizedImageWithExceptions(imagePath, newWidth, newHeight, compressFormat, quality, - successCb, failureCb); + rotation, successCb, failureCb); } catch (IOException e) { failureCb.invoke(e.getMessage()); } } private void createResizedImageWithExceptions(String imagePath, int newWidth, int newHeight, - String compressFormatString, int quality, + String compressFormatString, int quality, int rotation, final Callback successCb, final Callback failureCb) throws IOException { Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.valueOf(compressFormatString); imagePath = imagePath.replace("file:", ""); String resizedImagePath = ImageResizer.createResizedImage(this.context, imagePath, newWidth, - newHeight, compressFormat, quality); + newHeight, compressFormat, quality, rotation); - successCb.invoke(resizedImagePath); + successCb.invoke("file:" + resizedImagePath); } } diff --git a/index.android.js b/index.android.js index df19e00..14972ee 100644 --- a/index.android.js +++ b/index.android.js @@ -3,10 +3,10 @@ import React from 'react-native'; const ImageResizerAndroid = React.NativeModules.ImageResizerAndroid; export default { - createResizedImage: (imagePath, newWidth, newHeight, compressFormat, quality) => { + createResizedImage: (imagePath, newWidth, newHeight, compressFormat, quality, rotation = 0) => { return new Promise((resolve, reject) => { ImageResizerAndroid.createResizedImage(imagePath, newWidth, newHeight, - compressFormat, quality, resolve, reject); + compressFormat, quality, rotation, resolve, reject); }); }, };