android - 如何像 Facebook 和 WhatsApp 那样优化图像?

标签 android image bitmap

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

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

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

带有源代码的答案会很有帮助。

提前致谢。

最佳答案

你需要解码图像(位图)

这是代码。

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

    }

关于android - 如何像 Facebook 和 WhatsApp 那样优化图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25260060/

相关文章:

android intent 选择器出现缓慢

java - 使用 AssetManager 时位图字体倒置

java - Android Java,在缩放的 Canvas 上绘制

android - Android Spinner将背景恢复为默认值

c# - 在 C# 中加密然后用 Android 解密 (AES | IllegalBlockSizeException)

Activity 类之外的 Android 上下文

python - 尝试将视频分成图像阵列以用于ImageTk

java - 有没有办法在 JButton 中存储数据?

java - 确保在Java中保留List中的顺序并提高效率

c# - 什么是 BitmapData.reserved?