Skip to content

Commit

Permalink
Merge pull request #3 from dejandobnikar/feature/outputFilename
Browse files Browse the repository at this point in the history
Feature/output filename
  • Loading branch information
hkk595 committed Jan 9, 2018
2 parents a933b52 + 3d260e4 commit 494f008
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
35 changes: 34 additions & 1 deletion app/src/main/java/me/echodev/resizer/Resizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

import io.reactivex.Flowable;
Expand All @@ -19,13 +20,15 @@ public class Resizer {
private int targetLength, quality;
private Bitmap.CompressFormat compressFormat;
private String outputDirPath;
private String outputFilename;
private File sourceImage;

public Resizer(Context context) {
targetLength = 1080;
quality = 80;
compressFormat = Bitmap.CompressFormat.JPEG;
outputDirPath = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath();
outputFilename = null;
}

public Resizer setTargetLength(int targetLength) {
Expand Down Expand Up @@ -53,6 +56,35 @@ public Resizer setOutputFormat(String outputFormat) {
return this;
}

public Resizer setOutputFormat(Bitmap.CompressFormat compressFormat) {
if (compressFormat == null) {
throw new NullPointerException("compressFormat null");
}

this.compressFormat = compressFormat;
return this;
}

/** Set output file name.
* @param filename name of the output file, without file extension
* */
public Resizer setOutputFilename(String filename) {

if (filename == null) {
throw new NullPointerException("filename null");
}

if (filename.toLowerCase(Locale.US).endsWith(".jpg")
|| filename.toLowerCase(Locale.US).endsWith(".jpeg")
|| filename.toLowerCase(Locale.US).endsWith(".png")
|| filename.toLowerCase(Locale.US).endsWith(".webp")) {
throw new IllegalStateException("Filename should be provided without extension. See setOutputFormat(String).");
}

this.outputFilename = filename;
return this;
}

public Resizer setOutputDirPath(String outputDirPath) {
this.outputDirPath = outputDirPath;
return this;
Expand All @@ -64,7 +96,8 @@ public Resizer setSourceImage(File sourceImage) {
}

public File getResizedFile() throws IOException {
return ImageUtils.getScaledImage(targetLength, quality, compressFormat, outputDirPath, sourceImage);
return ImageUtils.getScaledImage(targetLength, quality, compressFormat, outputDirPath, outputFilename,
sourceImage);
}

public Bitmap getResizedBitmap() throws IOException {
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/me/echodev/resizer/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
*/

public class FileUtils {
public static String getOutputFilePath(Bitmap.CompressFormat compressFormat, String outputDirPath, File sourceImage) {
public static String getOutputFilePath(Bitmap.CompressFormat compressFormat, String outputDirPath, String outputFilename, 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;
if (outputFilename == null) {
int extensionIndex = originalFileName.lastIndexOf('.');
if (extensionIndex == -1) {
targetFileName = originalFileName + targetFileExtension;
} else {
targetFileName = originalFileName.substring(0, extensionIndex) + targetFileExtension;
}
} else {
targetFileName = originalFileName.substring(0, extensionIndex) + targetFileExtension;
targetFileName = outputFilename + targetFileExtension;
}

return outputDirPath + File.separator + targetFileName;
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/me/echodev/resizer/util/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
*/

public class ImageUtils {
public static File getScaledImage(int targetLength, int quality, Bitmap.CompressFormat compressFormat, String outputDirPath, File sourceImage) throws IOException {
public static File getScaledImage(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();
}

// Prepare the new file name and path
String outputFilePath = FileUtils.getOutputFilePath(compressFormat, outputDirPath, sourceImage);
String outputFilePath = FileUtils.getOutputFilePath(compressFormat, outputDirPath, outputFilename, sourceImage);

// Write the resized image to the new file
Bitmap scaledBitmap = getScaledBitmap(targetLength, sourceImage);
Expand Down

0 comments on commit 494f008

Please sign in to comment.