Image are reduced before being sent

This commit is contained in:
Pierre 2017-12-25 17:48:33 +01:00
parent 197bd9fe40
commit 624c30c5cc
13 changed files with 78 additions and 40 deletions

View File

@ -12,7 +12,7 @@ import android.widget.Toast;
import org.communiquons.android.comunic.client.data.Account.Account; import org.communiquons.android.comunic.client.data.Account.Account;
import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.Account.AccountUtils;
import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;

View File

@ -3,11 +3,9 @@ package org.communiquons.android.comunic.client.api;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.util.Log;
import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.BuildConfig;
import org.communiquons.android.comunic.client.data.Account.Account; import org.communiquons.android.comunic.client.data.Account.Account;
import org.communiquons.android.comunic.client.data.Utilities;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -17,7 +15,6 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -3,7 +3,7 @@ package org.communiquons.android.comunic.client.data.Account;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;

View File

@ -5,8 +5,7 @@ import android.content.Context;
import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.api.APIRequestParameters;
import org.communiquons.android.comunic.client.api.APIRequestTask; import org.communiquons.android.comunic.client.api.APIRequestTask;
import org.communiquons.android.comunic.client.api.APIResponse; import org.communiquons.android.comunic.client.api.APIResponse;
import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
/** /**

View File

@ -3,8 +3,6 @@ package org.communiquons.android.comunic.client.data.ImageLoad;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import org.communiquons.android.comunic.client.data.Utilities;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;

View File

@ -3,13 +3,11 @@ package org.communiquons.android.comunic.client.data.ImageLoad;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.v4.content.ContextCompat;
import android.util.Log; import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -19,7 +17,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
/** /**

View File

@ -3,7 +3,7 @@ package org.communiquons.android.comunic.client.data.ImageLoad;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import java.io.File; import java.io.File;

View File

@ -1,7 +1,6 @@
package org.communiquons.android.comunic.client.data.conversations; package org.communiquons.android.comunic.client.data.conversations;
import android.content.Context; import android.content.Context;
import android.graphics.Typeface;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -12,12 +11,9 @@ import android.widget.ArrayAdapter;
import android.widget.TextView; import android.widget.TextView;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.zip.Inflater;
import static android.R.id.list;
/** /**
* Conversations adapter * Conversations adapter

View File

@ -1,6 +1,6 @@
package org.communiquons.android.comunic.client.data.friendsList; package org.communiquons.android.comunic.client.data.friendsList;
import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
/** /**
* Friend object * Friend object

View File

@ -11,10 +11,9 @@ import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import org.communiquons.android.comunic.client.BuildConfig;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager;
import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import org.communiquons.android.comunic.client.fragments.FriendsListFragment; import org.communiquons.android.comunic.client.fragments.FriendsListFragment;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -0,0 +1,61 @@
package org.communiquons.android.comunic.client.data.utils;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Base64;
import java.io.ByteArrayOutputStream;
/**
* Bitmap utilities
*
* @author Pierre HUBERT
* Created by pierre on 12/25/17.
*/
public class BitmapUtils {
/**
* Convert a Bitmap into a base64-encoded string
*
* @param bitmap The bitmap to convert
* @return Encoded string
*/
public static String bitmapToBase64(@NonNull Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
/**
* Create a bitmap based on another Bitmap by proportionally reducing its size
*
* Note : If the size of the Bitmap is smaller than the maximum width and height specified,
* The original bitmap is returned with no reduction
*
* @param bitmap The Bitmap to reduce
* @param maxW The maximum width of the new bitmap
* @param maxH The maximum height of the new bitmap
* @return The new bitmap
*/
public static Bitmap reduceBitmap(@NonNull Bitmap bitmap, int maxW, int maxH){
//Get the current dimensions
int currW = bitmap.getWidth();
int currH = bitmap.getHeight();
//Check if the source bitmap is small enough
if(currW <= maxW && currH <= maxH)
return bitmap;
//Compute new sizes for the bitmap
int newW = maxW;
int newH = (int) Math.floor((currH*maxW)/currW);
//Scale and return bitmap
return Bitmap.createScaledBitmap(bitmap, newW, newH, true);
}
}

View File

@ -1,4 +1,4 @@
package org.communiquons.android.comunic.client.data; package org.communiquons.android.comunic.client.data.utils;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
@ -224,20 +224,6 @@ public class Utilities {
} }
} }
/**
* Convert a Bitmap into a base64-encoded string
*
* @param bitmap The bitmap to convert
* @return Encoded string
*/
public static String bitmapToBase64(@NonNull Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
/** /**
* Prepare a string sent through the API to be shown in a TextView element * Prepare a string sent through the API to be shown in a TextView element

View File

@ -26,11 +26,11 @@ import org.communiquons.android.comunic.client.data.Account.AccountUtils;
import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
import org.communiquons.android.comunic.client.data.Utilities;
import org.communiquons.android.comunic.client.data.conversations.ConversationMessage; import org.communiquons.android.comunic.client.data.conversations.ConversationMessage;
import org.communiquons.android.comunic.client.data.conversations.ConversationMessageAdapter; import org.communiquons.android.comunic.client.data.conversations.ConversationMessageAdapter;
import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper;
import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable; import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable;
import org.communiquons.android.comunic.client.data.utils.BitmapUtils;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.InputStream;
@ -411,8 +411,13 @@ public class ConversationFragment extends Fragment
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
String message_image = new_message_bitmap == null ? String message_image = null;
null : Utilities.bitmapToBase64(new_message_bitmap);
//Reduce Bitmap and convert it to a base64-encoded string
if(new_message_bitmap != null)
message_image = BitmapUtils.bitmapToBase64(
BitmapUtils.reduceBitmap(new_message_bitmap, 1199, 1199));
return convMessHelper.sendMessage(conversation_id, message_content, message_image); return convMessHelper.sendMessage(conversation_id, message_content, message_image);
} }