Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exif support #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

<p align="center"><img width="50%" height="auto" src="https://raw.githubusercontent.com/hkk595/Resizer/master/app/src/main/res/drawable/library_logo.png"/></p>

Expand All @@ -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'
}
```

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
9 changes: 6 additions & 3 deletions app/src/main/java/me/echodev/resizer/Resizer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.echodev.resizer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

/**
Expand Down
113 changes: 108 additions & 5 deletions app/src/main/java/me/echodev/resizer/util/ImageUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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;
}

}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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