commit 0d4db6341f7f3295dc9ac88939f6f2c77e10e9c5 Author: Florian Rival Date: Sun Nov 29 20:58:58 2015 +0100 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..7c6b9f4 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# RnImageResizer diff --git a/android/build.gradle b/android/build.gradle new file mode 100644 index 0000000..deab604 --- /dev/null +++ b/android/build.gradle @@ -0,0 +1,23 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + } + } +} + +dependencies { + compile 'com.android.support:appcompat-v7:23.1.0' + compile 'com.facebook.react:react-native:0.14.+' +} diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..e832fb8 --- /dev/null +++ b/android/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + diff --git a/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java new file mode 100644 index 0000000..642c890 --- /dev/null +++ b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java @@ -0,0 +1,57 @@ +package fr.bamlab.rnimageresizer; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.media.ThumbnailUtils; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Date; + +/** + * Created by almouro on 11/19/15. + */ +class ImageResizer { + private static Bitmap resizeImage(String imagePath, int newWidth, int newHeight) { + return ThumbnailUtils.extractThumbnail( + BitmapFactory.decodeFile(imagePath), + newWidth, + newHeight + ); + } + + private static String saveImage(Bitmap bitmap, File saveDirectory, String fileName, + Bitmap.CompressFormat compressFormat, int quality) + throws IOException { + + File newFile = new File(saveDirectory, fileName + "." + compressFormat.name()); + if(!newFile.createNewFile()) { + throw new IOException("The file already exists"); + } + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + bitmap.compress(compressFormat, quality, outputStream); + byte[] bitmapData = outputStream.toByteArray(); + + outputStream.flush(); + outputStream.close(); + + FileOutputStream fos = new FileOutputStream(newFile); + fos.write(bitmapData); + fos.flush(); + fos.close(); + + return newFile.getAbsolutePath(); + } + + 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); + 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 new file mode 100644 index 0000000..fc86d28 --- /dev/null +++ b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizerModule.java @@ -0,0 +1,54 @@ +package fr.bamlab.rnimageresizer; + +import android.content.Context; +import android.graphics.Bitmap; + +import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; + +import java.io.IOException; + +/** + * Created by almouro on 19/11/15. + */ +class ImageResizerModule extends ReactContextBaseJavaModule { + private Context context; + + public ImageResizerModule(ReactApplicationContext reactContext) { + super(reactContext); + this.context = reactContext; + } + + /** + * @return the name of this module. This will be the name used to {@code require()} this module + * from javascript. + */ + @Override + public String getName() { + return "ImageResizerAndroid"; + } + + @ReactMethod + public void createResizedImage(String imagePath, int newWidth, int newHeight, String compressFormat, + int quality, final Callback successCb, final Callback failureCb) { + try { + createResizedImageWithExceptions(imagePath, newWidth, newHeight, compressFormat, quality, + successCb, failureCb); + } catch (IOException e) { + failureCb.invoke(e.getMessage()); + } + } + + private void createResizedImageWithExceptions(String imagePath, int newWidth, int newHeight, + String compressFormatString, int quality, + 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); + + successCb.invoke(resizedImagePath); + } +} diff --git a/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizerPackage.java b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizerPackage.java new file mode 100644 index 0000000..a273c7c --- /dev/null +++ b/android/src/main/java/fr/bamlab/rnimageresizer/ImageResizerPackage.java @@ -0,0 +1,49 @@ +package fr.bamlab.rnimageresizer; + +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.uimanager.ViewManager; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Created by almouro on 19/11/15. + */ +public class ImageResizerPackage implements ReactPackage { + /** + * @param reactContext react application context that can be used to create modules + * @return list of native modules to register with the newly created catalyst instance + */ + @Override + public List createNativeModules(ReactApplicationContext reactContext) { + List modules = new ArrayList<>(); + modules.add(new ImageResizerModule(reactContext)); + + return modules; + } + + /** + * @return list of JS modules to register with the newly created catalyst instance. + *

+ * IMPORTANT: Note that only modules that needs to be accessible from the native code should be + * listed here. Also listing a native module here doesn't imply that the JS implementation of it + * will be automatically included in the JS bundle. + */ + @Override + public List> createJSModules() { + return Collections.emptyList(); + } + + /** + * @param reactContext + * @return a list of view managers that should be registered with {@link UIManagerModule} + */ + @Override + public List createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } +} diff --git a/index.android.js b/index.android.js new file mode 100644 index 0000000..df19e00 --- /dev/null +++ b/index.android.js @@ -0,0 +1,12 @@ +import React from 'react-native'; + +const ImageResizerAndroid = React.NativeModules.ImageResizerAndroid; + +export default { + createResizedImage: (imagePath, newWidth, newHeight, compressFormat, quality) => { + return new Promise((resolve, reject) => { + ImageResizerAndroid.createResizedImage(imagePath, newWidth, newHeight, + compressFormat, quality, resolve, reject); + }); + }, +}; diff --git a/index.ios.js b/index.ios.js new file mode 100644 index 0000000..e4b16b6 --- /dev/null +++ b/index.ios.js @@ -0,0 +1,21 @@ +import { + NativeModules, +} from 'react-native'; + +export default { + createResizedImage: (path, width, height, format, quality) => { + if (format !== 'JPEG') { + throw new Error('Only JPEG format is supported by createResizedImage'); + } + + return new Promise((resolve, reject) => { + NativeModules.ImageResizerModule.createResizedImage(path, width, height, quality, (err, resizedPath) => { + if (err) { + return reject(err); + } + + resolve(resizedPath); + }); + }); + }, +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..8f9276b --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "rn-image-resizer", + "version": "0.0.1", + "description": "Resize local images with React Native", + "main": "index.android.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "react-native", + "react", + "android", + "camera", + "camera-roll" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/4ian/rn-image-resizer.git" + }, + "author": "Florian Rival (http://bamlab.fr)", + "license": "MIT", + "bugs": { + "url": "https://github.com/4ian/rn-image-resizer/issues" + }, + "homepage": "https://github.com/4ian/rn-image-resizer#readme" +}