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

标签 android bitmap renderscript

我在 Android 上使用 renderscript 来编辑照片,目前由于 Android 上的纹理大小限制和内存限制,如果我尝试任何太大的东西(例如用设备相机拍摄的照片),应用程序将会崩溃。

我第一个想到解决这个问题是使用 BitmapRegionDecoder 并将大照片平铺成可管理的部分,通过 renderscript 编辑它们并一次保存一个,然后使用 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");
    }
}

关于android - 如何在 Android 中编辑和保存大位图?目前将其写成图 block ,但将它们重新组合在一起的速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31969996/

相关文章:

android - 将位图保存为 jpeg 图像

java - rsSendToClient 失败并出现异常

android - Renderscript 未使用构建工具 25.0.0+ 进行编译

java - Sprite ,将位图存储在数组中

android - 使用异步任务显示外部图像

android - 我们什么时候应该(真正)使用 RenderScript?

android - Jetpack 撰写 : How to avoid Button Text disappearing when Button size is small?

android - 寄存器的类型为 Long(High Half)但预期为 Long(Low Half)

android - 从Android Studio从github导入外部库的问题

android - 跨多个 Android 版本实现 SharedPreferences