1

我需要在渲染脚本中旋转图像,并且我有以下代码:

private ScriptC_flip mScript;
Button flip = (Button)view.findViewById(R.id.flipVertical);
flip.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        mScript.set_direction(1);
        flip();
    }
});
mBitmapIn = loadBitmap(R.drawable.face2);

in = (ImageView) view.findViewById(R.id.displayin);
in.setImageBitmap(mBitmapIn);

createScript();

需要以下功能:

protected void flip() {
    mScript.invoke_filter();
    mOutAllocation.copyTo(mBitmapIn);

    mRS.destroy();
    mInAllocation.destroy();
    mOutAllocation.destroy();
    mScript.destroy();

    createScript();
    in.invalidate();
}

private void createScript() {
    mRS = RenderScript.create(getActivity());

    mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
            Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType());

    mScript = new ScriptC_flip(mRS, getResources(), R.raw.flip);
    mScript.set_width(mBitmapIn.getWidth());
    mScript.set_height(mBitmapIn.getHeight());
    mScript.set_gIn(mInAllocation);
    mScript.set_gOut(mOutAllocation);
    mScript.set_gScript(mScript);

}

private Bitmap loadBitmap(int resource) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    return BitmapFactory.decodeResource(getResources(), resource, options);
}

这是我的 RenderSCript 代码:

#pragma version(1)
#pragma rs java_package_name(com.example.android.rs.hellocompute)

rs_allocation gIn;
rs_allocation gOut;
rs_script gScript;
int width;
int height;
int direction = 0; // 0 - flip horizontally, 1 - flip vertically
float rotation;

void init(){
    rotation = 0.0f;
}

void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {
    if(direction == 4){ // rotate right
        const uchar4 *element = rsGetElementAt(gIn, x, y);
        float4 color = rsUnpackColor8888(*element);
        float4 output = {color.r, color.g, color.b};
        *v_out = rsPackColorTo8888(output);

        rs_matrix4x4 matrix;
        rsMatrixLoadIdentity(&matrix);
        rsMatrixTranslate(&matrix, 100.0f, 100.0f, 0.0f);
        rsMatrixRotate(&matrix, rotation++, 0.0f, 0.0f, 1.0f);
        //       rsgProgramVertexLoadModelMatrix(&matrix);
    }else if(direction == 5){ // rotate right
        const uchar4 *element = rsGetElementAt(gIn, y, height - x);
        float4 color = rsUnpackColor8888(*element);
        float4 output = {color.r, color.g, color.b};
        *v_out = rsPackColorTo8888(output);
    }
}

void filter() {
    rsForEach(gScript, gIn, gOut, 0);
}

如果我尝试对这一行进行注释:

//       rsgProgramVertexLoadModelMatrix(&matrix);

我收到此方法不存在的错误。为什么会这样?我在其他渲染脚本示例中使用了它。唯一的区别是那里我有一个 RSSurfaceView,在这里,我将结果设置在图像视图上。现在我怎样才能让它旋转?如果我将“方向”设置为 5,那么它会向右旋转 90 度。如果我尝试使用“direction”= 4,它不会做任何事情。我从一个例子中得到了这个,它会一遍又一遍地旋转一个网格

4

2 回答 2

1

我想出了如何使用 RSSurfaceView 来做到这一点,但遗憾的是,这个类已被弃用,所以我不能再将渲染脚本用于图形了。

于 2012-11-14T09:08:04.637 回答
1

rsgProgramVertexLoadModelMatrix 调用是 RenderScript 图形方面的一部分,但您在计算脚本中使用它。

通过将原始图像中的位置乘以矩阵来在计算脚本中进行旋转:

rs_allocation gIn;
rs_allocation gOut;
rs_script gScript;
int width;
int height;
int direction = 0; // 0 - flip horizontally, 1 - flip vertically
float rotation;

float gImageWidth;
float gImageHeight;

void init(){
    rotation = 0.0f;
}

void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {
    if(direction == 4){ // rotate right
        rs_matrix4x4 matrix;
        rsMatrixLoadIdentity(&matrix);
        rsMatrixTranslate(&matrix, gImageWidth/2.0f, gImageHeight/2.0f, 0.0f);
        rsMatrixRotate(&matrix, rotation, 0.0f, 0.0f, 1.0f);
        rsMatrixTranslate(&matrix, -gImageWidth/2.0f, -gImageHeight/2.0f, 0.0f);

        float4 in_vec = {x, y, 0.0f, 1.0f};
        float4 trans = rsMatrixMultiply( &matrix, in_vec);

        float trans_x = trans.x;
        float trans_y = trans.y;

        if ( trans_x < 0.0f) {
            trans_x = 0.0f;
        }
        else if ( trans_x >= gImageWidth) {
            trans_x = gImageWidth - 1.0f;
        }
        if ( trans_y < 0.0f) {
            trans_y = 0.0f;
        }
        else if ( trans_y >= gImageHeight) {
            trans_y = gImageHeight - 1.0f;
        }

        const uchar4 *element = rsGetElementAt(gIn, trans_x, trans_y);
        *v_out = *element;


       //rsgProgramVertexLoadModelMatrix(&matrix);
    }else if(direction == 5){ // rotate right
        const uchar4 *element = rsGetElementAt(gIn, y, height - x);
        float4 color = rsUnpackColor8888(*element);
        float4 output = {color.r, color.g, color.b};
        *v_out = rsPackColorTo8888(output);
    }
}

void filter() {
    gImageWidth = rsAllocationGetDimX(gIn);
    gImageHeight = rsAllocationGetDimY(gIn);
    rsForEach(gScript, gIn, gOut, 0);
}
于 2012-12-18T10:42:44.887 回答