Initial commit
This commit is contained in:
commit
0d4db6341f
|
@ -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.+'
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="fr.bamlab.rnimageresizer">
|
||||||
|
</manifest>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
|
||||||
|
List<NativeModule> modules = new ArrayList<>();
|
||||||
|
modules.add(new ImageResizerModule(reactContext));
|
||||||
|
|
||||||
|
return modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list of JS modules to register with the newly created catalyst instance.
|
||||||
|
* <p/>
|
||||||
|
* 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<Class<? extends JavaScriptModule>> createJSModules() {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param reactContext
|
||||||
|
* @return a list of view managers that should be registered with {@link UIManagerModule}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
|
@ -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 <florianr@bamlab.fr> (http://bamlab.fr)",
|
||||||
|
"license": "MIT",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/4ian/rn-image-resizer/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/4ian/rn-image-resizer#readme"
|
||||||
|
}
|
Loading…
Reference in New Issue