我需要在renderscript中旋转图像,我有以下代码:
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);
我收到此方法不存在的错误。为什么会这样?我在其他renderscript示例中使用过它。唯一的区别是那边我有一个RSSurfaceView,在这里,我将结果设置在图像视图上。现在我怎么能让它旋转?如果我将“方向”设置为5,则它会以90度右旋。如果我尝试“方向”= 4,它什么都不做。我从一个可以反复旋转网格的例子中得到了这个
答案 0 :(得分:1)
我想出了如何使用RSSurfaceView进行操作,但遗憾的是,这个类已被弃用,因此我不能再使用renderscript进行图形处理。
答案 1 :(得分: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);
}