|
|
@@ -28,25 +28,10 @@ public class ImageProcessing extends CordovaPlugin {
|
|
|
public static final String LOG_TAG = "ImageProcessing";
|
|
|
public CallbackContext callbackContext;
|
|
|
|
|
|
- private Bitmap resize(Bitmap image, int newWidth, int newHeight, boolean keepScale)
|
|
|
+ private Bitmap resize(Bitmap image, int newWidth, int newHeight)
|
|
|
{
|
|
|
if (newWidth > 0 && newHeight > 0) {
|
|
|
- int width = image.getWidth();
|
|
|
- int height = image.getHeight();
|
|
|
- int finalWidth = width;
|
|
|
- int finalHeight = height;
|
|
|
-
|
|
|
- /*if(keepScale)
|
|
|
- {
|
|
|
- //Log.d("autocrop", width + " x " + height);
|
|
|
- float scaleFactor = (width > height) ? (float)maxWidth / width : (float)maxHeight / height;
|
|
|
- //Log.d("autocrop", "" + scaleFactor);
|
|
|
- finalWidth = (int)(width * scaleFactor);
|
|
|
- finalHeight = (int)(height * scaleFactor);
|
|
|
- }*/
|
|
|
-
|
|
|
- image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
|
|
|
- return image;
|
|
|
+ return Bitmap.createScaledBitmap(image, newWidth, newHeight, true);
|
|
|
} else {
|
|
|
return image;
|
|
|
}
|
|
|
@@ -55,9 +40,27 @@ public class ImageProcessing extends CordovaPlugin {
|
|
|
private Bitmap crop(Bitmap image, Rect rect) {
|
|
|
return Bitmap.createBitmap(image, rect.left, rect.top, rect.width(), rect.height());
|
|
|
}
|
|
|
+
|
|
|
+ private Bitmap readImage(String sourceUri) throws IOException {
|
|
|
+ Bitmap image = null;
|
|
|
+ if (sourceUri.startsWith("content://")) {
|
|
|
+ Uri uri = Uri.parse(sourceUri);
|
|
|
+ ContentResolver res = ImageProcessing.super.cordova.getActivity().getContentResolver();
|
|
|
+ try (InputStream is = res.openInputStream(uri)) {
|
|
|
+ image = BitmapFactory.decodeStream(is);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ image = BitmapFactory.decodeFile(sourceUri);
|
|
|
+ }
|
|
|
+ return image;
|
|
|
+ }
|
|
|
|
|
|
private String saveImage(Bitmap image, String destinationUri) throws JSONException, IOException
|
|
|
{
|
|
|
+ if (destinationUri.startsWith("file://")) {
|
|
|
+ destinationUri = destinationUri.substring(7);
|
|
|
+ }
|
|
|
+
|
|
|
File f = new File(destinationUri);
|
|
|
File folder = f.getParentFile();
|
|
|
if (!folder.exists()) {
|
|
|
@@ -82,31 +85,27 @@ public class ImageProcessing extends CordovaPlugin {
|
|
|
final String destinationUri = (String) args.get(1);
|
|
|
final int newWidth = args.getInt(2);
|
|
|
final int newHeight = args.getInt(3);
|
|
|
- final boolean keepScale = args.getBoolean(4);
|
|
|
|
|
|
super.cordova.getActivity().runOnUiThread(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
- try
|
|
|
- {
|
|
|
- Bitmap image = BitmapFactory.decodeFile(sourceUri);
|
|
|
-
|
|
|
- if (null == image) {
|
|
|
- throw new FileNotFoundException(sourceUri);
|
|
|
+ try {
|
|
|
+ Bitmap image = readImage(sourceUri);
|
|
|
+ if (null == image) {
|
|
|
+ throw new FileNotFoundException(sourceUri);
|
|
|
+ }
|
|
|
+ Bitmap newImage = resize(image, newWidth, newHeight);
|
|
|
+ callbackContext.success(saveImage(newImage, destinationUri));
|
|
|
+ } catch (JSONException e) {
|
|
|
+ Log.e(LOG_TAG, e.getMessage(), e);
|
|
|
+ callbackContext.error(e.getClass().getSimpleName() + ": " + e .getMessage());
|
|
|
+ } catch (IOException e) {
|
|
|
+ Log.e(LOG_TAG, e.getMessage(), e);
|
|
|
+ callbackContext.error(e.getClass().getSimpleName() + ": " + e .getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.e(LOG_TAG, e.getMessage(), e);
|
|
|
+ callbackContext.error(e.getClass().getSimpleName() + ": " + e .getMessage());
|
|
|
}
|
|
|
-
|
|
|
- Bitmap newImage = resize(image, newWidth, newHeight, keepScale);
|
|
|
-
|
|
|
- saveImage(newImage, destinationUri);
|
|
|
-
|
|
|
- callbackContext.success(destinationUri);
|
|
|
-
|
|
|
- } catch (JSONException e) {
|
|
|
- callbackContext.error(e.getMessage());
|
|
|
-
|
|
|
- } catch (IOException e) {
|
|
|
- callbackContext.error(e.getMessage());
|
|
|
- }
|
|
|
}
|
|
|
});
|
|
|
|
|
|
@@ -125,17 +124,13 @@ public class ImageProcessing extends CordovaPlugin {
|
|
|
super.cordova.getActivity().runOnUiThread(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
- Uri uri = Uri.parse(sourceUri);
|
|
|
- ContentResolver res = ImageProcessing.super.cordova.getActivity().getContentResolver();
|
|
|
- try (InputStream is = res.openInputStream(uri)) {
|
|
|
- Bitmap image = BitmapFactory.decodeStream(is);
|
|
|
-
|
|
|
+ try {
|
|
|
+ Bitmap image = readImage(sourceUri);
|
|
|
if (null == image) {
|
|
|
throw new FileNotFoundException(sourceUri);
|
|
|
}
|
|
|
|
|
|
Bitmap newImage = crop(image, rect);
|
|
|
-
|
|
|
callbackContext.success(saveImage(newImage, destinationUri));
|
|
|
} catch (JSONException e) {
|
|
|
Log.e(LOG_TAG, e.getMessage(), e);
|