Reputation: 1817
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.
Upvotes: 0
Views: 5284
Reputation: 109
Here is a great library on GitHub for reducing image size without losing quality.
https://github.com/zetbaitsu/Compressor
Upvotes: 1
Reputation: 2325
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);
Upvotes: 0