Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android reducing image size before uploading to server

I have to upload image captured from camera/gallery to server. In many apps I have seen images having resolution 1000X560 having size of 35 KB. While in my case, the image size goes upto 380 KB. My phone's camera captures images of resolution 2368X4224 of size < 2 MB. How can I have image at high resolution while keeping its size low? Here's what I have tried so far:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(realPath, bmOptions);
bmOptions.inSampleSize = 1;
bmOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
bmOptions.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(realPath, bmOptions);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

I had read this documentation. The problem I am facing is how to decide min width and min height for the image.

like image 423
Nitish Avatar asked Jul 26 '26 08:07

Nitish


1 Answers

Hey can u check http://voidcanvas.com/whatsapp-like-image-compression-in-android/ this link

I think this is best image compression tutorial.
Also check this answer: https://stackoverflow.com/a/26928768/5275436
I hope it help.

Edit: Here is image resize .

//      max Height and width values of the compressed image is taken as 816x612

    float maxHeight = 816.0f;
    float maxWidth = 612.0f;
    float imgRatio = actualWidth / actualHeight;
    float maxRatio = maxWidth / maxHeight;


 //      width and height values are set maintaining the aspect ratio of the image
        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {               imgRatio = maxHeight / actualHeight;                actualWidth = (int) (imgRatio * actualWidth);               actualHeight = (int) maxHeight;             } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;

            }
        }
like image 130
Govinda Paliwal Avatar answered Jul 28 '26 14:07

Govinda Paliwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!