-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support overwriting original image file
- Loading branch information
Showing
5 changed files
with
104 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.