package fr.bamlab.rnimageresizer;

import android.content.Context;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Date;

/**
 * Created by almouro on 11/19/15.
 */
class ImageResizer {

    private static Bitmap resizeImage(String imagePath, int maxWidth, int maxHeight, Context context) {
        try {
            Bitmap image;
            if (!imagePath.startsWith("content://") && !imagePath.startsWith("file://")) {
                image = BitmapFactory.decodeFile(imagePath);
            } else {
                ContentResolver cr = context.getContentResolver();
                Uri url = Uri.parse(imagePath);
                InputStream input = cr.openInputStream(url);
                image = BitmapFactory.decodeStream(input);
                input.close();
            }
 
            if (image == null) {
                return null; // Can't load the image from the given path.
            }

            if (maxHeight > 0 && maxWidth > 0) {
                int width = image.getWidth();
                int height = image.getHeight();
                float ratioBitmap = (float) width / (float) height;
                float ratioMax = (float) maxWidth / (float) maxHeight;

                int finalWidth = maxWidth;
                int finalHeight = maxHeight;
                if (ratioMax > 1) {
                    finalWidth = (int) ((float) maxHeight * ratioBitmap);
                } else {
                    finalHeight = (int) ((float) maxWidth / ratioBitmap);
                }
                image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
            }

            return image;
        }catch (IOException ex) {
             // No memory available for resizing.
        }
        
        return null;
    }

    public static Bitmap rotateImage(Bitmap b, float degrees)
    {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees,
                    (float) b.getWidth() / 2, (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(
                        b, 0, 0, b.getWidth(), b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                // No memory available for rotating. Return the original bitmap.
            }
        }
        return b;
    }

    private static String saveImage(Bitmap bitmap, File saveDirectory, String fileName,
                                    Bitmap.CompressFormat compressFormat, int quality)
            throws IOException {
        if (bitmap == null) {
            throw new IOException("The bitmap couldn't be resized");
        }

        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, int rotation) throws IOException  {

        Bitmap resizedImage = ImageResizer.rotateImage(ImageResizer.resizeImage(imagePath, newWidth, newHeight,context), rotation);
        return ImageResizer.saveImage(resizedImage, context.getCacheDir(),
                Long.toString(new Date().getTime()), compressFormat, quality);
    }
}

@@ -1062,17 +1062,16 @@
       %7D%0A
- 
 %0A       
@@ -1250,18 +1250,20 @@
         
-in
+floa
 t width 
@@ -1298,18 +1298,20 @@
         
-in
+floa
 t height
@@ -1328,24 +1328,25 @@
 etHeight();%0A
+%0A
             
@@ -1364,90 +1364,27 @@
 atio
-Bitmap = (float) width / (float) height;%0A                float ratioMax = 
+ = Math.min(
 (float)
- 
 maxW
@@ -1389,24 +1389,31 @@
 xWidth /
+ width,
  (float)
  maxHeig
@@ -1400,25 +1400,24 @@
 dth, (float)
- 
 maxHeight;%0A%0A
@@ -1413,16 +1413,26 @@
 axHeight
+ / height)
 ;%0A%0A     
@@ -1449,132 +1449,8 @@
  int
- finalWidth = maxWidth;%0A                int finalHeight = maxHeight;%0A                if (ratioMax %3E 1) %7B%0A                   
  fin
@@ -1470,33 +1470,21 @@
 t) (
-(float) maxHeight
+width
  * ratio
 Bitm
@@ -1475,30 +1475,24 @@
 idth * ratio
-Bitmap
 );%0A         
@@ -1502,108 +1502,49 @@
     
-%7D else %7B%0A                    finalHeight = (int) ((float) maxWidth / ratioBitmap);%0A                %7D
+int finalHeight = (int) (height * ratio);
 %0A   
@@ -1764,24 +1764,16 @@
       %7D%0A
-        
 %0A       

Fix portrait image resizing on Android