如何像Facebook和WhatApp那样优化图像?

use*_*917 11 android image bitmap

我想像whatsapp和facebook正在做的那样优化图像文件大小.我在whatsapp上发送了5MB图像,并且接收到的图像大小为80KB.接收的图像看起来与原始图像相同,但分辨率低于原始图像.

我在stackoverflow上尝试了几乎所有的android图像压缩源代码,但这对我不起作用.然后我遇到了这个链接,以优化图像,这是很好的工作,但仍然没有得到像whatsapp的结果.

如何在不降低图像质量的情况下实现最大图像压缩,就像whatsapp一样?

使用源代码回答将非常有帮助.

提前致谢.

小智 0

将图像转换为Bitmap 并使用 compress()方法将其压缩到一定质量。为了保持图像的质量,请使用其高度和宽度的纵横比来减少图像。您可以使用以下代码:其中 data 是图像的字节数组

Bitmap screen = BitmapFactory.decodeByteArray(data, 0,
                    data.length);
            float oHeight = screen.getHeight();
            float oWidth = screen.getWidth();
            float aspectRatio = oWidth / oHeight;
            int newHeight = 0;
            int newWidth = 0;
            newHeight = 450;
            newWidth = (int) (newHeight * aspectRatio);
            screen = Bitmap.createScaledBitmap(screen, newWidth, newHeight,
                    true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
            screen.compress(Bitmap.CompressFormat.JPEG, 50, bos);
Run Code Online (Sandbox Code Playgroud)

  • 以上答案并不能解决我的问题。我已经在我的代码中做了这件事,它在我的问题的链接中给出。 (3认同)