Package com.example.breeze_seas
Class ImageUtils
java.lang.Object
com.example.breeze_seas.ImageUtils
Utility methods for image compression, Base64 conversion, and bitmap decoding.
This class centralizes all image-specific logic so UI classes do not need to
manage compression rules or decoding details directly.
-
Method Summary
Modifier and TypeMethodDescriptionstatic android.graphics.Bitmapbase64ToBitmap(String base64) Decodes a Base64 image string into a bitmap.static byte[]base64ToBytes(String base64) Decodes a Base64 image string into raw bytes.static StringbitmapToCompressedBase64(android.graphics.Bitmap bitmap, int maxBase64Bytes) Compresses a bitmap until its Base64 representation fits under the size limit.static intcalculateInSampleSize(android.graphics.BitmapFactory.Options options, int reqWidth, int reqHeight) Calculates a sample size for efficient bitmap decoding.static android.graphics.BitmapdecodeSampledBitmapFromUri(android.content.Context context, android.net.Uri uri, int reqWidth, int reqHeight) Decodes and down-samples an image from a content Uri.static intReturns the default max Base64 size used by this utility.static intReturns the default max dimension used by this utility.static android.graphics.BitmapscaleDown(android.graphics.Bitmap src, int maxWidth, int maxHeight) Scales a bitmap down to fit within the specified bounds.static StringuriToCompressedBase64(android.content.Context context, android.net.Uri uri) Converts a Uri image into a compressed Base64 string using default limits.static StringuriToCompressedBase64(android.content.Context context, android.net.Uri uri, int maxDimension, int maxBase64Bytes) Converts a Uri image into a compressed Base64 string using custom limits.
-
Method Details
-
uriToCompressedBase64
public static String uriToCompressedBase64(android.content.Context context, android.net.Uri uri) throws IOException Converts a Uri image into a compressed Base64 string using default limits.- Parameters:
context- Context used to open the Uri stream.uri- Selected image Uri.- Returns:
- Compressed Base64 string.
- Throws:
IOException- When the image cannot be read or compressed.
-
uriToCompressedBase64
public static String uriToCompressedBase64(android.content.Context context, android.net.Uri uri, int maxDimension, int maxBase64Bytes) throws IOException Converts a Uri image into a compressed Base64 string using custom limits.- Parameters:
context- Context used to open the Uri stream.uri- Selected image Uri.maxDimension- Maximum dimension for width/height.maxBase64Bytes- Maximum allowed Base64 size in bytes.- Returns:
- Compressed Base64 string.
- Throws:
IOException- When the image cannot be read or compressed.
-
bitmapToCompressedBase64
public static String bitmapToCompressedBase64(android.graphics.Bitmap bitmap, int maxBase64Bytes) throws IOException Compresses a bitmap until its Base64 representation fits under the size limit.- Parameters:
bitmap- Source bitmap.maxBase64Bytes- Maximum allowed Base64 size in bytes.- Returns:
- Compressed Base64 string.
- Throws:
IOException- When compression fails.
-
base64ToBitmap
Decodes a Base64 image string into a bitmap.- Parameters:
base64- Base64 image string.- Returns:
- Decoded bitmap, or null if empty/invalid.
-
base64ToBytes
Decodes a Base64 image string into raw bytes.- Parameters:
base64- Base64 image string.- Returns:
- Decoded bytes, or null if empty/invalid.
-
decodeSampledBitmapFromUri
public static android.graphics.Bitmap decodeSampledBitmapFromUri(android.content.Context context, android.net.Uri uri, int reqWidth, int reqHeight) throws IOException Decodes and down-samples an image from a content Uri.- Parameters:
context- Context used to open the Uri stream.uri- Source image Uri.reqWidth- Requested max width.reqHeight- Requested max height.- Returns:
- Decoded bitmap.
- Throws:
IOException- When the image cannot be opened or decoded.
-
calculateInSampleSize
public static int calculateInSampleSize(android.graphics.BitmapFactory.Options options, int reqWidth, int reqHeight) Calculates a sample size for efficient bitmap decoding.- Parameters:
options- Bitmap bounds options.reqWidth- Requested max width.reqHeight- Requested max height.- Returns:
- Sample size to use.
-
scaleDown
public static android.graphics.Bitmap scaleDown(android.graphics.Bitmap src, int maxWidth, int maxHeight) Scales a bitmap down to fit within the specified bounds.- Parameters:
src- Source bitmap.maxWidth- Maximum width.maxHeight- Maximum height.- Returns:
- Scaled bitmap.
-
getDefaultMaxBase64Bytes
public static int getDefaultMaxBase64Bytes()Returns the default max Base64 size used by this utility.- Returns:
- Default Base64 byte limit.
-
getDefaultMaxDimension
public static int getDefaultMaxDimension()Returns the default max dimension used by this utility.- Returns:
- Default max dimension.
-