Skip to content

Commit

Permalink
Support overwriting original image file
Browse files Browse the repository at this point in the history
  • Loading branch information
hkk595 committed Sep 5, 2017
1 parent aee7d46 commit 5773d94
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 59 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Resizer
[![](https://jitpack.io/v/hkk595/Resizer.svg)](https://jitpack.io/#hkk595/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)

Inspired by zetbaitsu's [Compressor](https://github.com/zetbaitsu/Compressor), Resizer is a lightweight and easy-to-use Android library for image resizing. It allows you to create a smaller or bigger image from the original image while keeping the aspect ratio.
<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>

Inspired by zetbaitsu's [Compressor](https://github.com/zetbaitsu/Compressor), Resizer is a lightweight and easy-to-use Android library for image scaling. It allows you to resize an image file to a smaller or bigger one while keeping the aspect ratio.

#### Include Resizer into your project
1. Add the JitPack repository to your top-level build.gradle at the end of repositories
Expand All @@ -15,7 +17,7 @@ allprojects {
2. Add the dependency in your module-level build.gradle
```groovy
dependencies {
compile 'com.github.hkk595:Resizer:v1.0'
compile 'com.github.hkk595:Resizer:v1.1'
}
```

Expand Down
59 changes: 3 additions & 56 deletions app/src/main/java/me/echodev/resizer/Resizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.Callable;

import io.reactivex.Flowable;
import me.echodev.resizer.util.ImageUtils;

/**
* Created by K.K. Ho on 1/9/2017.
Expand Down Expand Up @@ -65,63 +64,11 @@ public Resizer setSourceImage(File sourceImage) {
}

public File getResizedFile() throws IOException {
File file = new File(destinationDirPath);
if (!file.exists()) {
file.mkdirs();
}

// Prepare the new file name and path
String originalFileName = sourceImage.getName();
String targetFileName;
String targetFileExtension = "." + compressFormat.name().toLowerCase().replace("jpeg", "jpg");

int extensionIndex = originalFileName.lastIndexOf('.');
if (extensionIndex == -1) {
targetFileName = originalFileName + targetFileExtension;
} else {
targetFileName = originalFileName.substring(0, extensionIndex) + targetFileExtension;
}

String destinationFilePath = destinationDirPath + File.separator + targetFileName;

// Write the resized image to the new file
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(destinationFilePath);
getResizedBitmap().compress(compressFormat, quality, fileOutputStream);
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}

return new File(destinationFilePath);
return ImageUtils.getScaledImage(targetLength, quality, compressFormat, destinationDirPath, sourceImage);
}

public Bitmap getResizedBitmap() throws IOException {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(sourceImage.getAbsolutePath(), options);

// Get the dimensions of the original bitmap
int originalWidth = options.outWidth;
int originalHeight = options.outHeight;
float aspectRatio = (float) originalWidth / originalHeight;

// Calculate the target dimensions
int targetWidth, targetHeight;

if (originalWidth > originalHeight) {
targetWidth = targetLength;
targetHeight = Math.round(targetWidth / aspectRatio);
} else {
aspectRatio = 1 / aspectRatio;
targetHeight = targetLength;
targetWidth = Math.round(targetHeight / aspectRatio);
}

return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
return ImageUtils.getScaledBitmap(targetLength, sourceImage);
}

public Flowable<File> getResizedFileAsFlowable() {
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/me/echodev/resizer/util/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package me.echodev.resizer.util;

import android.graphics.Bitmap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* Created by K.K. Ho on 3/9/2017.
*/

public class FileUtils {
public static String getDestinationFilePath(Bitmap.CompressFormat compressFormat, String destinationDirPath, File sourceImage) {
String originalFileName = sourceImage.getName();
String targetFileName;
String targetFileExtension = "." + compressFormat.name().toLowerCase().replace("jpeg", "jpg");

int extensionIndex = originalFileName.lastIndexOf('.');
if (extensionIndex == -1) {
targetFileName = originalFileName + targetFileExtension;
} else {
targetFileName = originalFileName.substring(0, extensionIndex) + targetFileExtension;
}

return destinationDirPath + File.separator + targetFileName;
}

public static void writeBitmapToFile(Bitmap bitmap, Bitmap.CompressFormat compressFormat, int quality, String filePath) throws IOException {
FileOutputStream fileOutputStream = null;

try {
fileOutputStream = new FileOutputStream(filePath);
bitmap.compress(compressFormat, quality, fileOutputStream);
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
}
}
54 changes: 54 additions & 0 deletions app/src/main/java/me/echodev/resizer/util/ImageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.echodev.resizer.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.io.IOException;

/**
* Created by K.K. Ho on 3/9/2017.
*/

public class ImageUtils {
public static File getScaledImage(int targetLength, int quality, Bitmap.CompressFormat compressFormat, String destinationDirPath, File sourceImage) throws IOException {
File file = new File(destinationDirPath);
if (!file.exists()) {
file.mkdirs();
}

// Prepare the new file name and path
String destinationFilePath = FileUtils.getDestinationFilePath(compressFormat, destinationDirPath, sourceImage);

// Write the resized image to the new file
Bitmap scaledBitmap = getScaledBitmap(targetLength, sourceImage);
FileUtils.writeBitmapToFile(scaledBitmap, compressFormat, quality, destinationFilePath);

return new File(destinationFilePath);
}

public static Bitmap getScaledBitmap(int targetLength, File sourceImage) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(sourceImage.getAbsolutePath(), options);

// Get the dimensions of the original bitmap
int originalWidth = options.outWidth;
int originalHeight = options.outHeight;
float aspectRatio = (float) originalWidth / originalHeight;

// Calculate the target dimensions
int targetWidth, targetHeight;

if (originalWidth > originalHeight) {
targetWidth = targetLength;
targetHeight = Math.round(targetWidth / aspectRatio);
} else {
aspectRatio = 1 / aspectRatio;
targetHeight = targetLength;
targetWidth = Math.round(targetHeight / aspectRatio);
}

return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
}
}
Binary file added app/src/main/res/drawable/library_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5773d94

Please sign in to comment.