Browse Source

Speichern und Lesen von verschiedenen URI Schemata; JS-Interface wesentlich schlanker;

Christian Kahlau 6 years ago
parent
commit
f2392d778a
2 changed files with 42 additions and 72 deletions
  1. 38 43
      src/android/ImageProcessing.java
  2. 4 29
      www/ImageProcessing.js

+ 38 - 43
src/android/ImageProcessing.java

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

+ 4 - 29
www/ImageProcessing.js

@@ -1,5 +1,4 @@
 var exec = require('cordova/exec');
-var argscheck = require('cordova/argscheck');
 
 var imageProcessing = new ImageProcessing();
 
@@ -7,36 +6,12 @@ function ImageProcessing() {
     console.log("ImageProcessing.js - is created");
 }
 
-imageProcessing.resize = function (successCallback, errorCallback, options) {
-    console.log("ImageProcessing.js - resize: " + JSON.stringify(options));
-
-    options = options || {};
-    var getValue = argscheck.getValue;
-
-    var sourceUri = options.sourceUri;
-    var destinationUri = options.destinationUri;
-    var width = options.newWidth;
-    var height = options.newHeight;
-    var keepScale = getValue(options.keepScale, false);
-
-    var args = [sourceUri, destinationUri, width, height, keepScale];
-
-    exec(successCallback, errorCallback, "ImageProcessing", "resize", args);
+imageProcessing.resize = function (successCallback, errorCallback, sourceUri, destinationUri, width, height) {
+    exec(successCallback, errorCallback, "ImageProcessing", "resize", [sourceUri, destinationUri, width, height]);
 };
 
-imageProcessing.rotate = function (successCallback, errorCallback, options) {
-    console.log("ImageProcessing.js - rotate: " + JSON.stringify(options));
-
-    options = options || {};
-    var getValue = argscheck.getValue;
-
-    var sourceUri = options.sourceUri;
-    var destinationUri = options.destinationUri;
-    var angle = getValue(options.angle, 90);
-
-    var args = [sourceUri, destinationUri, angle];
-
-    exec(successCallback, errorCallback, "ImageProcessing", "rotate", args);
+imageProcessing.rotate = function (successCallback, errorCallback, sourceUri, destinationUri, angle) {
+    errorCallback("Not implemented yet");
 };
 
 imageProcessing.crop = function (successCallback, errorCallback, sourceUri, destinationUri, rect) {