2013-04-26 11:23:43 -07:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
2014-12-30 01:36:51 -08:00
|
|
|
import android.graphics.Bitmap.CompressFormat;
|
2013-04-26 11:23:43 -07:00
|
|
|
import android.graphics.BitmapFactory;
|
2014-01-08 12:29:05 -10:00
|
|
|
import android.graphics.Canvas;
|
2015-04-09 23:10:19 -07:00
|
|
|
import android.graphics.Color;
|
2014-08-18 14:45:31 +02:00
|
|
|
import android.graphics.Matrix;
|
2014-01-08 12:29:05 -10:00
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.PorterDuffXfermode;
|
|
|
|
import android.graphics.Rect;
|
2015-04-09 23:10:19 -07:00
|
|
|
import android.graphics.RectF;
|
2015-05-04 11:36:18 -07:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2013-04-26 11:23:43 -07:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
2014-12-17 11:47:19 -08:00
|
|
|
import android.util.Pair;
|
|
|
|
|
2015-04-09 23:10:19 -07:00
|
|
|
import com.android.gallery3d.data.Exif;
|
|
|
|
|
2014-12-17 11:47:19 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2013-04-26 11:23:43 -07:00
|
|
|
|
2014-09-30 12:53:34 -07:00
|
|
|
import java.io.BufferedInputStream;
|
2014-12-30 01:36:51 -08:00
|
|
|
import java.io.ByteArrayInputStream;
|
2013-04-26 11:23:43 -07:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
public class BitmapUtil {
|
2014-09-03 15:08:02 -05:00
|
|
|
private static final String TAG = BitmapUtil.class.getSimpleName();
|
2013-04-26 11:23:43 -07: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;
|
|
|
|
|
2014-08-18 14:45:31 +02:00
|
|
|
public static byte[] createScaledBytes(Context context, MasterSecret masterSecret, Uri uri, int maxWidth, int maxHeight, int maxSize)
|
2013-05-21 10:32:48 -07:00
|
|
|
throws IOException, BitmapDecodingException
|
2013-04-26 11:23:43 -07:00
|
|
|
{
|
2014-09-03 15:08:02 -05:00
|
|
|
Bitmap bitmap;
|
|
|
|
try {
|
2014-08-18 14:45:31 +02:00
|
|
|
bitmap = createScaledBitmap(context, masterSecret, uri, maxWidth, maxHeight, false);
|
2014-09-03 15:08:02 -05:00
|
|
|
} catch(OutOfMemoryError oome) {
|
|
|
|
Log.w(TAG, "OutOfMemoryError when scaling precisely, doing rough scale to save memory instead");
|
2014-08-18 14:45:31 +02:00
|
|
|
bitmap = createScaledBitmap(context, masterSecret, uri, maxWidth, maxHeight, true);
|
2014-09-03 15:08:02 -05:00
|
|
|
}
|
2013-04-26 11:23:43 -07: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);
|
|
|
|
|
2015-01-02 15:43:28 -08:00
|
|
|
Log.w(TAG, "createScaledBytes(" + uri + ") -> quality " + Math.min(quality, MAX_COMPRESSION_QUALITY) + ", " + attempts + " attempt(s)");
|
|
|
|
|
2013-04-26 11:23:43 -07:00
|
|
|
bitmap.recycle();
|
|
|
|
|
|
|
|
if (baos.size() <= maxSize) return baos.toByteArray();
|
|
|
|
else throw new IOException("Unable to scale image below: " + baos.size());
|
|
|
|
}
|
|
|
|
|
2014-08-18 14:45:31 +02:00
|
|
|
public static Bitmap createScaledBitmap(Context context, MasterSecret masterSecret, Uri uri, int maxWidth, int maxHeight)
|
2014-12-30 01:36:51 -08:00
|
|
|
throws BitmapDecodingException, IOException
|
2014-08-18 14:45:31 +02:00
|
|
|
{
|
2014-12-29 16:40:37 -08:00
|
|
|
Bitmap bitmap;
|
|
|
|
try {
|
|
|
|
bitmap = createScaledBitmap(context, masterSecret, uri, maxWidth, maxHeight, false);
|
|
|
|
} catch(OutOfMemoryError oome) {
|
|
|
|
Log.w(TAG, "OutOfMemoryError when scaling precisely, doing rough scale to save memory instead");
|
|
|
|
bitmap = createScaledBitmap(context, masterSecret, uri, maxWidth, maxHeight, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
2014-08-18 14:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Bitmap createScaledBitmap(Context context, MasterSecret masterSecret, Uri uri, int maxWidth, int maxHeight, boolean constrainedMemory)
|
2014-12-30 01:36:51 -08:00
|
|
|
throws IOException, BitmapDecodingException
|
2014-08-18 14:45:31 +02:00
|
|
|
{
|
2015-02-11 18:15:50 -08:00
|
|
|
InputStream is = PartAuthority.getPartStream(context, masterSecret, uri);
|
|
|
|
if (is == null) throw new IOException("Couldn't obtain InputStream");
|
|
|
|
return createScaledBitmap(is,
|
2014-08-18 14:45:31 +02:00
|
|
|
PartAuthority.getPartStream(context, masterSecret, uri),
|
|
|
|
PartAuthority.getPartStream(context, masterSecret, uri),
|
|
|
|
maxWidth, maxHeight, constrainedMemory);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Bitmap createScaledBitmap(InputStream measure, InputStream orientationStream, InputStream data,
|
|
|
|
int maxWidth, int maxHeight, boolean constrainedMemory)
|
|
|
|
throws BitmapDecodingException
|
|
|
|
{
|
|
|
|
Bitmap bitmap = createScaledBitmap(measure, data, maxWidth, maxHeight, constrainedMemory);
|
|
|
|
return fixOrientation(bitmap, orientationStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Bitmap createScaledBitmap(InputStream measure, InputStream data, int maxWidth, int maxHeight,
|
|
|
|
boolean constrainedMemory)
|
|
|
|
throws BitmapDecodingException
|
|
|
|
{
|
|
|
|
final BitmapFactory.Options options = getImageDimensions(measure);
|
|
|
|
return createScaledBitmap(data, maxWidth, maxHeight, options, constrainedMemory);
|
|
|
|
}
|
|
|
|
|
2014-05-28 20:53:34 -07: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 15:08:02 -05:00
|
|
|
Log.w(TAG, "creating scaled bitmap with scale " + scale + " => " + outWidth + "x" + outHeight);
|
|
|
|
return createScaledBitmap(data, outWidth, outHeight, options, false);
|
2014-05-28 20:53:34 -07:00
|
|
|
}
|
|
|
|
|
2014-08-18 14:45:31 +02:00
|
|
|
public static Bitmap createScaledBitmap(InputStream measure, InputStream data, int maxWidth, int maxHeight)
|
2014-09-03 15:08:02 -05:00
|
|
|
throws BitmapDecodingException
|
|
|
|
{
|
|
|
|
return createScaledBitmap(measure, data, maxWidth, maxHeight, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Bitmap createScaledBitmap(InputStream data, int maxWidth, int maxHeight,
|
|
|
|
BitmapFactory.Options options, boolean constrainedMemory)
|
2013-05-21 10:32:48 -07:00
|
|
|
throws BitmapDecodingException
|
2013-04-26 11:23:43 -07:00
|
|
|
{
|
2014-05-28 20:53:34 -07:00
|
|
|
final int imageWidth = options.outWidth;
|
|
|
|
final int imageHeight = options.outHeight;
|
2013-04-26 11:23:43 -07:00
|
|
|
|
|
|
|
int scaler = 1;
|
2014-09-03 15:08:02 -05:00
|
|
|
int scaleFactor = (constrainedMemory ? 1 : 2);
|
|
|
|
while ((imageWidth / scaler / scaleFactor >= maxWidth) && (imageHeight / scaler / scaleFactor >= maxHeight)) {
|
2013-04-26 11:23:43 -07:00
|
|
|
scaler *= 2;
|
2014-09-03 15:08:02 -05:00
|
|
|
}
|
2013-04-26 11:23:43 -07:00
|
|
|
|
|
|
|
options.inSampleSize = scaler;
|
|
|
|
options.inJustDecodeBounds = false;
|
|
|
|
|
2014-09-30 12:53:34 -07:00
|
|
|
BufferedInputStream is = new BufferedInputStream(data);
|
|
|
|
Bitmap roughThumbnail = BitmapFactory.decodeStream(is, null, options);
|
2014-09-03 15:08:02 -05:00
|
|
|
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 10:32:48 -07:00
|
|
|
if (roughThumbnail == null) {
|
|
|
|
throw new BitmapDecodingException("Decoded stream was null.");
|
|
|
|
}
|
2014-09-03 15:08:02 -05:00
|
|
|
if (constrainedMemory) {
|
|
|
|
return roughThumbnail;
|
|
|
|
}
|
2013-05-21 10:32:48 -07:00
|
|
|
|
2014-05-28 20:53:34 -07:00
|
|
|
if (options.outWidth > maxWidth || options.outHeight > maxHeight) {
|
|
|
|
final float aspectWidth, aspectHeight;
|
2013-09-30 23:56:50 -05: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-12-17 11:47:19 -08:00
|
|
|
final int fineWidth = Math.round(aspectWidth);
|
|
|
|
final int fineHeight = Math.round(aspectHeight);
|
|
|
|
|
|
|
|
Log.w(TAG, "fine scale " + options.outWidth + "x" + options.outHeight +
|
|
|
|
" => " + fineWidth + "x" + fineHeight);
|
2014-09-03 15:08:02 -05:00
|
|
|
Bitmap scaledThumbnail = null;
|
|
|
|
try {
|
2014-12-17 11:47:19 -08:00
|
|
|
scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, fineWidth, fineHeight, true);
|
2014-09-03 15:08:02 -05:00
|
|
|
} finally {
|
|
|
|
if (roughThumbnail != scaledThumbnail) roughThumbnail.recycle();
|
|
|
|
}
|
2013-04-26 11:23:43 -07:00
|
|
|
return scaledThumbnail;
|
|
|
|
} else {
|
|
|
|
return roughThumbnail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 14:45:31 +02:00
|
|
|
private static Bitmap fixOrientation(Bitmap bitmap, InputStream orientationStream) {
|
|
|
|
final int orientation = Exif.getOrientation(orientationStream);
|
|
|
|
|
|
|
|
if (orientation != 0) {
|
|
|
|
return rotateBitmap(bitmap, orientation);
|
|
|
|
} else {
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
|
|
|
|
Matrix matrix = new Matrix();
|
|
|
|
matrix.postRotate(angle);
|
|
|
|
Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
|
|
|
if (rotated != bitmap) bitmap.recycle();
|
|
|
|
return rotated;
|
|
|
|
}
|
|
|
|
|
2013-04-26 11:23:43 -07:00
|
|
|
private static BitmapFactory.Options getImageDimensions(InputStream inputStream) {
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
options.inJustDecodeBounds = true;
|
2014-09-30 12:53:34 -07:00
|
|
|
BufferedInputStream fis = new BufferedInputStream(inputStream);
|
2014-09-03 15:08:02 -05:00
|
|
|
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 11:23:43 -07:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2015-01-02 15:43:28 -08:00
|
|
|
public static Pair<Integer, Integer> getDimensions(InputStream inputStream) {
|
|
|
|
BitmapFactory.Options options = getImageDimensions(inputStream);
|
|
|
|
return new Pair<>(options.outWidth, options.outHeight);
|
|
|
|
}
|
|
|
|
|
2014-12-30 01:36:51 -08:00
|
|
|
public static InputStream toCompressedJpeg(Bitmap bitmap) {
|
|
|
|
ByteArrayOutputStream thumbnailBytes = new ByteArrayOutputStream();
|
|
|
|
bitmap.compress(CompressFormat.JPEG, 85, thumbnailBytes);
|
|
|
|
return new ByteArrayInputStream(thumbnailBytes.toByteArray());
|
|
|
|
}
|
|
|
|
|
2014-01-14 00:26:43 -08:00
|
|
|
public static byte[] toByteArray(Bitmap bitmap) {
|
|
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
|
|
|
return stream.toByteArray();
|
|
|
|
}
|
2015-04-09 23:10:19 -07:00
|
|
|
|
|
|
|
public static Bitmap getCircleBitmap(Bitmap bitmap) {
|
|
|
|
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
|
|
|
|
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
final Canvas canvas = new Canvas(output);
|
|
|
|
|
|
|
|
final int color = Color.RED;
|
|
|
|
final Paint paint = new Paint();
|
|
|
|
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
|
|
|
|
final RectF rectF = new RectF(rect);
|
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas.drawARGB(0, 0, 0, 0);
|
|
|
|
paint.setColor(color);
|
|
|
|
canvas.drawOval(rectF, paint);
|
|
|
|
|
|
|
|
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
|
|
|
canvas.drawBitmap(bitmap, rect, rect, paint);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-05-04 11:36:18 -07:00
|
|
|
public static Bitmap createFromDrawable(Drawable drawable) {
|
|
|
|
if (drawable instanceof BitmapDrawable) {
|
|
|
|
return ((BitmapDrawable)drawable).getBitmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = drawable.getIntrinsicWidth();
|
|
|
|
width = width > 0 ? width : 1;
|
|
|
|
|
|
|
|
int height = drawable.getIntrinsicHeight();
|
|
|
|
height = height > 0 ? height : 1;
|
|
|
|
|
|
|
|
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas canvas = new Canvas(bitmap);
|
|
|
|
drawable.draw(canvas);
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2013-04-26 11:23:43 -07:00
|
|
|
}
|