Nitish
Nitish

Reputation: 3155

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.

Upvotes: 3

Views: 7780

Answers (1)

Govinda P
Govinda P

Reputation: 3319

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;

            }
        }

Upvotes: 3

Related Questions