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

Patch 2 #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 4 additions & 21 deletions app/src/main/java/me/echodev/resizer/util/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

public class ImageUtils {
public static File getScaledImage(int targetLength, int quality, Bitmap.CompressFormat compressFormat,
public static File getScaledImage(int targetLength, int targetWidth, int quality, Bitmap.CompressFormat compressFormat,
String outputDirPath, String outputFilename, File sourceImage) throws IOException {
File directory = new File(outputDirPath);
if (!directory.exists()) {
Expand All @@ -22,34 +22,17 @@ public static File getScaledImage(int targetLength, int quality, Bitmap.Compress
String outputFilePath = FileUtils.getOutputFilePath(compressFormat, outputDirPath, outputFilename, sourceImage);

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

return new File(outputFilePath);
}

public static Bitmap getScaledBitmap(int targetLength, File sourceImage) {
public static Bitmap getScaledBitmap(int targetLength, int targetWidth, 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);
return Bitmap.createScaledBitmap(bitmap, targetWidth, targetLength, true);
}
}