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)) {

View File

@@ -6,6 +6,9 @@ import java.io.InputStream;
import javax.annotation.Nullable;
public interface TestAvatarCreator {
String MIME_TYPE = "image/webp";
@Nullable
InputStream getAvatarInputStream() throws IOException;
}

View File

@@ -316,7 +316,7 @@ public class TestDataCreatorImpl implements TestDataCreator {
Message m;
try {
m = avatarMessageEncoder.encodeUpdateMessage(groupId, 0,
"image/jpeg", is).getFirst();
TestAvatarCreator.MIME_TYPE, is).getFirst();
} catch (IOException e) {
throw new DbException(e);
}