Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to optimize image like Facebook and WhatApp does?

I want to optimize the Image file size like whatsapp and facebook are doing. I have send 5MB Image on whatsapp, And received image has size of 80KB. Received image looks identical to the original ones but resolution is less than the original one.

I tried almost all the source codes of android image compression available on stackoverflow but that doesn't work for me. And then i came across this link to optimize the image, which is doing good work but still not getting the result like whatsapp.

How can i achieve maximum image compression without degrading the quality of image, Same like whatsapp did?

Answer with a source code will be very helpful.

Thanks In advance.

like image 306
user3607917 Avatar asked Aug 12 '14 08:08

user3607917


People also ask

How does WhatsApp compress images?

The instant messaging platform from Meta automatically resizes images before it is being sent to the receiver. Even if the image is of high quality, WhatsApp acts as an image compressor and reduces the quality and size of the image before sending it. However, it allows managing of storage and data through the settings.


1 Answers

You need to decode Image (bitmap)

Here is the code.

public Bitmap  decodeFile(String path) {
        // Decode image size
        int orientation;
        try {
            if (path == null) {
                return null ;
            }
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 0;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bm = BitmapFactory.decodeFile(path, o2);
            Bitmap bitmap = bm;
            ExifInterface exif = new ExifInterface(path);
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

            Log.e("orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true);
            ImageViewChooseImage.setImageBitmap(bitmap);
            bitmapfinal = bitmap;
            return bitmap ;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
like image 147
Robin Royal Avatar answered Oct 20 '22 00:10

Robin Royal