From 624c30c5cc5c52e6e6d9aa5b239bbd682eb9efa0 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 25 Dec 2017 17:48:33 +0100 Subject: [PATCH] Image are reduced before being sent --- .../android/comunic/client/LoginActivity.java | 2 +- .../comunic/client/api/APIRequest.java | 3 - .../comunic/client/data/Account/Account.java | 2 +- .../client/data/Account/AccountUtils.java | 3 +- .../data/ImageLoad/ImageDownloadRunnable.java | 2 - .../client/data/ImageLoad/ImageLoadTask.java | 5 +- .../client/data/ImageLoad/ImageLoadUtils.java | 2 +- .../ConversationsListAdapter.java | 6 +- .../client/data/friendsList/Friend.java | 2 +- .../data/friendsList/FriendsAdapter.java | 3 +- .../client/data/utils/BitmapUtils.java | 61 +++++++++++++++++++ .../client/data/{ => utils}/Utilities.java | 16 +---- .../fragments/ConversationFragment.java | 11 +++- 13 files changed, 78 insertions(+), 40 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/utils/BitmapUtils.java rename app/src/main/java/org/communiquons/android/comunic/client/data/{ => utils}/Utilities.java (93%) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java index 1846875..b034e83 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java @@ -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.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.JSONObject; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java index 86f53b6..ffbd98e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java @@ -3,11 +3,9 @@ package org.communiquons.android.comunic.client.api; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; -import android.util.Log; import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.data.Account.Account; -import org.communiquons.android.comunic.client.data.Utilities; import java.io.BufferedOutputStream; import java.io.BufferedReader; @@ -17,7 +15,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.Reader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/Account.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/Account.java index c9c559d..59f9156 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/Account.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/Account.java @@ -3,7 +3,7 @@ package org.communiquons.android.comunic.client.data.Account; import android.content.Context; 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.JSONException; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java index 7a1f3d0..4ed04f5 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java @@ -5,8 +5,7 @@ import android.content.Context; import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.api.APIRequestTask; import org.communiquons.android.comunic.client.api.APIResponse; -import org.communiquons.android.comunic.client.data.Utilities; -import org.json.JSONException; +import org.communiquons.android.comunic.client.data.utils.Utilities; import org.json.JSONObject; /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java index 7352447..bb4f38f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java @@ -3,8 +3,6 @@ package org.communiquons.android.comunic.client.data.ImageLoad; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import org.communiquons.android.comunic.client.data.Utilities; - import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadTask.java b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadTask.java index e49c61e..b7b42f6 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadTask.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadTask.java @@ -3,13 +3,11 @@ package org.communiquons.android.comunic.client.data.ImageLoad; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; import android.os.AsyncTask; -import android.support.v4.content.ContextCompat; import android.util.Log; 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.FileInputStream; @@ -19,7 +17,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadUtils.java index 4e4ed83..40227a3 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadUtils.java @@ -3,7 +3,7 @@ package org.communiquons.android.comunic.client.data.ImageLoad; import android.content.Context; 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; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java index 8d63062..4b70e35 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java @@ -1,7 +1,6 @@ package org.communiquons.android.comunic.client.data.conversations; import android.content.Context; -import android.graphics.Typeface; import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -12,12 +11,9 @@ import android.widget.ArrayAdapter; import android.widget.TextView; 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.zip.Inflater; - -import static android.R.id.list; /** * Conversations adapter diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java index 2bf1e67..24c7f47 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java @@ -1,6 +1,6 @@ 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 diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java index c765a24..27f3b02 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java @@ -11,10 +11,9 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; -import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.R; 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 java.util.ArrayList; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/BitmapUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/BitmapUtils.java new file mode 100644 index 0000000..dbdd5de --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/BitmapUtils.java @@ -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); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java similarity index 93% rename from app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java rename to app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java index 0e09321..f31e426 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java @@ -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.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 diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java index cffbc8b..c53220f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java @@ -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.UsersInfo.GetUsersHelper; 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.ConversationMessageAdapter; 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.utils.BitmapUtils; import java.io.FileNotFoundException; import java.io.InputStream; @@ -411,8 +411,13 @@ public class ConversationFragment extends Fragment @Override protected Boolean doInBackground(Void... params) { - String message_image = new_message_bitmap == null ? - null : Utilities.bitmapToBase64(new_message_bitmap); + String message_image = null; + + //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); }