mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
scale down images on sending, closes #168
This commit is contained in:
@@ -97,6 +97,7 @@ import org.thoughtcrime.securesms.permissions.Permissions;
|
||||
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.scribbles.ScribbleActivity;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
||||
import org.thoughtcrime.securesms.util.DynamicTheme;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
@@ -871,14 +872,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
||||
filename = filename.substring(0, i);
|
||||
}
|
||||
}
|
||||
String path = null;
|
||||
for (int i=0; i<1000; i++) {
|
||||
String test = dcContext.getBlobdir()+"/"+filename+(i==0? "" : i<100? "-"+i : "-"+(new Date().getTime()+i))+ext;
|
||||
if (!new File(test).exists()) {
|
||||
path = test;
|
||||
break;
|
||||
}
|
||||
}
|
||||
String path = dcContext.getBlobdirFile(filename, ext);
|
||||
|
||||
// copy content to this file
|
||||
if(path!=null) {
|
||||
@@ -965,7 +959,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
||||
if (action==ACTION_SEND_OUT) {
|
||||
if(msg!=null) {
|
||||
if(recompress) {
|
||||
MediaUtil.recodeImageMsg(msg);
|
||||
BitmapUtil.recodeImageMsg(ConversationActivity.this, msg);
|
||||
}
|
||||
dcContext.sendMsg(dcChat.getId(), msg);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@@ -176,6 +177,34 @@ public class ApplicationDcContext extends DcContext {
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
public String getBlobdirFile(String filename, String ext) {
|
||||
String outPath = null;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
String test = getBlobdir() + "/" + filename + (i == 0 ? "" : i < 100 ? "-" + i : "-" + (new Date().getTime() + i)) + ext;
|
||||
if (!new File(test).exists()) {
|
||||
outPath = test;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(outPath==null) {
|
||||
// should not happen
|
||||
outPath = getBlobdir() + "/" + Math.random();
|
||||
}
|
||||
return outPath;
|
||||
}
|
||||
|
||||
public String getBlobdirFile(String path) {
|
||||
String filename = path.substring(path.lastIndexOf('/')+1); // is the whole path if '/' is not found (lastIndexOf() returns -1 then)
|
||||
String ext = "";
|
||||
int point = filename.indexOf('.');
|
||||
if(point!=-1) {
|
||||
ext = filename.substring(point);
|
||||
filename = filename.substring(0, point);
|
||||
}
|
||||
return getBlobdirFile(filename, ext);
|
||||
|
||||
}
|
||||
|
||||
/***********************************************************************************************
|
||||
* create objects compatible to the database model of Signal
|
||||
**********************************************************************************************/
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.graphics.Bitmap.CompressFormat;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.YuvImage;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
@@ -16,17 +17,22 @@ import android.support.media.ExifInterface;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.b44t.messenger.DcMsg;
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy;
|
||||
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.mms.GlideApp;
|
||||
import org.thoughtcrime.securesms.mms.MediaConstraints;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@@ -367,4 +373,78 @@ public class BitmapUtil {
|
||||
return height;
|
||||
}
|
||||
}
|
||||
|
||||
// recode jpeg
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public static void recodeImageMsg(Context context, DcMsg msg)
|
||||
{
|
||||
final int desiredWH = 1280;
|
||||
|
||||
try {
|
||||
String inPath = msg.getFile();
|
||||
if(inPath==null || !new File(inPath).exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Matrix matrix = new Matrix();
|
||||
|
||||
// find out scaling
|
||||
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
|
||||
bmOptions.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeFile(inPath, bmOptions);
|
||||
|
||||
if( bmOptions.outWidth<1 || bmOptions.outHeight<1
|
||||
|| (bmOptions.outWidth<desiredWH && bmOptions.outHeight<desiredWH)) {
|
||||
Log.i(TAG, String.format("recoding of %s not needed, image is small enough", inPath));
|
||||
return;
|
||||
}
|
||||
|
||||
float scaleFactor = Math.min((float)desiredWH/(float)bmOptions.outWidth, (float)desiredWH/(float)bmOptions.outHeight);
|
||||
matrix.postScale(scaleFactor, scaleFactor);
|
||||
|
||||
// find out rotating
|
||||
{
|
||||
android.media.ExifInterface exif;
|
||||
try {
|
||||
exif = new android.media.ExifInterface(inPath);
|
||||
int orientation = exif.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, 1);
|
||||
switch (orientation) {
|
||||
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
|
||||
matrix.postRotate(90);
|
||||
break;
|
||||
case android.media.ExifInterface.ORIENTATION_ROTATE_180:
|
||||
matrix.postRotate(180);
|
||||
break;
|
||||
case android.media.ExifInterface.ORIENTATION_ROTATE_270:
|
||||
matrix.postRotate(270);
|
||||
break;
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
Log.i(TAG, String.format("cannot get exif information for %s", inPath));
|
||||
}
|
||||
}
|
||||
|
||||
// recode file
|
||||
Bitmap inBitmap = BitmapFactory.decodeFile(inPath, null);
|
||||
Bitmap outBitmap = Bitmap.createBitmap(inBitmap, 0, 0, inBitmap.getWidth(), inBitmap.getHeight(), matrix, false);
|
||||
inBitmap.recycle();
|
||||
|
||||
String outPath = DcHelper.getContext(context).getBlobdirFile(inPath);
|
||||
FileOutputStream outStream = new FileOutputStream(outPath);
|
||||
if(!outBitmap.compress(Bitmap.CompressFormat.JPEG, 85, outStream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
msg.setDimension(outBitmap.getWidth(), outBitmap.getHeight());
|
||||
msg.setFile(outPath, null);
|
||||
outBitmap.recycle();
|
||||
|
||||
Log.i(TAG, String.format("recoding for %s dine", inPath));
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.thoughtcrime.securesms.mms.Slide;
|
||||
import org.thoughtcrime.securesms.mms.VideoSlide;
|
||||
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -266,10 +267,4 @@ public class MediaUtil {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void recodeImageMsg(DcMsg msg)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user