微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何优化像Facebook和WhatApp这样的图像?

我想优化图像文件的大小,像whatsapp和Facebook正在做的.我已经发送了5MB的图片,whatsapp,并且收到的图像大小为80KB.收到的图像与原始图像相似,但分辨率小于原始图像.

我尝试几乎所有的源代码android图像压缩可用在stackoverflow,但不适用于我.然后我碰到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,bm.getWidth(),bm.getHeight(),null,true);
            ImageViewChooseImage.setimageBitmap(bitmap);
            bitmapfinal = bitmap;
            return bitmap ;
        } catch (Exception e) {
            e.printstacktrace();
            return null;
        }

    }

原文地址:https://www.jb51.cc/android/312182.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐