压缩图像而不会丢失任何质量的android?

时间:2015-12-10 09:19:12

标签: android image resize compression

我创建了一个图像选择器库,用户可以在其中选择多个图像上传到服务器。我正面临一个问题,即我必须压缩所选图像而不会像在流行的应用程序中那样失去图像质量<强势> Facebook / Whatsapp 等图像保持高质量,但尺寸较小。我已经跟随official文档,但它并不真正令我满意。如果只上传大小较少的图像我只能加载到UI快速地。

非常感谢任何建议/帮助。

2 个答案:

答案 0 :(得分:1)

这是GitHub上的一个很棒的库,用于在不损失质量的情况下缩小图像大小。

https://github.com/zetbaitsu/Compressor

答案 1 :(得分:0)

试试这个:

            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);
相关问题