C++ and JNI - How to pass an array into a jfloatArray

后端 未结 4 590
谎友^
谎友^ 2020-12-19 10:10

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

相关标签:
4条回答
  • 2020-12-19 10:25
    validateAudio(JNIEnv* env, jobject obj, jstring resourceFolderPath, ,jfloatArray thresholdArray){
        const char *resource_folder_path = (*env)->GetStringUTFChars(env,resourceFolderPath,0); // string parameter
        const jfloat* threshold_array = (*env)->GetFloatArrayElements(env, thresholdArray,0);  //float array
    }
    
    0 讨论(0)
  • 2020-12-19 10:30

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

    jfloatArray jArray = env -> NewFloatArray(8);
    
    0 讨论(0)
  • 2020-12-19 10:40

    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 );
    
    0 讨论(0)
  • 2020-12-19 10:44

    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) {
         return NULL; /* 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.

    0 讨论(0)
提交回复
热议问题