【问题标题】:how to optimize image like Facebook and WhatApp does?如何像 Facebook 和 WhatsApp 一样优化图像?
【发布时间】:2014-10-05 06:31:54
【问题描述】:

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

我尝试了 stackoverflow 上几乎所有可用的 android 图像压缩源代码,但这对我不起作用。然后我遇到this link 来优化图像,它做得很好,但仍然没有像whatsapp那样得到结果。

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

用源代码回答会很有帮助。

提前致谢。

【问题讨论】:

  • 实现它的唯一方法是在Bitmap 中使用inSampleSize。您在答案中指定的链接通过指定目标最大宽度和高度来计算该因子。您可以尝试减少它们,看看是否能得到更好的结果。
  • WhatsApp 确实使用有损压缩,图像质量急剧下降。不要指望仅通过无损算法就能获得这种尺寸改进......
  • 抱歉弄乱了一个旧线程,很好奇,whatsapp/fb 会降低客户端(如 android 应用程序)或服务器端的质量吗?谢谢

标签: android image bitmap


【解决方案1】:

你需要解码图像(位图)

这里是代码。

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;
        }

    }

【讨论】:

    【解决方案2】:

    将图像转换为位图,并使用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);
    

    【讨论】:

    • 这取决于你的要求,比如保持宽高,重要的是在 compress() 方法中将整数值从 50 增加到以上
    • 是的,但问题是它没有像 whatsapp 那样提供质量,如果我们降低 50,,40,,30 等值,它会非常严重地降低质量
    • @Nish8900 您是否检查了我在上述问题中提供的链接?将质量级别设置为 50 会减小图像大小但也会降低图像质量,我想在保持图像质量的同时减小图像大小。
    • 以上答案不能解决我的问题。我已经在我的代码中做了那件事,这在我的问题的链接中给出。
    • compress() 中的整数值为 80,并根据显示保持高度
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2011-03-24
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多