This commit is contained in:
Moxie Marlinspike
2018-02-07 14:01:37 -08:00
parent 8bec5a96f5
commit d567534609
72 changed files with 1164 additions and 505 deletions

View File

@@ -44,9 +44,19 @@ public class BitmapUtil {
private static final int MAX_COMPRESSION_ATTEMPTS = 5;
private static final int MIN_COMPRESSION_QUALITY_DECREASE = 5;
@android.support.annotation.WorkerThread
@WorkerThread
public static <T> ScaleResult createScaledBytes(Context context, T model, MediaConstraints constraints)
throws BitmapDecodingException
{
return createScaledBytes(context, model,
constraints.getImageMaxWidth(context),
constraints.getImageMaxHeight(context),
constraints.getImageMaxSize(context));
}
@WorkerThread
public static <T> ScaleResult createScaledBytes(Context context, T model, int maxImageWidth, int maxImageHeight, int maxImageSize)
throws BitmapDecodingException
{
try {
int quality = MAX_COMPRESSION_QUALITY;
@@ -59,8 +69,7 @@ public class BitmapUtil {
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.downsample(DownsampleStrategy.AT_MOST)
.submit(constraints.getImageMaxWidth(context),
constraints.getImageMaxWidth(context))
.submit(maxImageWidth, maxImageHeight)
.get();
if (scaledBitmap == null) {
@@ -76,14 +85,14 @@ public class BitmapUtil {
Log.w(TAG, "iteration with quality " + quality + " size " + (bytes.length / 1024) + "kb");
if (quality == MIN_COMPRESSION_QUALITY) break;
int nextQuality = (int)Math.floor(quality * Math.sqrt((double)constraints.getImageMaxSize(context) / bytes.length));
int nextQuality = (int)Math.floor(quality * Math.sqrt((double)maxImageSize / bytes.length));
if (quality - nextQuality < MIN_COMPRESSION_QUALITY_DECREASE) {
nextQuality = quality - MIN_COMPRESSION_QUALITY_DECREASE;
}
quality = Math.max(nextQuality, MIN_COMPRESSION_QUALITY);
}
while (bytes.length > constraints.getImageMaxSize(context) && attempts++ < MAX_COMPRESSION_ATTEMPTS);
if (bytes.length > constraints.getImageMaxSize(context)) {
while (bytes.length > maxImageSize && attempts++ < MAX_COMPRESSION_ATTEMPTS);
if (bytes.length > maxImageSize) {
throw new BitmapDecodingException("Unable to scale image below: " + bytes.length);
}
Log.w(TAG, "createScaledBytes(" + model.toString() + ") -> quality " + Math.min(quality, MAX_COMPRESSION_QUALITY) + ", " + attempts + " attempt(s)");

View File

@@ -3,6 +3,9 @@ package org.thoughtcrime.securesms.util;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -39,4 +42,30 @@ public class JsonUtils {
public static ObjectMapper getMapper() {
return objectMapper;
}
public static class SaneJSONObject {
private final JSONObject delegate;
public SaneJSONObject(JSONObject delegate) {
this.delegate = delegate;
}
public String getString(String name) throws JSONException {
if (delegate.isNull(name)) return null;
else return delegate.getString(name);
}
public long getLong(String name) throws JSONException {
return delegate.getLong(name);
}
public boolean isNull(String name) {
return delegate.isNull(name);
}
public int getInt(String name) throws JSONException {
return delegate.getInt(name);
}
}
}