简体   繁体   中英

Compress image without losing any quality android?

I have created an image picker library in which user can select multiple image for uploading to server.were I'm facing a problem ie, i have to compress the selected images without loosing the quality of image much like in popular application Facebook/Whatsapp etc were images maintain with high quality but less size .i have followed the official doc but it's not really satisfying me.if am uploading size less image only i can load it into UI in a fast manner.

Any suggestions/helps are greatly appreciated.

Here is a great library on GitHub for reducing image size without losing quality.

https://github.com/zetbaitsu/Compressor

Try this:

            int width = YourImageView.getWidth();
            int height = YourImageView.getHeight();

            Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
            float originalWidth = selectedBitmap.getWidth(), originalHeight = selectedBitmap.getHeight();
            Canvas canvas = new Canvas(bitmap);
            float scale = width/originalWidth;
            float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f;
            Matrix transformation = new Matrix();
            transformation.postTranslate(xTranslation, yTranslation);
            transformation.preScale(scale, scale);
            Paint paint = new Paint();
            paint.setFilterBitmap(true);
            canvas.drawBitmap(selectedBitmap, transformation, paint);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM