Compare commits

...

4 Commits

Author SHA1 Message Date
akwizgran
7345d5c22c Add WebP library for API 16. 2021-05-24 15:09:19 +01:00
akwizgran
b4a2725f03 Add workarounds for missing MIME type information on API 16. 2021-05-24 14:56:50 +01:00
akwizgran
a5972e26fe Use WebP for compressing images. 2021-05-21 12:43:50 +01:00
akwizgran
aabb0bfb4a Allow WebP images to be used as attachments and avatars. 2021-05-21 12:43:19 +01:00
10 changed files with 34 additions and 11 deletions

View File

@@ -119,7 +119,12 @@ public class AndroidUtils {
* Returns an array of supported content types for image attachments. * Returns an array of supported content types for image attachments.
*/ */
public static String[] getSupportedImageContentTypes() { public static String[] getSupportedImageContentTypes() {
return new String[] {"image/jpeg", "image/png", "image/gif"}; return new String[] {
"image/jpeg",
"image/png",
"image/gif",
"image/webp"
};
} }
@Nullable @Nullable

View File

@@ -121,6 +121,7 @@ dependencies {
exclude group: 'com.android.support' exclude group: 'com.android.support'
exclude module: 'disklrucache' // when there's no disk cache, we can't accidentally use it exclude module: 'disklrucache' // when there's no disk cache, we can't accidentally use it
} }
implementation "com.github.zjupure:webpdecoder:2.0.$glideVersion"
annotationProcessor 'com.google.dagger:dagger-compiler:2.24' annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion" annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"

View File

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

View File

@@ -11,7 +11,7 @@ public interface ImageCompressor {
/** /**
* The MIME type of compressed images * 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 * 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 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 android.graphics.BitmapFactory.decodeStream;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
@@ -50,7 +50,7 @@ class ImageCompressorImpl implements ImageCompressor {
public InputStream compressImage(Bitmap bitmap) throws IOException { public InputStream compressImage(Bitmap bitmap) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int quality = 100; quality >= 0; quality -= 10) { for (int quality = 100; quality >= 0; quality -= 10) {
if (!bitmap.compress(JPEG, quality, out)) if (!bitmap.compress(WEBP, quality, out))
throw new IOException(); throw new IOException();
if (out.size() <= MAX_IMAGE_SIZE) { if (out.size() <= MAX_IMAGE_SIZE) {
if (LOG.isLoggable(INFO)) { if (LOG.isLoggable(INFO)) {

View File

@@ -3,6 +3,8 @@ package org.briarproject.briar.android.attachment.media;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import com.bumptech.glide.integration.webp.WebpBitmapFactory;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.InputStream; import java.io.InputStream;
@@ -25,6 +27,10 @@ class ImageHelperImpl implements ImageHelper {
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options); BitmapFactory.decodeStream(is, null, options);
if (options.outWidth < 1 || options.outHeight < 1) {
// BitmapFactory doesn't fully support WebP on API < 17
WebpBitmapFactory.decodeStream(is, null, options);
}
String mimeType = options.outMimeType; String mimeType = options.outMimeType;
if (mimeType == null) mimeType = ""; if (mimeType == null) mimeType = "";
return new DecodeResult(options.outWidth, options.outHeight, return new DecodeResult(options.outWidth, options.outHeight,
@@ -34,6 +40,7 @@ class ImageHelperImpl implements ImageHelper {
@Nullable @Nullable
@Override @Override
public String getExtensionFromMimeType(String mimeType) { public String getExtensionFromMimeType(String mimeType) {
if ("image/webp".equals(mimeType)) return "webp";
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(mimeType); return mimeTypeMap.getExtensionFromMimeType(mimeType);
} }

View File

@@ -21,6 +21,7 @@ import static androidx.exifinterface.media.ExifInterface.TAG_ORIENTATION;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger; import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
@NotNullByDefault @NotNullByDefault
class ImageSizeCalculatorImpl implements ImageSizeCalculator { class ImageSizeCalculatorImpl implements ImageSizeCalculator {
@@ -61,6 +62,10 @@ class ImageSizeCalculatorImpl implements ImageSizeCalculator {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
} }
} }
if (!size.hasError() && isNullOrEmpty(size.getMimeType())) {
LOG.info("Could not determine content type, using supplied type");
size = new Size(size.getWidth(), size.getHeight(), contentType);
}
return size; return size;
} }

View File

@@ -4,6 +4,8 @@ import android.graphics.Bitmap;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import com.bumptech.glide.integration.webp.decoder.WebpDrawable;
import com.bumptech.glide.integration.webp.decoder.WebpDrawableTransformation;
import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.Transformation;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -80,7 +82,9 @@ class ImageViewHolder extends ViewHolder {
.load(a.getHeader()) .load(a.getHeader())
.diskCacheStrategy(NONE) .diskCacheStrategy(NONE)
.error(ERROR_RES) .error(ERROR_RES)
.transform(transformation) .optionalTransform(transformation)
.optionalTransform(WebpDrawable.class,
new WebpDrawableTransformation(transformation))
.transition(withCrossFade()) .transition(withCrossFade())
.into(imageView) .into(imageView)
.waitForLayout(); .waitForLayout();

View File

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

View File

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