Add optional rotation argument on Android

This commit is contained in:
Florian Rival 2015-12-06 18:03:33 +01:00
parent bbd20103c2
commit e9b1dec7b0
3 changed files with 45 additions and 14 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
});
},
};