Java Android Renderscript类型与U8_4不匹配
我试图从中实现Renderscript 因此,我的Renderscript如下所示:Java Android Renderscript类型与U8_4不匹配,java,android,renderscript,Java,Android,Renderscript,我试图从中实现Renderscript 因此,我的Renderscript如下所示: #pragma version(1) #pragma rs java_package_name(bavarit.app.cinnac) rs_allocation inImage; int inWidth; int inHeight; uchar4 __attribute__ ((kernel)) rotate_90_clockwise (uchar4 in, uint32_t x, uint32_t y)
#pragma version(1)
#pragma rs java_package_name(bavarit.app.cinnac)
rs_allocation inImage;
int inWidth;
int inHeight;
uchar4 __attribute__ ((kernel)) rotate_90_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = inWidth - 1 - y;
uint32_t inY = x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) rotate_270_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = y;
uint32_t inY = inHeight - 1 - x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
Java代码如下所示:
private static Bitmap rotateBitmapNew(Bitmap bitmap, Context ctx) {
RenderScript rs = RenderScript.create(ctx);
ScriptC_imageRotation script = new ScriptC_imageRotation(rs);
script.set_inWidth(bitmap.getWidth());
script.set_inHeight(bitmap.getHeight());
Allocation sourceAllocation = Allocation.createFromBitmap(
rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT
);
bitmap.recycle();
script.set_inImage(sourceAllocation);
int targetHeight = bitmap.getWidth();
int targetWidth = bitmap.getHeight();
Bitmap.Config config = bitmap.getConfig();
Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, config);
final Allocation targetAllocation = Allocation.createFromBitmap(
rs, target, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT
);
script.forEach_rotate_90_clockwise(targetAllocation, targetAllocation);
targetAllocation.copyTo(target);
rs.destroy();
return target;
}
但是当我调用这个函数时,我得到了这个错误
android.renderscript.RSRuntimeException: Type mismatch with U8_4!
我试图调试它并找到错误,我发现源代码是ScriptC_imageRotation.java类中的这一行
public void forEach_rotate_90_clockwise(Allocation ain, Allocation aout, Script.LaunchOptions sc) {
// check ain
if (!ain.getType().getElement().isCompatible(__U8_4)) {
throw new RSRuntimeException("Type mismatch with U8_4!");
}
...
}
由于我没有使用RenderScript的经验,我只能在谷歌上搜索,但找不到与此相关的错误。也许你们当中有人有线索。我想我能说的是,分配的类型是不正确的,当我注销分配的
.getType().getElement()
时,它有点像U_5_6_5。也许这对理解这一点的人有帮助renderscript代码希望将RGBA_888
作为元素类型(因为您使用uchar4
作为输入/输出类型)。提供的位图
为RGB 565格式,因此,通过createFromBitmap()
为renderscript分配设置的元素类型被设置为RGB_565
您需要确保源位图
为ARGB 8888格式,因为您的目标位图
是使用源的位图.Config
创建的,并且RS代码希望源分配也采用此格式。是!!非常感谢,这就是我的想法,但由于我在这方面缺乏技能,我希望看到它得到确认:)没问题,很高兴能帮助你。你能告诉我如何调整RGB 565格式的renderscript吗?我必须使用哪种数据类型?我现在不知道,您必须深入了解它在分配和元素类型中是如何打包的。