Created
February 12, 2013 23:21
-
-
Save DHuckaby/4774437 to your computer and use it in GitHub Desktop.
PhotupBitmapOptionsFactory.java
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 PhotupBitmapOptionsFactory implements BitmapOptionsFactory { | |
private static int MAX_DIMEN = 512; | |
@Override | |
public Options newOptionsFromStream(InputStream inputStream, int width, int height) { | |
BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options(); | |
final int maxDim; | |
if (height > 0 && width > 0) { | |
maxDim = Math.max(height, width); | |
} else { | |
maxDim = MAX_DIMEN; | |
} | |
bitmapFactoryOptions.inJustDecodeBounds = true; | |
try { | |
BitmapFactory.decodeStream(inputStream, null, bitmapFactoryOptions); | |
} catch (SecurityException ignore) {} | |
final int origWidth = bitmapFactoryOptions.outWidth; | |
final int origHeight = bitmapFactoryOptions.outHeight; | |
bitmapFactoryOptions.inJustDecodeBounds = false; | |
bitmapFactoryOptions.inScaled = false; | |
bitmapFactoryOptions.inPurgeable = true; | |
bitmapFactoryOptions.inInputShareable = true; | |
bitmapFactoryOptions.inDither = true; | |
bitmapFactoryOptions.inPreferredConfig = Bitmap.Config.RGB_565; | |
if (origWidth > maxDim || origHeight > maxDim) { | |
int k = 1; | |
int tmpHeight = origHeight, tmpWidth = origWidth; | |
while ((tmpWidth / 2) >= maxDim || (tmpHeight / 2) >= maxDim) { | |
tmpWidth /= 2; | |
tmpHeight /= 2; | |
k *= 2; | |
} | |
bitmapFactoryOptions.inSampleSize = k; | |
} | |
return bitmapFactoryOptions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment