3

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.

Nitish
  • 3,097
  • 13
  • 45
  • 80

1 Answers1

3

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;

            }
        }
Community
  • 1
  • 1
Govinda P
  • 3,261
  • 5
  • 22
  • 43
  • I had read this tutorial. They are using max width and height of 816 and 612. That's what I am not getting. Should I use hardcoded value or I should do some calculation or at which factors these values are decided? Actually this I will use in an ecommerce app – Nitish Jun 18 '16 at 05:20
  • Ok but it's depend on you means your image size requirement. And my opening for ecommerce app : decide max w and h and if image greater than this size then you can resize . Please check edited answer ( resize function ). – Govinda P Jun 18 '16 at 05:32
  • Tried but resolution got down to 457X816 and file size is still 276 KB. I have to keep the resolution high while keeping the file size low. – Nitish Jun 18 '16 at 06:34
  • Try to reduce compress 80 to 60 i.e. scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 60, out); – Govinda P Jun 18 '16 at 07:02