Use WebP for compressing images.

This commit is contained in:
akwizgran
2021-05-21 12:38:57 +01:00
parent aabb0bfb4a
commit a5972e26fe
5 changed files with 10 additions and 9 deletions

View File

@@ -102,12 +102,10 @@ class AttachmentCreationTask {
}
InputStream is = contentResolver.openInputStream(uri);
if (is == null) throw new IOException();
is = imageCompressor
.compressImage(is, contentType);
is = imageCompressor.compressImage(is, contentType);
long timestamp = System.currentTimeMillis();
AttachmentHeader h = messagingManager
.addLocalAttachment(groupId, timestamp,
ImageCompressor.MIME_TYPE, is);
AttachmentHeader h = messagingManager.addLocalAttachment(groupId,
timestamp, ImageCompressor.MIME_TYPE, is);
tryToClose(is, LOG, WARNING);
logDuration(LOG, "Storing attachment", start);
return h;

View File

@@ -11,7 +11,7 @@ public interface ImageCompressor {
/**
* The MIME type of compressed images
*/
String MIME_TYPE = "image/jpeg";
String MIME_TYPE = "image/webp";
/**
* Load an image from {@code is}, compress it and return an InputStream

View File

@@ -12,7 +12,7 @@ import java.util.logging.Logger;
import javax.inject.Inject;
import static android.graphics.Bitmap.CompressFormat.JPEG;
import static android.graphics.Bitmap.CompressFormat.WEBP;
import static android.graphics.BitmapFactory.decodeStream;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
@@ -50,7 +50,7 @@ class ImageCompressorImpl implements ImageCompressor {
public InputStream compressImage(Bitmap bitmap) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int quality = 100; quality >= 0; quality -= 10) {
if (!bitmap.compress(JPEG, quality, out))
if (!bitmap.compress(WEBP, quality, out))
throw new IOException();
if (out.size() <= MAX_IMAGE_SIZE) {
if (LOG.isLoggable(INFO)) {