【问题标题】:Issue with Bitmap Quality after Scaling/Decoding in Android在 Android 中缩放/解码后的位图质量问题
【发布时间】:2014-11-19 23:00:06
【问题描述】:

我有以下 getBitmap() 和 decode() 代码。 url 图片资源为 1080x1920 jpeg 图片。我的手机尺寸也是 1080x1920。但是,在执行以下功能并获取位图后,我正在执行以下操作:

imageView.setImageBitmap(bitmap);
relativeLayout.setBackground(imageView.getDrawable());

下面代码中的解码/缩放是否有问题?

private Bitmap getBitmap(String url)
    {
        File f=fileCache.getFile(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null){
            Log.d(TAG, "getBitmap() - Decoded from File successfully");
            return b;   
        }

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } 
        catch (Exception ex)
        {
           ex.printStackTrace();
           return null;
        }
    }

    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //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=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

【问题讨论】:

  • 没关系。如果损失使图像无法识别或单色区域太大,您可以进行两步缩放。我的意思是扩大到中等规模,然后扩大到最终
  • 感谢您的回复@eduyayo。您能否告诉我需要更新哪些行以提高图像质量?
  • 出于某种原因,我有一个来自 url 的图片资源,大小为 700KB。我监控了内存使用情况,每次添加新的图像视图时,每个图像都会显着增加(5mb 左右)。我的应用将在给定时间显示许多图像,我正在尝试在使用良好图像质量的同时尽量减少内存消耗。
  • 按照@slogamo 的建议使用毕加索。它将为您处理回收和缩放,因此内存消耗较低。我之前用两步的意思是,如果图像的宽度为 1000 像素,并且您必须将其渲染为 50 像素的图标,则最好分两步甚至三步将其缩小:1000 像素-> 300 像素-> 50 像素

标签: android bitmap android-imageview scaling android-bitmap


【解决方案1】:

这是我在我的应用程序中使用的一个很棒的第三方库。图像大小调整精美。希望它可以提供帮助。 http://square.github.io/picasso/

【讨论】:

  • 感谢@Sloganho 的推荐。出于某种原因......当我尝试使用毕加索时,假设我有一个来自 url 的图像资产,它是 700KB。我监控了内存使用情况,每次添加新的图像视图时,每个图像都会显着增加(5mb 左右)。我的应用程序将在给定时间显示许多图像,我试图在使用良好图像质量的同时尽量减少内存消耗。毕加索是否允许配置内存消耗或图像质量?
  • 这个似乎更可定制,更适合更大的图片。我对毕加索一无所知,所以我很抱歉。但也许你可以看看这个。 github.com/nostra13/Android-Universal-Image-Loaderopensourcealternative.org/tutorials/android-tutorials/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多