android jpeg compression support (#658)

*  * removed some of the re-parsing of the output image byte array
 * removed re-saving of output file
 * moved image processing onto an async task to allow camera to be used while processing is running

* added jpeg compression support for android

* move explanation to correct bit of docs
This commit is contained in:
Nick Pomfret 2017-04-06 07:28:47 +01:00 committed by Tomas Roos
parent 5097f95c6f
commit 5c54275aec
3 changed files with 14 additions and 25 deletions

View File

@ -300,6 +300,7 @@ Supported options:
- `metadata` This is metadata to be added to the captured image.
- `location` This is the object returned from `navigator.geolocation.getCurrentPosition()` (React Native's geolocation polyfill). It will add GPS metadata to the image.
- `rotation` This will rotate the image by the number of degrees specified.
- `jpegQuality` (integer between 1 and 100) This property is used to compress the output jpeg file with 100% meaning no jpeg compression will be applied.
The promise will be fulfilled with an object with some of the following properties:

View File

@ -20,7 +20,6 @@ import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@ -135,13 +134,13 @@ public class MutableImage {
}
}
public String toBase64() {
return Base64.encodeToString(toBytes(currentRepresentation), Base64.DEFAULT);
public String toBase64(int jpegQualityPercent) {
return Base64.encodeToString(toJpeg(currentRepresentation, jpegQualityPercent), Base64.DEFAULT);
}
public void writeDataToFile(File file, ReadableMap options) throws IOException {
public void writeDataToFile(File file, ReadableMap options, int jpegQualityPercent) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
fos.write(toBytes(currentRepresentation));
fos.write(toJpeg(currentRepresentation, jpegQualityPercent));
fos.close();
try {
@ -205,22 +204,6 @@ public class MutableImage {
return originalImageMetaData;
}
private static byte[] toBytes(Bitmap image) {
byte[] result = null;
try {
result = toJpeg(image, 85);
} catch (OutOfMemoryError e) {
try {
result = toJpeg(image, 70);
} catch (OutOfMemoryError e2) {
e.printStackTrace();
}
}
return result;
}
private static byte[] toJpeg(Bitmap bitmap, int quality) throws OutOfMemoryError {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);

View File

@ -575,9 +575,14 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
promise.reject("Error mirroring image", e);
}
int jpegQualityPercent = 80;
if(options.hasKey("jpegQuality")) {
jpegQualityPercent = options.getInt("jpegQuality");
}
switch (options.getInt("target")) {
case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
String encoded = mutableImage.toBase64();
String encoded = mutableImage.toBase64(jpegQualityPercent);
WritableMap response = new WritableNativeMap();
response.putString("data", encoded);
promise.resolve(response);
@ -590,7 +595,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
}
try {
mutableImage.writeDataToFile(cameraRollFile, options);
mutableImage.writeDataToFile(cameraRollFile, options, jpegQualityPercent);
} catch (IOException e) {
promise.reject("failed to save image file", e);
return;
@ -610,7 +615,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
}
try {
mutableImage.writeDataToFile(pictureFile, options);
mutableImage.writeDataToFile(pictureFile, options, 85);
} catch (IOException e) {
promise.reject("failed to save image file", e);
return;
@ -628,7 +633,7 @@ public class RCTCameraModule extends ReactContextBaseJavaModule
}
try {
mutableImage.writeDataToFile(tempFile, options);
mutableImage.writeDataToFile(tempFile, options, 85);
} catch (IOException e) {
promise.reject("failed to save image file", e);
return;