如何在 Android 中编辑和保存大位图?目前将其写成图块,但将它们重新组合在一起的速度很慢

How do I edit and save a large bitmap in Android? Currently writing it out as tiles but it is slow to join them back together

我在 Android 上使用 renderscript 来编辑照片,目前由于 Android 上的纹理大小限制和内存限制,如果我尝试太大的应用程序将崩溃,例如用设备摄像头。

我第一个想到解决这个问题是使用 BitmapRegionDecoder 并将大照片平铺成易于管理的部分,通过渲染脚本编辑它们并一次保存一个,然后使用 PNGJ - 一种 PNG 解码和编码将它们拼接在一起允许将 PNG 图像分段写入磁盘的库,因此我没有完整的图像在内存中。

这很好用,但将它们拼接在一起需要相当长的时间 - 估计大约需要 1 分钟。

我应该考虑其他解决方案吗?如果那里有解决方案,我可以改成JPEG,但我还没有找到。基本上我正在寻找 BitmapRegionDecoder 的另一面,一个 BitmapRegionEncoder。

澄清一下,我不想调整图片大小。

  1. 使用 BitmapRegionDecoder 以水平条纹加载图像。下面的代码假定它是 PNG 并使用 PNGJ 将元数据复制到新图像,但是添加对 JPEG 的支持应该不会太困难。
  2. 使用 Renderscript 处理每个条纹。
  3. 使用 PNGJ 保存。不要使用高压缩,否则它会变慢。

this image 的 PNG 版本 (4850x3635px) 在 Nexus 5 上使用普通 RS 过滤器(去饱和)需要 12 秒。

void processPng(String forig,String fdest) {
    try {
        Allocation inAllocation = null;
        Allocation outAllocation = null;
        final int block_height = 64;

        FileInputStream orig = new FileInputStream(forig);
        FileInputStream orig2 = new FileInputStream(forig);
        FileOutputStream dest = new FileOutputStream(fdest);

        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(orig, false);
        Rect blockRect = new Rect();

        PngReader pngr = new PngReader(orig2);
        PngWriter pngw = new PngWriter(dest, pngr.imgInfo);
        pngw.copyChunksFrom(pngr.getChunksList());

        // keep compression quick
        pngw.getPixelsWriter().setDeflaterCompLevel(1);

        int channels = 3; // needles to say, this should not be hardcoded
        int width = pngr.imgInfo.samplesPerRow / channels;
        int height = pngr.imgInfo.rows;

        pngr.close(); // don't need it anymore

        blockRect.left = 0;
        blockRect.right = width;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap blockBitmap;
        byte []bytes = new byte[width * block_height * 4];
        byte []byteline = new byte[width * channels];

        for (int row = 0; row <= height / block_height; row++) {
            int h;

            // are we nearing the end?
            if((row + 1) *  block_height <= height)
                h = block_height;
            else {
                h = height - row * block_height;

                // so that new, smaller Allocations are created
                inAllocation = outAllocation = null;
            }

            blockRect.top = row * block_height;
            blockRect.bottom = row * block_height + h;

            blockBitmap = decoder.decodeRegion(blockRect, options);

            if(inAllocation == null)
                inAllocation = Allocation.createFromBitmap(mRS, blockBitmap);

            if(outAllocation == null)
            {
                Type.Builder TypeDir = new Type.Builder(mRS, Element.U8_4(mRS));
                TypeDir.setX(width).setY(h);

                outAllocation = Allocation.createTyped(mRS, TypeDir.create());
            }

            inAllocation.copyFrom(blockBitmap);
            mScript.forEach_saturation(inAllocation, outAllocation);
            outAllocation.copyTo(bytes);

            int idx = 0;

            for(int raster = 0; raster < h; raster++) {
                for(int m = 0; m < width; m++)
                {
                    byteline[m * channels] = bytes[idx++];
                    byteline[m * channels + 1] = bytes[idx++];
                    byteline[m * channels + 2] = bytes[idx++];
                    idx++;
                }

                ImageLineByte line = new ImageLineByte(pngr.imgInfo, byteline);
                pngw.writeRow(line);
            }
        }
        pngw.end();
    } catch (IOException e)
    {
        Log.d("BIG", "File io problem");
    }
}

根据@MiloslawSmyk 的回答,这是加载大型 JPEG 并使用 PNGJ 保存的版本:

fun processPng(forig: String, fdest: String) {
    try {
        val blockHeight = 64

        val orig = FileInputStream(forig)
        val dest = FileOutputStream(fdest)

        val decoder = BitmapRegionDecoder.newInstance(orig, false)
        val blockRect = Rect()

        val channels = 3 // needles to say, this should not be hardcoded
        val sizeOptions = BitmapFactory.Options().apply {
            inJustDecodeBounds = true
        }
        BitmapFactory.decodeFile(forig, sizeOptions)
        val height: Int = sizeOptions.outHeight
        val width: Int = sizeOptions.outWidth

        val pngw = PngWriter(dest, ImageInfo(width, height, 8, false))

        // keep compression quick
        pngw.pixelsWriter.deflaterCompLevel = 1

        blockRect.left = 0
        blockRect.right = width

        val options = BitmapFactory.Options().apply {
            inPreferredConfig = Bitmap.Config.ARGB_8888
        }
        var blockBitmap: Bitmap
        val byteLine = ByteArray(width * channels)

        for (row in 0..height / blockHeight) {
            // are we nearing the end?
            val h: Int = if ((row + 1) * blockHeight <= height)
                            blockHeight
                        else {
                            height - row * blockHeight
                        }

            blockRect.top = row * blockHeight
            blockRect.bottom = row * blockHeight + h

            blockBitmap = decoder.decodeRegion(blockRect, options)

            // convert bitmap into byte array
            val size = blockBitmap.rowBytes * blockBitmap.height
            val byteBuffer = ByteBuffer.allocate(size)
            blockBitmap.copyPixelsToBuffer(byteBuffer)
            val bytes = byteBuffer.array()

            var idx = 0

            for (raster in 0 until h) {
                for (m in 0 until width) {
                    byteLine[m * channels] = bytes[idx++]
                    byteLine[m * channels + 1] = bytes[idx++]
                    byteLine[m * channels + 2] = bytes[idx++]
                    idx++
                }

                val line = ImageLineByte(pngw.imgInfo, byteLine)
                pngw.writeRow(line)
            }
        }
        pngw.end()

    } catch (e: IOException) {
        Log.d("BIG", "File io problem")
    }

}