returning multiple values from a native function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

returning multiple values from a native function

Qianqian Fang
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

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/w2FR5qfr0okJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: returning multiple values from a native function

niko20
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

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/4d22sQ7Yzo0J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.