在Android中创建和保存单色PNG位图时减少内存使用量 [英] Reducing memory usage while creating and saving a single color PNG bitmap in Android

查看:69
本文介绍了在Android中创建和保存单色PNG位图时减少内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建和保存单色PNG图像(位图填充单色).

I need to create and save single color PNG images (bitmap filled with a single color).

我正在创建位图:

public static Bitmap createColorSwatchBitmap(int width, int height, int color) {
    final Bitmap colorBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    colorBitmap.eraseColor(color);
    return colorBitmap;
}

并将其保存到设备存储中的文件中

and saving it to a file on the device storage:

stream = new FileOutputStream(filePath);
success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

如果我创建1200x1200位图,则内存消耗为5760000字节(5.76 MB),这由 bitmap.getAllocationByteCount()报告.但是,PNG文件的大小仅为8,493字节.

If I create a 1200x1200 bitmap, the memory consumption is 5,760,000 bytes (5.76 MB), as reported by bitmap.getAllocationByteCount(). However the PNG file size is only 8,493 bytes.

为几乎只有8 KB的文件分配近6 MB的内存似乎太过分了.

It seems so overkill to allocate almost 6 MB of memory for a file that will only have 8 KB.

有更好的方法吗?

推荐答案

您可以使用 PNGJ 库(免责声明:我是作者).由于它会逐步保存图像,因此只需要分配一行即可.

You can use the PNGJ library (disclaimer: I'm the author). Because it saves the image progressively, it only needs to allocate a single row.

例如:

 public static void create(OutputStream os,int cols,int rows,int r,int  g,int  b,int  a)  {     
        ImageInfo imi = new ImageInfo(cols, rows, 8, true); // 8 bits per channel, alpha
        PngWriter png = new PngWriter(os, imi);
        // just a hint to the coder to optimize compression+speed:
        png.setFilterType(FilterType.FILTER_NONE); 
        ImageLineByte iline = new ImageLineByte (imi);
        byte[] scanline = iline.getScanlineByte();// RGBA
        for (int col = 0,pos=0; col < imi.cols; col++) { 
           scanline[pos++]=(byte) r;  
           scanline[pos++]=(byte) g;
           scanline[pos++]=(byte) b;
           scanline[pos++]=(byte) a;
        }
        for (int row = 0; row < png.imgInfo.rows; row++) {
           png.writeRow(iline);
        }
        png.end();   
 }

为几乎只有8 KB的文件分配近6 MB的内存似乎太过分了.

It seems so overkill to allocate almost 6 MB of memory for a file that will only have 8 KB.

这里有两件事.首先,浪费空间以便在内存中分配完整的图像-我的解决方案通过分配单个行来解决此问题.但是,除此之外,您还犯了一个概念错误:将内存中分配的空间与编码的图像大小进行比较是没有意义的,因为PNG是一种压缩格式(并且彩色图像将被高度压缩).实际上,任何原始可编辑位图(Android中的 Bitmap ,ImageIO中的 BufferedImage ,PNGJ中我自己的 ImageLineByte 或其他)分配的内存都会永远不会被压缩,因此,它将至少每个像素浪费4个字节.您可以检查:1200x1200x4 = 5760000.

There are two different things here. First, the space wasted in order to allocate the full image in memory - my solution alliviates this, by allocating a single row. But, apart from this, you are making a conceptual error: it make no sense to compare the space allocated in memory with the encoded image size, because PNG is a compressed format (and a single color image will be highly compressed). The memory allocated by any raw editable bitmap (Bitmap in Android, BufferedImage in ImageIO, my own ImageLineByte in PNGJ, or whatever) in practice will never be compressed, and hence it will always waste 4 bytes per pixel - at least. And you can check that: 1200x1200x4=5760000.

这篇关于在Android中创建和保存单色PNG位图时减少内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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