Last active
January 15, 2022 02:03
-
-
Save Lwdthe1/2d1cd0a12f30c18db698 to your computer and use it in GitHub Desktop.
A demonstration of three Java functions for scaling a photo's bitmap and saving it to a file without contorting the photo's dimensions. This is in response to this stackoverflow question: http://stackoverflow.com/questions/16060143/android-take-photo-and-resize-it-before-saving-on-sd-card/36210688#36210688
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
public class PhotoScalerAndSaver { | |
public static main(String[] args) { | |
//TODO: create your photo here | |
//Convert your photo to a bitmap | |
Bitmap photoBm = (Bitmap) "your Bitmap image"; | |
//scale and save the photo to a file on the directory | |
saveScaledPhotoToFile(Bitmap photoBm) | |
} | |
public static saveScaledPhotoToFile(Bitmap photoBm) { | |
//get its orginal dimensions | |
int bmOriginalWidth = photoBm.getWidth(); | |
int bmOriginalHeight = photoBm.getHeight(); | |
double originalWidthToHeightRatio = 1.0 * bmOriginalWidth / bmOriginalHeight; | |
double originalHeightToWidthRatio = 1.0 * bmOriginalHeight / bmOriginalWidth; | |
//choose a maximum height | |
int maxHeight = 1024; | |
//choose a max width | |
int maxWidth = 1024; | |
//call the method to get the scaled bitmap | |
photoBm = getScaledBitmap(photoBm, bmOriginalWidth, bmOriginalHeight, | |
originalWidthToHeightRatio, originalHeightToWidthRatio, | |
maxHeight, maxWidth); | |
/**********THE REST OF THIS IS FROM Prabu's answer*******/ | |
//create a byte array output stream to hold the photo's bytes | |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | |
//compress the photo's bytes into the byte array output stream | |
photoBm.compress(Bitmap.CompressFormat.JPEG, 40, bytes); | |
//construct a File object to save the scaled file to | |
File f = new File(Environment.getExternalStorageDirectory() | |
+ File.separator + "Imagename.jpg"); | |
//create the file | |
f.createNewFile(); | |
//create an FileOutputStream on the created file | |
FileOutputStream fo = new FileOutputStream(f); | |
//write the photo's bytes to the file | |
fo.write(bytes.toByteArray()); | |
//finish by closing the FileOutputStream | |
fo.close(); | |
} | |
private static Bitmap getScaledBitmap(Bitmap bm, int bmOriginalWidth, int bmOriginalHeight, double originalWidthToHeightRatio, double originalHeightToWidthRatio, int maxHeight, int maxWidth) { | |
if(bmOriginalWidth > maxWidth || bmOriginalHeight > maxHeight) { | |
Log.v(TAG, format("RESIZING bitmap FROM %sx%s ", bmOriginalWidth, bmOriginalHeight)); | |
if(bmOriginalWidth > bmOriginalHeight) { | |
bm = scaleDeminsFromWidth(bm, maxWidth, bmOriginalHeight, originalHeightToWidthRatio); | |
} else if (bmOriginalHeight > bmOriginalWidth){ | |
bm = scaleDeminsFromHeight(bm, maxHeight, bmOriginalHeight, originalWidthToHeightRatio); | |
} | |
Log.v(TAG, format("RESIZED bitmap TO %sx%s ", bm.getWidth(), bm.getHeight())); | |
} | |
return bm; | |
} | |
private static Bitmap scaleDeminsFromHeight(Bitmap bm, int maxHeight, int bmOriginalHeight, double originalWidthToHeightRatio) { | |
int newHeight = (int) Math.max(maxHeight, bmOriginalHeight * .55); | |
int newWidth = (int) (newHeight * originalWidthToHeightRatio); | |
bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true); | |
return bm; | |
} | |
private static Bitmap scaleDeminsFromWidth(Bitmap bm, int maxWidth, int bmOriginalWidth, double originalHeightToWidthRatio) { | |
//scale the width | |
int newWidth = (int) Math.max(maxWidth, bmOriginalWidth * .75); | |
int newHeight = (int) (newWidth * originalHeightToWidthRatio); | |
bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true); | |
return bm; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can i use de code after i take foto on camera ? please give me answer,