mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-23 02:10:44 +01:00
Updated image compression parameters.
This commit is contained in:
committed by
Cody Henthorne
parent
3bdf2e7e2c
commit
236e1ba885
@@ -48,6 +48,11 @@ public class BitmapUtil {
|
||||
private static final int MIN_COMPRESSION_QUALITY_DECREASE = 5;
|
||||
private static final int MAX_IMAGE_HALF_SCALES = 3;
|
||||
|
||||
/**
|
||||
* @deprecated You probably want to use {@link ImageCompressionUtil} instead, which has a clearer
|
||||
* contract and handles mimetypes properly.
|
||||
*/
|
||||
@Deprecated
|
||||
@WorkerThread
|
||||
public static <T> ScaleResult createScaledBytes(@NonNull Context context, @NonNull T model, @NonNull MediaConstraints constraints)
|
||||
throws BitmapDecodingException
|
||||
@@ -58,6 +63,10 @@ public class BitmapUtil {
|
||||
constraints.getImageMaxSize(context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated You probably want to use {@link ImageCompressionUtil} instead, which has a clearer
|
||||
* contract and handles mimetypes properly.
|
||||
*/
|
||||
@WorkerThread
|
||||
public static <T> ScaleResult createScaledBytes(@NonNull Context context,
|
||||
@NonNull T model,
|
||||
@@ -69,6 +78,10 @@ public class BitmapUtil {
|
||||
return createScaledBytes(context, model, maxImageWidth, maxImageHeight, maxImageSize, CompressFormat.JPEG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated You probably want to use {@link ImageCompressionUtil} instead, which has a clearer
|
||||
* contract and handles mimetypes properly.
|
||||
*/
|
||||
@WorkerThread
|
||||
public static <T> ScaleResult createScaledBytes(Context context,
|
||||
T model,
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import androidx.annotation.IntRange;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public final class ImageCompressionUtil {
|
||||
|
||||
private ImageCompressionUtil () {}
|
||||
|
||||
/**
|
||||
* A result satisfying the provided constraints, or null if they could not be met.
|
||||
*/
|
||||
@WorkerThread
|
||||
public static @Nullable Result compressWithinConstraints(@NonNull Context context,
|
||||
@NonNull String mimeType,
|
||||
@NonNull Object glideModel,
|
||||
int maxDimension,
|
||||
int maxBytes,
|
||||
@IntRange(from = 0, to = 100) int quality)
|
||||
throws BitmapDecodingException
|
||||
{
|
||||
Result result = compress(context, mimeType, glideModel, maxDimension, quality);
|
||||
|
||||
if (result.getData().length <= maxBytes) {
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compresses the image to match the requested parameters.
|
||||
*/
|
||||
@WorkerThread
|
||||
public static @NonNull Result compress(@NonNull Context context,
|
||||
@NonNull String mimeType,
|
||||
@NonNull Object glideModel,
|
||||
int maxDimension,
|
||||
@IntRange(from = 0, to = 100) int quality)
|
||||
throws BitmapDecodingException
|
||||
{
|
||||
Bitmap scaledBitmap;
|
||||
|
||||
try {
|
||||
scaledBitmap = GlideApp.with(context.getApplicationContext())
|
||||
.asBitmap()
|
||||
.load(glideModel)
|
||||
.skipMemoryCache(true)
|
||||
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
||||
.centerInside()
|
||||
.submit(maxDimension, maxDimension)
|
||||
.get();
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
throw new BitmapDecodingException(e);
|
||||
}
|
||||
|
||||
if (scaledBitmap == null) {
|
||||
throw new BitmapDecodingException("Unable to decode image");
|
||||
}
|
||||
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
Bitmap.CompressFormat format = mimeTypeToCompressFormat(mimeType);
|
||||
scaledBitmap.compress(format, quality, output);
|
||||
|
||||
byte[] data = output.toByteArray();
|
||||
|
||||
return new Result(data, compressFormatToMimeType(format), scaledBitmap.getWidth(), scaledBitmap.getHeight());
|
||||
}
|
||||
|
||||
private static @NonNull Bitmap.CompressFormat mimeTypeToCompressFormat(@NonNull String mimeType) {
|
||||
if (MediaUtil.isJpegType(mimeType) || MediaUtil.isHeicType(mimeType) || MediaUtil.isHeifType(mimeType)) {
|
||||
return Bitmap.CompressFormat.JPEG;
|
||||
} else {
|
||||
return Bitmap.CompressFormat.PNG;
|
||||
}
|
||||
}
|
||||
|
||||
private static @NonNull String compressFormatToMimeType(@NonNull Bitmap.CompressFormat format) {
|
||||
switch (format) {
|
||||
case JPEG:
|
||||
return MediaUtil.IMAGE_JPEG;
|
||||
case PNG:
|
||||
return MediaUtil.IMAGE_PNG;
|
||||
default:
|
||||
throw new AssertionError("Unsupported format!");
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Result {
|
||||
private final byte[] data;
|
||||
private final String mimeType;
|
||||
private final int height;
|
||||
private final int width;
|
||||
|
||||
public Result(@NonNull byte[] data, @NonNull String mimeType, int width, int height) {
|
||||
this.data = data;
|
||||
this.mimeType = mimeType;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public @NonNull String getMimeType() {
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user