Re: returning multiple values from a native function

373 views
Skip to first unread message

niko20

unread,
Jan 6, 2013, 11:24:58 PM1/6/13
to andro...@googlegroups.com
The code here looks OK. I would double check that you have the correct values in the first place being put into the float array when you are returning it.


On Wednesday, January 2, 2013 3:31:01 PM UTC-6, Qianqian Fang wrote:
Dear list, 

I wrote a JNI function using C and linked in my android program. The function reads in a float array buffer, and returns 4 values, the max/min and their positions in the buffer. I found some online code samples, and use jfloatArray as the return value, however, the returned values are all incorrect (program compiles and runs ok). I am wondering if I did not call this properly. 

My code sample is listed below:

in the *.c unit, I have:

JNIEXPORT jfloatArray Java_com_test_mox_SamplePlotOverlay_SignalMaxMin(JNIEnv* env,
jobject this, jfloatArray buf, jint len, jint t, jint w){

jfloatArray peaks=(*env)->NewFloatArray(env, 4);
jfloat* dat = (*env)->GetFloatArrayElements(env,buf,NULL);
jfloat extremes[4];

        ...  // loop over the array, and save results in extremes[0-4]

(*env)->ReleaseFloatArrayElements(env,buf,dat,0);
(*env)->SetFloatArrayRegion(env, peaks, 0, 4, extremes);
return peaks;
}

in the java code, I have

class SamplePlotOverlay extends View {
        float[] peaks;  // output will saved to this array
        ....
        static {
System.loadLibrary("mylib");
}
private native float [] SignalMaxMin(float[] dat, int len, int t, int w);

        public void onDraw(Canvas canvas) {
               peaks=SignalMaxMin(roiavg, SAMPLE_LEN, count, 20);
               ... // plot peaks
        }
}

can anyone let me know if there is anything obviously wrong?
any other better ways to return multiple outputs?

thanks

Qianqian
Reply all
Reply to author
Forward
0 new messages