diff --git a/README.md b/README.md index d7909e6..580c52a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Resizer -[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Resizer-green.svg?style=flat)](https://android-arsenal.com/details/1/6155) [![](https://jitpack.io/v/hkk595/Resizer.svg)](https://jitpack.io/#hkk595/Resizer) +[![](https://jitpack.io/v/iamdeveloper-lopez/Resizer.svg)](https://jitpack.io/#iamdeveloper-lopez/Resizer)
@@ -17,7 +17,7 @@ allprojects { 2. Add the dependency in your module-level build.gradle ```groovy dependencies { - compile 'com.github.hkk595:Resizer:v1.5' + implementation 'com.github.iamdeveloper-lopez:Resizer:v1.6' } ``` @@ -88,8 +88,6 @@ new Resizer(this) ``` Note: You don't need to declare the new image as final nor array if it's an instance variable of the class, instead of a local variable in a function. -#### Refer to the [JavaDoc](https://hkk595.github.io/Resizer) for more details. - #### Library specification Minimum SDK: API 16 @@ -119,7 +117,7 @@ Note: You don't need to declare the new image as final nor array if it's an inst ## License MIT License - Copyright (c) 2017 K.K. Ho + Copyright (c) 2018 L.L. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/app/build.gradle b/app/build.gradle index 7869dce..f79b315 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -27,5 +27,5 @@ dependencies { androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' - compile 'io.reactivex.rxjava2:rxjava:2.1.6' + implementation 'io.reactivex.rxjava2:rxjava:2.1.12' } diff --git a/app/src/main/java/me/echodev/resizer/Resizer.java b/app/src/main/java/me/echodev/resizer/Resizer.java index 25bf035..7c73111 100644 --- a/app/src/main/java/me/echodev/resizer/Resizer.java +++ b/app/src/main/java/me/echodev/resizer/Resizer.java @@ -1,5 +1,6 @@ package me.echodev.resizer; +import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.os.Environment; @@ -24,12 +25,14 @@ public class Resizer { private Bitmap.CompressFormat compressFormat; private String outputDirPath, outputFilename; private File sourceImage; + private Activity context; /** * The constructor to initialize Resizer instance. * @param context The global application context. You can get it by getApplicationContext(). */ - public Resizer(Context context) { + public Resizer(Activity context) { + this.context = context; targetLength = 1080; quality = 80; compressFormat = Bitmap.CompressFormat.JPEG; @@ -146,7 +149,7 @@ public Resizer setSourceImage(File sourceImage) { * @throws IOException */ public File getResizedFile() throws IOException { - return ImageUtils.getScaledImage(targetLength, quality, compressFormat, outputDirPath, outputFilename, + return ImageUtils.getScaledImage(context, targetLength, quality, compressFormat, outputDirPath, outputFilename, sourceImage); } @@ -156,7 +159,7 @@ public File getResizedFile() throws IOException { * @throws IOException */ public Bitmap getResizedBitmap() throws IOException { - return ImageUtils.getScaledBitmap(targetLength, sourceImage); + return ImageUtils.getScaledBitmap(context, targetLength, sourceImage); } /** diff --git a/app/src/main/java/me/echodev/resizer/util/ImageUtils.java b/app/src/main/java/me/echodev/resizer/util/ImageUtils.java index 0e63e25..0d6c074 100644 --- a/app/src/main/java/me/echodev/resizer/util/ImageUtils.java +++ b/app/src/main/java/me/echodev/resizer/util/ImageUtils.java @@ -1,7 +1,12 @@ package me.echodev.resizer.util; +import android.app.Activity; +import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.media.ExifInterface; +import android.util.DisplayMetrics; import java.io.File; import java.io.IOException; @@ -11,24 +16,60 @@ */ public class ImageUtils { - public static File getScaledImage(int targetLength, int quality, Bitmap.CompressFormat compressFormat, - String outputDirPath, String outputFilename, File sourceImage) throws IOException { + public static File getScaledImage(Activity context, int targetLength, int quality, Bitmap.CompressFormat compressFormat, + String outputDirPath, String outputFilename, File sourceImage) throws IOException { File directory = new File(outputDirPath); if (!directory.exists()) { directory.mkdirs(); } + DisplayMetrics displaymetrics; + displaymetrics = new DisplayMetrics(); + context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); + int screenWidth = displaymetrics.widthPixels; + int screenHeight = displaymetrics.heightPixels; + // Prepare the new file name and path String outputFilePath = FileUtils.getOutputFilePath(compressFormat, outputDirPath, outputFilename, sourceImage); // Write the resized image to the new file - Bitmap scaledBitmap = getScaledBitmap(targetLength, sourceImage); - FileUtils.writeBitmapToFile(scaledBitmap, compressFormat, quality, outputFilePath); + Bitmap scaledBitmap = getScaledBitmap(context, targetLength, sourceImage); + + try { + Matrix matrix = new Matrix(); + ExifInterface exifReader = new ExifInterface(sourceImage.getAbsolutePath()); + int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1); + int rotate = 0; + if (orientation == ExifInterface.ORIENTATION_NORMAL) { + // Do nothing. The original image is fine. + } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { + rotate = 90; + } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { + rotate = 180; + } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { + rotate = 270; + } + matrix.postRotate(rotate); + if (rotate > 0) + scaledBitmap = loadBitmap(sourceImage.getAbsolutePath(), rotate, screenWidth, screenHeight); + FileUtils.writeBitmapToFile(scaledBitmap, compressFormat, quality, outputFilePath); + } catch (Exception ex) { + ex.printStackTrace(); + FileUtils.writeBitmapToFile(scaledBitmap, compressFormat, quality, outputFilePath); + } return new File(outputFilePath); } - public static Bitmap getScaledBitmap(int targetLength, File sourceImage) { + public static Bitmap getScaledBitmap(Activity context, int targetLength, File sourceImage) { + int rotate = 0; + + DisplayMetrics displaymetrics; + displaymetrics = new DisplayMetrics(); + context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); + int screenWidth = displaymetrics.widthPixels; + int screenHeight = displaymetrics.heightPixels; + BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(sourceImage.getAbsolutePath(), options); @@ -50,6 +91,68 @@ public static Bitmap getScaledBitmap(int targetLength, File sourceImage) { targetWidth = Math.round(targetHeight / aspectRatio); } + try { + ExifInterface exifReader = new ExifInterface(sourceImage.getAbsolutePath()); + int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1); + if (orientation == ExifInterface.ORIENTATION_NORMAL) { + // Do nothing. The original image is fine. + } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { + rotate = 90; + } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { + rotate = 180; + } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { + rotate = 270; + } + } catch (Exception ex) { + ex.printStackTrace(); + } + if (rotate > 0) + return loadBitmap(sourceImage.getAbsolutePath(), rotate, screenWidth, screenHeight); return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true); } + + public static Bitmap loadBitmap(String path, int orientation, final int targetWidth, final int targetHeight) { + Bitmap bitmap = null; + try { + final BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(path, options); + int sourceWidth, sourceHeight; + if (orientation == 90 || orientation == 270) { + sourceWidth = options.outHeight; + sourceHeight = options.outWidth; + } else { + sourceWidth = options.outWidth; + sourceHeight = options.outHeight; + } + if (sourceWidth > targetWidth || sourceHeight > targetHeight) { + float widthRatio = (float) sourceWidth / (float) targetWidth; + float heightRatio = (float) sourceHeight / (float) targetHeight; + float maxRatio = Math.max(widthRatio, heightRatio); + options.inJustDecodeBounds = false; + options.inSampleSize = (int) maxRatio; + bitmap = BitmapFactory.decodeFile(path, options); + } else { + bitmap = BitmapFactory.decodeFile(path); + } + if (orientation > 0) { + Matrix matrix = new Matrix(); + matrix.postRotate(orientation); + bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + } + sourceWidth = bitmap.getWidth(); + sourceHeight = bitmap.getHeight(); + if (sourceWidth != targetWidth || sourceHeight != targetHeight) { + float widthRatio = (float) sourceWidth / (float) targetWidth; + float heightRatio = (float) sourceHeight / (float) targetHeight; + float maxRatio = Math.max(widthRatio, heightRatio); + sourceWidth = (int) ((float) sourceWidth / maxRatio); + sourceHeight = (int) ((float) sourceHeight / maxRatio); + bitmap = Bitmap.createScaledBitmap(bitmap, sourceWidth, sourceHeight, true); + } + } catch (Exception e) { + } + return bitmap; + } + } diff --git a/build.gradle b/build.gradle index 1d7c2ca..dd47ce3 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' + classpath 'com.android.tools.build:gradle:3.1.1' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0c4abff..9f8e683 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Nov 21 01:40:52 HKT 2017 +#Wed Apr 18 12:18:48 SGT 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip