gpt4 book ai didi

android - rs和android之间的数据传输?

转载 作者:行者123 更新时间:2023-11-30 03:57:28 25 4
gpt4 key购买 nike

我正在使用渲染脚本。我想传递一个数组的元素来呈现脚本和想要在渲染脚本中执行每个元素的平方并将数据取回安卓框架工作。

我正在尝试通过以下代码来做到这一点。

1.java代码2.RS码

但是通过这些代码这是不可能的。请告诉我我在做什么错误使用这些代码。

============================================= =============================

java代码

  public class SUM extends Activity {


private int[] input;
private int[] output;
private RenderScript mRS;
private Allocation mInAllocation;
private Allocation mOutAllocation;
private ScriptC_Sum mScript;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

input= new int[4];
input[0]=0;
input[1]=1;
input[2]=2;
input[3]=3;

output =new int[4];

createScript();
}


private void createScript() {

mRS = RenderScript.create(this);

mInAllocation = Allocation.createSized(mRS, Element.U32(mRS),4);
mOutAllocation = Allocation.createTyped(mRS,mInAllocation.getType());
mScript = new ScriptC_Sum(mRS, getResources(), R.raw.sum);

mScript.bind_v_in(mInAllocation);
mScript.bind_v_out(mOutAllocation);

mScript.invoke_square(mInAllocation,mOutAllocation);


}
}

============================================= ============RS代码

#pragma version(1)
#pragma rs java_package_name(com.cdacb.mars.summation)

#include "rs_types.rsh"
#include "rs_graphics.rsh"
#include "rs_core.rsh"

int32_t *v_in ;
int32_t *v_out;

void square(){

}

void root(int32_t *v_in,int32_t *v_out )
{

square();

}

最佳答案

几件事:

1- 我发现您的 .rs 文件不能与您的 .java 文件同名

2- 您在根函数 (*v_out) 上声明了一个输出变量,但从未在函数内部计算它。

3- 你的 java 数组都是整数。来自 Renderscript API 上的幂函数声明, 他们都至少接受一个 float 作为输入

这是我的java代码:

    public class Sum extends Activity {
private float[] input;
private float[] output;
private RenderScript mRS;
private Allocation mInAllocation;
private Allocation mOutAllocation;
private TextView t1;
private ScriptC_Square mScript;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sum);
t1= new TextView(this);
t1= (TextView)findViewById(R.id.textview1);

input= new float[4];
input[0]=0;
input[1]=1;
input[2]=2;
input[3]=3;
output =new float[input.length];
createScript();
}

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

mInAllocation = Allocation.createSized(mRS, Element.F32(mRS),4);
mOutAllocation = Allocation.createTyped(mRS,mInAllocation.getType());
mScript = new ScriptC_Square(mRS, getResources(), R.raw.square);

mInAllocation.copyFrom(input);
mScript.forEach_root(mInAllocation, mOutAllocation); // calls the forEach function to operate on the root function (each allocation input, will have a corresponding allocation output
mOutAllocation.copyTo(output); // copy the result that was stored in mOutAllocation into the array output

t1.setText(String.format("Input:%s\n\noutput:%s",
ArrayToString(input), ArrayToString(output)));
}


/**
* this function just print each element of a primitive float array into a text string
* @param array
* @return
*/
public String ArrayToString(float[] array){

String s="";
for(int i=0; i<array.length; i++){
s+= String.format("\nValue %d: %f", i, array[i]);
}

return s;
}

}

这是我的 Square.rs 文件:

#pragma version(1)
#pragma rs java_package_name(com.example.sum)//don't forget to change that package name

void root(const float *v_in, float *v_out ){

*v_out = pow(*v_in, 2); //use the internal pow function of Renderscript pow(float x, float y) = x ^y;

}

关于android - rs和android之间的数据传输?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13119809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com