2013-04-26 20:23:43 +02:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2014-01-08 23:29:05 +01:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.PorterDuffXfermode;
|
|
|
|
import android.graphics.Rect;
|
2013-04-26 20:23:43 +02:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
public class BitmapUtil {
|
2014-09-03 22:08:02 +02:00
|
|
|
private static final String TAG = BitmapUtil.class.getSimpleName();
|
2013-04-26 20:23:43 +02:00
|
|
|
|
|
|
|
private static final int MAX_COMPRESSION_QUALITY = 95;
|
|
|
|
private static final int MIN_COMPRESSION_QUALITY = 50;
|
|
|
|
private static final int MAX_COMPRESSION_ATTEMPTS = 4;
|
|
|
|
|
|
|
|
public static byte[] createScaledBytes(Context context, Uri uri, int maxWidth,
|
|
|
|
int maxHeight, int maxSize)
|
2013-05-21 19:32:48 +02:00
|
|
|
throws IOException, BitmapDecodingException
|
2013-04-26 20:23:43 +02:00
|
|
|
{
|
2014-09-03 22:08:02 +02:00
|
|
|
Bitmap bitmap;
|
|
|
|
try {
|
|
|
|
bitmap = createScaledBitmap(context.getContentResolver().openInputStream(uri),
|
|
|
|
context.getContentResolver().openInputStream(uri),
|
|
|
|
maxWidth, maxHeight, false);
|
|
|
|
} catch(OutOfMemoryError oome) {
|
|
|
|
Log.w(TAG, "OutOfMemoryError when scaling precisely, doing rough scale to save memory instead");
|
|
|
|
bitmap = createScaledBitmap(context.getContentResolver().openInputStream(uri),
|
|
|
|
context.getContentResolver().openInputStream(uri),
|
|
|
|
maxWidth, maxHeight, true);
|
|
|
|
}
|
2013-04-26 20:23:43 +02:00
|
|
|
int quality = MAX_COMPRESSION_QUALITY;
|
|
|
|
int attempts = 0;
|
|
|
|
|
|
|
|
ByteArrayOutputStream baos;
|
|
|
|
|
|
|
|
do {
|
|
|
|
baos = new ByteArrayOutputStream();
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
|
|
|
|
|
|
|
|
quality = Math.max((quality * maxSize) / baos.size(), MIN_COMPRESSION_QUALITY);
|
|
|
|
} while (baos.size() > maxSize && attempts++ < MAX_COMPRESSION_ATTEMPTS);
|
|
|
|
|
|
|
|
bitmap.recycle();
|
|
|
|
|
|
|
|
if (baos.size() <= maxSize) return baos.toByteArray();
|
|
|
|
else throw new IOException("Unable to scale image below: " + baos.size());
|
|
|
|
}
|
|
|
|
|
2014-05-29 05:53:34 +02:00
|
|
|
public static Bitmap createScaledBitmap(InputStream measure, InputStream data, float scale)
|
|
|
|
throws BitmapDecodingException
|
|
|
|
{
|
|
|
|
final BitmapFactory.Options options = getImageDimensions(measure);
|
|
|
|
final int outWidth = (int)(options.outWidth * scale);
|
|
|
|
final int outHeight = (int)(options.outHeight * scale);
|
2014-09-03 22:08:02 +02:00
|
|
|
Log.w(TAG, "creating scaled bitmap with scale " + scale + " => " + outWidth + "x" + outHeight);
|
|
|
|
return createScaledBitmap(data, outWidth, outHeight, options, false);
|
2014-05-29 05:53:34 +02:00
|
|
|
}
|
|
|
|
|
2013-04-26 20:23:43 +02:00
|
|
|
public static Bitmap createScaledBitmap(InputStream measure, InputStream data,
|
2014-09-03 22:08:02 +02:00
|
|
|
int maxWidth, int maxHeight)
|
|
|
|
throws BitmapDecodingException
|
|
|
|
{
|
|
|
|
return createScaledBitmap(measure, data, maxWidth, maxHeight, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Bitmap createScaledBitmap(InputStream measure, InputStream data,
|
|
|
|
int maxWidth, int maxHeight, boolean constrainedMemory)
|
2014-05-29 05:53:34 +02:00
|
|
|
throws BitmapDecodingException
|
|
|
|
{
|
|
|
|
final BitmapFactory.Options options = getImageDimensions(measure);
|
2014-09-03 22:08:02 +02:00
|
|
|
return createScaledBitmap(data, maxWidth, maxHeight, options, constrainedMemory);
|
2014-05-29 05:53:34 +02:00
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:02 +02:00
|
|
|
private static Bitmap createScaledBitmap(InputStream data, int maxWidth, int maxHeight,
|
|
|
|
BitmapFactory.Options options, boolean constrainedMemory)
|
2013-05-21 19:32:48 +02:00
|
|
|
throws BitmapDecodingException
|
2013-04-26 20:23:43 +02:00
|
|
|
{
|
2014-05-29 05:53:34 +02:00
|
|
|
final int imageWidth = options.outWidth;
|
|
|
|
final int imageHeight = options.outHeight;
|
2013-04-26 20:23:43 +02:00
|
|
|
|
|
|
|
int scaler = 1;
|
2014-09-03 22:08:02 +02:00
|
|
|
int scaleFactor = (constrainedMemory ? 1 : 2);
|
|
|
|
while ((imageWidth / scaler / scaleFactor >= maxWidth) && (imageHeight / scaler / scaleFactor >= maxHeight)) {
|
2013-04-26 20:23:43 +02:00
|
|
|
scaler *= 2;
|
2014-09-03 22:08:02 +02:00
|
|
|
}
|
2013-04-26 20:23:43 +02:00
|
|
|
|
|
|
|
options.inSampleSize = scaler;
|
|
|
|
options.inJustDecodeBounds = false;
|
|
|
|
|
2014-09-03 22:08:02 +02:00
|
|
|
FlushedInputStream is = new FlushedInputStream(data);
|
|
|
|
Bitmap roughThumbnail = BitmapFactory.decodeStream(is, null, options);
|
|
|
|
try {
|
|
|
|
is.close();
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, "IOException thrown when closing an images InputStream", ioe);
|
|
|
|
}
|
|
|
|
Log.w(TAG, "rough scale " + (imageWidth) + "x" + (imageHeight) +
|
|
|
|
" => " + (options.outWidth) + "x" + (options.outHeight));
|
2013-05-21 19:32:48 +02:00
|
|
|
if (roughThumbnail == null) {
|
|
|
|
throw new BitmapDecodingException("Decoded stream was null.");
|
|
|
|
}
|
2014-09-03 22:08:02 +02:00
|
|
|
if (constrainedMemory) {
|
|
|
|
return roughThumbnail;
|
|
|
|
}
|
2013-05-21 19:32:48 +02:00
|
|
|
|
2014-05-29 05:53:34 +02:00
|
|
|
if (options.outWidth > maxWidth || options.outHeight > maxHeight) {
|
|
|
|
final float aspectWidth, aspectHeight;
|
2013-10-01 06:56:50 +02:00
|
|
|
|
|
|
|
if (imageWidth == 0 || imageHeight == 0) {
|
|
|
|
aspectWidth = maxWidth;
|
|
|
|
aspectHeight = maxHeight;
|
|
|
|
} else if (options.outWidth >= options.outHeight) {
|
|
|
|
aspectWidth = maxWidth;
|
|
|
|
aspectHeight = (aspectWidth / options.outWidth) * options.outHeight;
|
|
|
|
} else {
|
|
|
|
aspectHeight = maxHeight;
|
|
|
|
aspectWidth = (aspectHeight / options.outHeight) * options.outWidth;
|
|
|
|
}
|
|
|
|
|
2014-09-03 22:08:02 +02:00
|
|
|
Log.w(TAG, "fine scale " + options.outWidth + "x" + options.outHeight +
|
|
|
|
" => " + aspectWidth + "x" + aspectHeight);
|
|
|
|
Bitmap scaledThumbnail = null;
|
|
|
|
try {
|
|
|
|
scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, (int) aspectWidth, (int) aspectHeight, true);
|
|
|
|
} finally {
|
|
|
|
if (roughThumbnail != scaledThumbnail) roughThumbnail.recycle();
|
|
|
|
}
|
2013-04-26 20:23:43 +02:00
|
|
|
return scaledThumbnail;
|
|
|
|
} else {
|
|
|
|
return roughThumbnail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static BitmapFactory.Options getImageDimensions(InputStream inputStream) {
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
options.inJustDecodeBounds = true;
|
2014-09-03 22:08:02 +02:00
|
|
|
FlushedInputStream fis = new FlushedInputStream(inputStream);
|
|
|
|
BitmapFactory.decodeStream(fis, null, options);
|
|
|
|
try {
|
|
|
|
fis.close();
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, "failed to close the InputStream after reading image dimensions");
|
|
|
|
}
|
2013-04-26 20:23:43 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2014-02-19 07:08:24 +01:00
|
|
|
public static Bitmap getCircleCroppedBitmap(Bitmap bitmap) {
|
2014-02-24 08:30:43 +01:00
|
|
|
if (bitmap == null) return null;
|
2014-03-03 04:30:50 +01:00
|
|
|
final int srcSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
|
|
|
|
return getScaledCircleCroppedBitmap(bitmap, srcSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Bitmap getScaledCircleCroppedBitmap(Bitmap bitmap, int destSize) {
|
|
|
|
if (bitmap == null) return null;
|
|
|
|
Bitmap output = Bitmap.createBitmap(destSize, destSize, Bitmap.Config.ARGB_8888);
|
2014-01-08 23:29:05 +01:00
|
|
|
Canvas canvas = new Canvas(output);
|
|
|
|
|
2014-03-03 04:30:50 +01:00
|
|
|
final int srcSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
|
|
|
|
final int srcX = (bitmap.getWidth() - srcSize) / 2;
|
|
|
|
final int srcY = (bitmap.getHeight() - srcSize) / 2;
|
|
|
|
final Rect srcRect = new Rect(srcX, srcY, srcX + srcSize, srcY + srcSize);
|
|
|
|
final Rect destRect = new Rect(0, 0, destSize, destSize);
|
2014-01-08 23:29:05 +01:00
|
|
|
final int color = 0xff424242;
|
|
|
|
final Paint paint = new Paint();
|
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas.drawARGB(0, 0, 0, 0);
|
|
|
|
paint.setColor(color);
|
2014-03-03 04:30:50 +01:00
|
|
|
canvas.drawCircle(destSize / 2, destSize / 2, destSize / 2, paint);
|
2014-01-08 23:29:05 +01:00
|
|
|
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
2014-03-03 04:30:50 +01:00
|
|
|
canvas.drawBitmap(bitmap, srcRect, destRect, paint);
|
2014-01-08 23:29:05 +01:00
|
|
|
return output;
|
|
|
|
}
|
2013-04-26 20:23:43 +02:00
|
|
|
|
2014-01-14 09:26:43 +01:00
|
|
|
public static byte[] toByteArray(Bitmap bitmap) {
|
|
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
|
|
|
return stream.toByteArray();
|
|
|
|
}
|
2013-04-26 20:23:43 +02:00
|
|
|
}
|