From 0e29554b975262c47772b8579a4344c68f6f22a6 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 24 Mar 2017 22:20:34 +0100 Subject: make image compression configurable --- src/main/java/de/pixart/messenger/Config.java | 2 -- .../pixart/messenger/persistance/FileBackend.java | 8 +++---- .../messenger/services/XmppConnectionService.java | 26 ++++++++++++++++++++++ src/main/res/values/arrays.xml | 12 ++++++++++ src/main/res/values/strings.xml | 3 +++ src/main/res/xml/preferences.xml | 8 +++---- 6 files changed, 49 insertions(+), 10 deletions(-) (limited to 'src/main') diff --git a/src/main/java/de/pixart/messenger/Config.java b/src/main/java/de/pixart/messenger/Config.java index 1b8290970..3bd5171ae 100644 --- a/src/main/java/de/pixart/messenger/Config.java +++ b/src/main/java/de/pixart/messenger/Config.java @@ -72,10 +72,8 @@ public final class Config { public static final int AVATAR_SIZE = 480; public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.JPEG; - public static final int IMAGE_SIZE = 3840; public static final Bitmap.CompressFormat IMAGE_FORMAT = Bitmap.CompressFormat.JPEG; public static final int IMAGE_QUALITY = 75; - public static final int IMAGE_MAX_SIZE = 1 * 1024 * 1024; // 1 MiB public static final int DEFAULT_ZOOM = 15; //for locations diff --git a/src/main/java/de/pixart/messenger/persistance/FileBackend.java b/src/main/java/de/pixart/messenger/persistance/FileBackend.java index 7f367f2b7..01a4070f1 100644 --- a/src/main/java/de/pixart/messenger/persistance/FileBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/FileBackend.java @@ -249,7 +249,7 @@ public class FileBackend { } File file = new File(path); long size = file.length(); - if (size == 0 || size >= Config.IMAGE_MAX_SIZE) { + if (size == 0 || size >= mXmppConnectionService.getCompressImageSizePreference()) { return false; } BitmapFactory.Options options = new BitmapFactory.Options(); @@ -259,7 +259,7 @@ public class FileBackend { if (options == null || options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) { return false; } - return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase())); + return (options.outWidth <= mXmppConnectionService.getCompressImageResolutionPreference() && options.outHeight <= mXmppConnectionService.getCompressImageResolutionPreference() && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase())); } catch (FileNotFoundException e) { return false; } @@ -370,7 +370,7 @@ public class FileBackend { if (originalBitmap == null) { throw new FileCopyException(R.string.error_not_an_image_file); } - Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE); + Bitmap scaledBitmap = resize(originalBitmap, mXmppConnectionService.getCompressImageResolutionPreference()); int rotation = getRotation(image); scaledBitmap = rotate(scaledBitmap, rotation); boolean targetSizeReached = false; @@ -382,7 +382,7 @@ public class FileBackend { throw new FileCopyException(R.string.error_compressing_image); } os.flush(); - targetSizeReached = file.length() <= Config.IMAGE_MAX_SIZE || quality <= 50; + targetSizeReached = file.length() <= mXmppConnectionService.getCompressImageSizePreference() || quality <= 50; quality -= 5; } scaledBitmap.recycle(); diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 936a4591f..6cfef225b 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -888,6 +888,32 @@ public class XmppConnectionService extends Service { return getPreferences().getString("picture_compression", "auto"); } + public int getCompressImageResolutionPreference() { + switch (getPreferences().getString("image_compression", "high")) { + case "low": + return 720; + case "mid": + return 1920; + case "high": + return 3840; + default: + return 3840; + } + } + + public int getCompressImageSizePreference() { + switch (getPreferences().getString("image_compression", "high")) { + case "low": + return 209715; // 0.2 * 1024 * 1024 = 209715 (0.2 MiB) + case "mid": + return 524288; // 0.5 * 1024 * 1024 = 524288 (0.5 MiB) + case "high": + return 1048576; // 1 * 1024 * 1024 = 1048576 (1 MiB) + default: + return 1048576; + } + } + private int getCompressVideoResolutionPreference() { switch (getPreferences().getString("video_compression", "high")) { case "low": diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml index b61a512ae..fd19b9442 100644 --- a/src/main/res/values/arrays.xml +++ b/src/main/res/values/arrays.xml @@ -70,6 +70,18 @@ @string/always + + low + mid + high + + + + @string/image_low + @string/image_mid + @string/image_high + + low mid diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index d1e053943..a54257398 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -764,4 +764,7 @@ Connect via Tor Tunnel all connections through the Tor network. Connection options + low (720p) + middle (1920p) + high (3840p) diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index bf0ee8ac8..daddaf6fa 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -125,10 +125,10 @@ android:summary="@string/pref_accept_files_summary_mobileroaming" android:title="@string/pref_accept_files_mobileroaming" />