Created
February 4, 2015 04:36
-
-
Save kashifrazzaqui/0c2d7dc252c1ab7be8fb to your computer and use it in GitHub Desktop.
Scaling a bitmap in android
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
import android.graphics.*; | |
import android.net.Uri; | |
public class BitmapFileLoader | |
{ | |
public static Bitmap loadFromUri(Uri fileUri, float maxWidth, float maxHeight) | |
{ | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(fileUri.getPath(), options); | |
int actualHeight = options.outHeight; | |
int actualWidth = options.outWidth; | |
float imgRatio = actualWidth / actualHeight; | |
float maxRatio = maxWidth / maxHeight; | |
if (actualHeight > maxHeight || actualWidth > maxWidth) | |
{ | |
if (imgRatio < maxRatio) | |
{ | |
imgRatio = maxHeight / actualHeight; | |
actualWidth = (int) (imgRatio * actualWidth); | |
actualHeight = (int) maxHeight; | |
} | |
else if (imgRatio > maxRatio) | |
{ | |
imgRatio = maxWidth / actualWidth; | |
actualHeight = (int) (imgRatio * actualHeight); | |
actualWidth = (int) maxWidth; | |
} | |
else | |
{ | |
actualHeight = (int) maxHeight; | |
actualWidth = (int) maxWidth; | |
} | |
} | |
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight); | |
options.inJustDecodeBounds = false; | |
options.inDither = true; | |
options.inPurgeable = true; | |
options.inInputShareable = true; | |
options.inTempStorage = new byte[16 * 1024]; | |
Bitmap sampledBitmap = BitmapFactory.decodeFile(fileUri.getPath(), options); | |
return createScaledBitmap(sampledBitmap, actualWidth, actualHeight); | |
} | |
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) | |
{ | |
final int height = options.outHeight; | |
final int width = options.outWidth; | |
int inSampleSize = 1; | |
if (height > reqHeight || width > reqWidth) | |
{ | |
final int heightRatio = Math.round((float) height / (float) reqHeight); | |
final int widthRatio = Math.round((float) width / (float) reqWidth); | |
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; | |
} | |
final float totalPixels = width * height; | |
final float totalReqPixelsCap = reqWidth * reqHeight * 2; | |
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) | |
{ | |
inSampleSize++; | |
} | |
return inSampleSize; | |
} | |
private static Bitmap createScaledBitmap(Bitmap src, int reqWidth, int reqHeight) | |
{ | |
int width = src.getWidth(); | |
int height = src.getHeight(); | |
float ratioHeight = (float) height / (float) reqHeight; | |
float ratioWidth = (float) width / (float) reqWidth; | |
int scaledWidth = width; | |
int scaledHeight = height; | |
if (ratioHeight > 1 || ratioWidth > 1) | |
{ | |
float maxRatio = Math.max(ratioHeight, ratioWidth); | |
scaledWidth = (int) (width / maxRatio); | |
scaledHeight = (int) (height / maxRatio); | |
} | |
return Bitmap.createScaledBitmap(src, scaledWidth, scaledHeight, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment