mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-23 20:03:47 +00:00
replies
This commit is contained in:
@@ -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)");
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user