[英]Rotate image in RenderScript
I need to rotate a image in renderscript and I have the following code:我需要在渲染脚本中旋转图像,我有以下代码:
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();
The following functions are needed:需要以下功能:
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);
}
This is my RenderSCript code:这是我的 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);
}
If I try to decomment this line:如果我尝试对这一行进行注释:
// rsgProgramVertexLoadModelMatrix(&matrix);
I get an error that this method does not exist.我收到一个错误,说这个方法不存在。 Why is this happening?
为什么会这样? I used it in other renderscript examples.
我在其他渲染脚本示例中使用了它。 the only difference is that over there i had an RSSurfaceView, here, i set the result on a image view.
唯一的区别是在那里我有一个 RSSurfaceView,在这里,我将结果设置在图像视图上。 Now how can i make it rotate?
现在我怎样才能让它旋转? If i set the "direction" to 5, then it rotates right with 90degrees.
如果我将“方向”设置为 5,那么它会向右旋转 90 度。 If i try with "direction" = 4, it doesn't do anything.
如果我尝试使用“方向”= 4,它不会做任何事情。 I took this from an example where it would rotate a mesh over and over again
我从一个例子中得到了这个,它会一遍又一遍地旋转网格
我想出了如何使用 RSSurfaceView 来做到这一点,但遗憾的是,这个类已被弃用,所以我不能再将渲染脚本用于图形。
The rsgProgramVertexLoadModelMatrix call is part of the graphics side of RenderScript but you are using it in a compute script. rsgProgramVertexLoadModelMatrix 调用是 RenderScript 图形方面的一部分,但您在计算脚本中使用它。
To do the rotation in the compute script by multiply the position in the original image by the matrix:要在计算脚本中通过将原始图像中的位置乘以矩阵来进行旋转:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.