Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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++ array. I have used the jni set up. Most the tutorials I find are either too simple and don't explain enough or are too complicated and go over my understanding at the moment. So, can some one just point me to a simple example of an array being passed from java to c++, then have some method/function performed on the data and sent back to java.

Heres my attempt so far but I have two errors left in the way. Im not sure if the rest of the syntax is up to par either but I don't see anything at compile time.

#include <iostream>
#include <Eigen/Dense>
#include <math.h>
#include <jni.h>

using namespace Eigen;

Vector3f vec;
Vector3f vec2;
Vector3f vecRtrn;


void vecLoad(float x, float y, float z, float x2, float y2, float z2){

vec(0) = x;
vec(1) = y;
vec(2) = z;
vec2(0) = x2;
vec2(1) = y2;
vec2(2) = z2;

}

void vecAdd(Vector3f vecA, Vector3f vecB){
vecRtrn = vecA+vecB;
}

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

float array1[3];

jfloatArray flt1 = fltarray1;
jfloatArray flt2 = fltarray2;

//flt1 = env->GetFloatArrayElements( fltarray1,0);
//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];

return array1;

};
}

And these are the errors at compile time

 $ /cygdrive/c/android-ndk-r7/ndk-build
Compile++ thumb  : test <= test.cpp
jni/test.cpp: In function '_jfloatArray* Java_jnimath_act_JnimathActivity_test(JNIEnv*,    _jobject*, _jfloatArray*, _jfloatArray*)':
jni/test.cpp:42: error: cannot convert '_jfloatArray' to 'float' for argument '1' to      'void vecLoad(float, float, float, float, float, float)'
jni/test.cpp:49: error: cannot convert 'float*' to '_jfloatArray*' in return
make: *** [obj/local/armeabi/objs/test/test.o] Error 1
like image 367
James andresakis Avatar asked Dec 16 '11 23:12

James andresakis


1 Answers

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.

like image 161
Tae-Sung Shin Avatar answered Oct 20 '22 00:10

Tae-Sung Shin