Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在RenderScript中旋转图像_Android_Matrix_Rotation_Imageview_Renderscript - Fatal编程技术网

Android 在RenderScript中旋转图像

Android 在RenderScript中旋转图像,android,matrix,rotation,imageview,renderscript,Android,Matrix,Rotation,Imageview,Renderscript,我需要在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);

我需要在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度。如果我尝试使用“direction”=4,它不会起任何作用。我举了一个例子,在这个例子中,它会一次又一次地旋转一个网格

我想出了如何使用RSSurfaceView来实现这一点,但遗憾的是,这个类被弃用了,因此,我无法再将renderscript用于图形。

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);
}
rs\u分配;
rs_分配痛风;
rs_脚本gScript;
整数宽度;
内部高度;
int方向=0;//0-水平翻转,1-垂直翻转
浮动旋转;
浮动框架宽度;
浮动框架高度;
void init(){
旋转=0.0f;
}
空根(常数uchar4*v_in,uchar4*v_out,常数void*usrData,uint32\u t x,uint32\u t y){
如果(方向==4){//向右旋转
rs_矩阵x4矩阵;
rsMatrixLoadIdentity(矩阵和矩阵);
rsMatrixTranslate(和矩阵,框架宽度/2.0f,框架高度/2.0f,0.0f);
rsMatrixRotate(和矩阵,旋转,0.0f,0.0f,1.0f);
rsMatrixTranslate(和矩阵,-gImageWidth/2.0f,-gImageHeight/2.0f,0.0f);
float4 in_vec={x,y,0.0f,1.0f};
float4 trans=rsMatrixMultiply(&matrix,in_vec);
浮动trans_x=trans.x;
浮动变速箱y=变速箱y;
if(trans_x<0.0f){
trans_x=0.0f;
}
else if(trans_x>=gImageWidth){
trans_x=框架宽度-1.0f;
}
如果(变速箱y<0.0f){
trans_y=0.0f;
}
否则如果(变速箱y>=框架高度){
变速箱y=框架高度-1.0f;
}
const uchar4*element=rsGetElementAt(gIn,trans\u x,trans\u y);
*v_out=*元素;
//rsgProgramVertexLoadModelMatrix(&matrix);
}否则,如果(方向==5){//向右旋转
常量uchar4*元素=rsGetElementAt(gIn,y,高度-x);
float4 color=RS8888(*元素);
float4输出={color.r,color.g,color.b};
*v_out=rsPackColorTo8888(输出);
}
}
空过滤器(){
gImageWidth=rsAllocationGetDimX(gIn);
gImageHeight=rsAllocationGetDimY(gIn);
rsForEach(gScript,杜松子酒,痛风,0);
}