Skip to content Skip to sidebar Skip to footer

C++ And Jni - How To Pass An Array Into A Jfloatarray

I have been messing with my own little project to teach myself the android ndk using c++ and jni but I can't figure out how to pass the data from a java float array to the c++ arra

Solution 1:

First you can't use jfloatArray directly. Instead, you should do this

JNIEXPORT jfloatArray JNICALL Java_jnimath_act_JnimathActivity_test
(JNIEnv *env, jobject obj, jfloatArray fltarray1, jfloatArray fltarray2)
{

jfloatArray result;
 result = env->NewFloatArray(3);
 if (result == NULL) {
     returnNULL; /* out of memory error thrown */
 }

jfloat array1[3];
jfloat* flt1 = env->GetFloatArrayElements( fltarray1,0);
jfloat* flt2 = env->GetFloatArrayElements( fltarray2,0);


vecLoad(flt1[0], flt1[1], flt1[2], flt2[0], flt2[1], flt2[2]);
vecAdd(vec, vec2);

array1[0] = vecRtrn[0];
array1[1] = vecRtrn[1];
array1[2] = vecRtrn[2];

env->ReleaseFloatArrayElements(fltarray1, flt1, 0);
env->ReleaseFloatArrayElements(fltarray2, flt2, 0);
env->SetFloatArrayRegion(result, 0, 3, array1);
return result;

}

Please use this as a tutorial and study more. As I said before, studying will help you more than practicing at this time.

Solution 2:

This is basically enough for create an empty array with new ndks. Assuming env is your jni environment.

jfloatArray jArray = env -> NewFloatArray(8);

Solution 3:

validateAudio(JNIEnv* env, jobject obj, jstring resourceFolderPath, ,jfloatArray thresholdArray){
    constchar *resource_folder_path = (*env)->GetStringUTFChars(env,resourceFolderPath,0); // string parameterconst jfloat* threshold_array = (*env)->GetFloatArrayElements(env, thresholdArray,0);  //float array
}

Solution 4:

Instead the older style (above Tae-Sung Shin's code, still works), we should do this nowadays:

jfloatArray result;
result = (*env)->NewFloatArray( env, numbers_here );

Post a Comment for "C++ And Jni - How To Pass An Array Into A Jfloatarray"