使用android Renderscript对洪水填充位图的算法

问题描述 投票:1回答:1

我试图制作一个填充图像颜色的应用程序。它使用Java工作正常,但由于某些性能问题,我想使用renderscript填充位图。我搜索了很多关于renderscript的东西,但我没有任何合适的东西。你能不能请各位指导我如何使用renderscript填充位图。任何帮助将不胜感激。谢谢

android performance renderscript flood-fill android-renderscript
1个回答
0
投票

你需要做的基本事情是为输入Allocation创建一个Bitmap,然后为输出创建一个可变的BitmapAllocation。假设你有一个名为inputBitmap的输入位图,它可能看起来像这样:

private RenderScript        mRsCtx;   //  RenderScript context, created during init
private ScriptC_bitmapFill  mFill;    //  RS kernel instance, created during init
.
.
.
public Bitmap doFill(Bitmap inputBitmap) {

    //  Ensure your input bitmap is also in ARGB8888
    Bitmap  output = Bitmap.createBitmap(inputBitmap.getWidth(),
                                         inputBitmap.getHeight(),
                                         Bitmap.Config.ARGB_8888);
    Allocation  outAlloc = Allocation.createFromBitmap(mRsCtx, output);
    Allocation  inAlloc = Allocation.createFromBitmap(mRsCtx, inputBitmap);

    //  Now call your kernel then copy back the results
    mFill.forEach_root(inAlloc, outAlloc);
    outAlloc.copyTo(outBitmap);
    return outBitmap;
} 

如果您只是填充整个图像甚至是一个区域,那么您将拥有一个RS内核,它将在调用内核时更改特定位置的像素值。这是一个非常简单的RS内核,只用纯色填充整个图像:

#pragma version(1)

#pragma rs java_package_name(com.example.bitmapfill)

void root(const uchar4 *v_in, uchar4 *v_out) {
    v_out->r = 0x12;
    v_out->g = 0x34;
    v_out->b = 0x56;
}

请注意,由于在这种情况下你没有真正对输入分配/位图做任何事情(只填充整个事情),你可以省略输入分配并使用维度。但是,如果你只是操纵一部分输入(一个小的子部分),那么你将不得不将其他像素从输入复制到输出而不是填充。

有关RS及其内部,性能等的其他信息,您可能会发现此演讲很有用:https://youtu.be/3ynA92x8WQo

© www.soinside.com 2019 - 2024. All rights reserved.