解码后的位图会产生更大的尺寸

时间:2012-07-12 08:43:07

标签: android bitmap

我正在尝试使用BitmapFactory.Options.inSampleSize以优选的大小对其进行解码后存储位图(我之前已从文件中读取)。问题是存储的位图的文件大小至少是原始文件大小的两倍。我已经搜索了很多,无法找到我如何处理这个,因为我不希望它发生在memmory效率(后来我重用存储的位图)。这是我所描述的方法:

private Bitmap decodeFileToPreferredSize(File f) {
    Bitmap b = null;
    try {
        // Decode image size
        Log.i("Bitmap", "Imported image size: " + f.length() + " bytes");
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(f.getAbsolutePath(), o);

        //Check if the user has defined custom image size
        int scale = 1;
        if(pref_height != -1 && pref_width != -1) {

            if (o.outHeight > pref_height || o.outWidth > pref_width) {

                scale = (int) Math.pow(
                        2,
                        (int) Math.round(Math.log(pref_width
                                / (double) Math.max(o.outHeight, o.outWidth))
                                / Math.log(0.5)));
            }
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        b = BitmapFactory.decodeFile(f.getAbsolutePath(), o2);
        String name = "Image_" + System.currentTimeMillis() + ".jpg";
        File file = new File(Environment.getExternalStorageDirectory(), name);
        FileOutputStream out = new FileOutputStream(file);
        b.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.close();
        Log.i("Bitmap", "Exported image size: " + file.length() + " bytes");

    } catch (Exception e) {
        b = null;
    }

    return b;
}

更新我看到了两个图像文件的密度。来自相机意图的那个具有72dpi的宽度和高度密度。从我的上述方法创建的图像文件具有96 dpi的宽度和高度密度。这解释了为什么使用我的上述方法将来自相机的0.5 MB图像大小调整为大约2.5 MByte,因为重新缩放因子是(96/72)* 2~ = 2.5。出于某种原因,我创建的位图不会获取来自相机的图像的密度。我尝试使用BitmapFactory.Options.inDensity的所有变体设置密度,但没有运气。此外,我试图用bitmap.setDensity(int dpi);更改位图密度,但仍然没有效果。所以,我的新问题是,如果有一种方法可以在存储图像时定义位图的密度。

提前致谢。

3 个答案:

答案 0 :(得分:2)

我有类似的问题。当我从网上下载图像时,他们在SD上使用的空间比从浏览器下载到我的PC时要多。我认为问题只是BitmapFactory以某种非优化格式保存图像。

我的解决方法是使用以下代替bitmapfactory:

    try {
        try {
            is = yourinputstream;
            // Consider reading stream twice and return the bitmap.

            os = youroutputstream;

            byte data[] = new byte[4096];
            int count;
            while ((count = is.read(data)) != -1) {
                os.write(data, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                is.close();
            }
            if(os != null) {
                os.close();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

答案 1 :(得分:1)

问题在于密度变化,这是正确的。

该主题揭示了一个解决方案:BitmapFactory returns bigger image than source

解码前还禁用inScaled

options.inSampleSize = 2; //or however you desire, power of 2
options.inScaled = false;

这样可以防止密度发生变化,你会看到你想要的尺寸减小。

答案 2 :(得分:0)

BitmapFactory.Options.inSampleSize = 0.5时,它会100%/ 0.5 = 200% 您认为我想要的是BitmapFactory.Options.inSampleSize = 2,它确实是100%/ 2 = 50%