为什么保存加载位图增加它的大小? [英] Why does saving a loaded Bitmap increase it's size?

查看:174
本文介绍了为什么保存加载位图增加它的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载从SD卡的PNG文件。我改变它以某种方式,然后再保存它。我注意到,保存的文件比原来大得多。

起初我还以为是因为修改的文件。然后,我用下面的code测试,仍然发生。

我只是加载PNG文件,然后用另一个名字保存它。

原始文件的 32Kb的并保存的文件为 300KB
任何想法?

 尝试
     {位图BM = BitmapFactory.de codeFILE(Environment.getExternalStorageDirectory()+/Original.png);
        FileOutputStream中出=新的FileOutputStream(Environment.getExternalStorageDirectory()+/Saved.png);
        bm.com preSS(Bitmap.Com pressFormat.PNG,100,出);
     }
     赶上(例外五)
     {//这里从来没有得到
     }


解决方案

您可以控制​​大小的位图 COM preSS系数(质):

  Bitmap.com preSS(Bitmap.Com pressFormat格式,诠释品质,FileOutputStream中出);

100 =最高的质量,0 =低质量。

尝试,如10或东西,让您的图像更小。
它也可能是您正在加载一个位图,它是在 16位颜色和不同于稍后保存在 32位的颜色,增加它的大小。

检查 BitmapConfig.RGB_565 BitmapConfig.ARGB_4444 BitmapConfig.ARGB_8888 分别。

编辑:

下面是如何正确地加载位图:

 公共抽象类Bitma $ P $ {psLoader    公共静态位图德codeBitmapFromResource(Bitmap.Config配置,资源RES,INT渣油,诠释reqWidth,诠释reqHeight){        最后BitmapFactory.Options选项=新BitmapFactory.Options();
        options.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeResource(RES,渣油,期权);        options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight);        //德code位与inSampleSize集
        options.inJustDe codeBounds = FALSE;
        options.in preferredConfig =配置;        返回BitmapFactory.de codeResource(RES,渣油,期权);
    }    私有静态诠释calculateInSampleSize(
                BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
        //原始高度和图像宽度
        最终诠释身高= options.outHeight;
        最终诠释宽度= options.outWidth;
        INT inSampleSize = 1;        如果(高度> reqHeight ||宽度GT; reqWidth){            //计算的高度和宽度的比率要求的高度和宽度
            最终诠释heightRatio = Math.round((浮点)高度/(浮点)reqHeight);
            最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth);            //选择最小比率inSampleSize值,这将保证
            //使用两个维度大于或等于所述最终图像
            //请求的高度和宽度。
            inSampleSize = heightRatio< widthRatio? heightRatio:widthRatio;
        }        返回inSampleSize;
    }
}

在code:

  // Bitmap.Config.RGB_565 = 16位
位图B = Bitma presLoader.de codeBitmapFromResource(Bitmap.Config.RGB_565,getResources(),宽,高);

这样的话,你可以控制你如何位图被加载到内存(请参阅 Bitmap.Config

I load a png file from the sdcard. I alter it in someway and then save it again. I have noticed that the saved file is much larger than the original.

At first I thought it was because of modifying the file. Then I tested with the code below and still happens.

I just load a png file and then save it with another name.

The Original file is 32Kb and the Saved file is 300Kb. Any ideas?

     try
     {  Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/Original.png");
        FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Saved.png"); 
        bm.compress(Bitmap.CompressFormat.PNG, 100, out);
     } 
     catch (Exception e)
     {  //Never gets here
     }

解决方案

You can control the size of your Bitmap by the compress factor (quality):

Bitmap.compress(Bitmap.CompressFormat format, int quality, FileOutputStream out);

100 = maximum quality, 0 = low quality.

Try like "10" or something to keep your image smaller. It could also be that you are loading a Bitmap that is in 16-Bit color and than later save it in 32-Bit color, increasing its size.

Check BitmapConfig.RGB_565, BitmapConfig.ARGB_4444 and BitmapConfig.ARGB_8888 respectively.

EDIT:

Here is how to load Bitmaps correctly:

public abstract class BitmapResLoader {

    public static Bitmap decodeBitmapFromResource(Bitmap.Config config, Resources res, int resId, int reqWidth, int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = config;

        return BitmapFactory.decodeResource(res, resId, options);
    }

    private static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }
}

In code:

// Bitmap.Config.RGB_565 = 16 Bit
Bitmap b = BitmapResLoader.decodeBitmapFromResource(Bitmap.Config.RGB_565, getResources(), width, height);

That way, you can control how your Bitmap is loaded into Memory (see Bitmap.Config).

这篇关于为什么保存加载位图增加它的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆