Log in or register to post comments

passing QCAR::Matrix44F from C++ to Java via JNI

November 13, 2013 - 1:44am #1

Hello,

i need to do some computation Java side, and i need Vuforia to tell my Java script what the current posematrix is.

so i wrote a simple function in C++

JNIEXPORT QCAR::Matrix44F JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_getCurrentPoseMatrix(JNIEnv *, jobject)
{
    QCAR::Matrix44F modelViewMatrix;
    QCAR::State state = QCAR::Renderer::getInstance().begin();

    const QCAR::TrackableResult* result = state.getTrackableResult(0);    //we only have one trackable
    const QCAR::Trackable& trackable = result->getTrackable();
    modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(result->getPose());

    return modelViewMatrix;
}

this compiles correctly. However, how am i supposed to declare this function in Java?

public native ????? getCurrentPoseMatrix();

i then need to pass this matrix to a Matrix4f in java.

(In general, how do i pass around non primitive data?)

thanks in advance!

 

passing QCAR::Matrix44F from C++ to Java via JNI

November 15, 2013 - 8:20am #5

thanks!

passing QCAR::Matrix44F from C++ to Java via JNI

November 14, 2013 - 3:46am #4

If this can help, the following code is an example of how to deliver a C++ QCAR Matrix to Java an an array of float:

 

JNIEXPORT jfloatArray JNICALL
Java_com_qualcomm_QCAR_CameraDevice_getProjectionMatrixNative(JNIEnv* env, jobject obj, jfloat neard, jfloat fard)
{
const QCAR::CameraCalibration& cameraCalibration = QCAR::CameraDevice::getInstance().getCameraCalibration();
QCAR::Matrix44F projMat = QCAR::Tool::getProjectionGL(cameraCalibration, neard, fard);

jfloatArray projMatrixArray = env->NewFloatArray(16);
jfloat *farr = env->GetFloatArrayElements(projMatrixArray, 0);

for (int i = 0; i < 16; ++i)
{
farr[i] = projMat.data[i];
}

env->ReleaseFloatArrayElements(projMatrixArray, farr, 0);
return projMatrixArray;
}

Please feel free to adapt it to your needs.

 

And another example of how to pass a "structure" from C++ to Java is this:

JNIEXPORT void JNICALL
Java_com_qualcomm_QCAR_CameraDevice_getVideoModeNative(JNIEnv* env, jobject obj, jobject jvideoMode, jint cameraVideoMode)
{
	QCAR::VideoMode videoMode = QCAR::CameraDevice::getInstance().getVideoMode(cameraVideoMode);

	jclass videoModeClass = env->GetObjectClass(jvideoMode);
	jfieldID widthID = env->GetFieldID(videoModeClass, "width", "I");
	jfieldID heightID = env->GetFieldID(videoModeClass, "height", "I");
	jfieldID frameRateID = env->GetFieldID(videoModeClass, "frameRate", "F");

	env->SetIntField(jvideoMode, widthID, (jint)(videoMode.mWidth));
	env->SetIntField(jvideoMode, heightID, (jint)(videoMode.mHeight));
	env->SetFloatField(jvideoMode, frameRateID, (jfloat)(videoMode.mFramerate));
}

 

 

passing QCAR::Matrix44F from C++ to Java via JNI

November 14, 2013 - 2:49am #3

Hello,

yes, i did see that page before, but that doesn't really help me (it's also outdated, I think maybe the page should link to http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html instead of  http://java.sun.com/docs/books/jni/html/types.html ? ).

Anyway I need to pass a non primitive data type, which is not covered and it's weird that the java QCAR API doesn't expose such essential data java side.

Respectfully,i think  this kind of example should be covered at least in a tutorial/faq.

Guess i'll google more, too bad.

passing QCAR::Matrix44F from C++ to Java via JNI

November 13, 2013 - 7:29am #2
Log in or register to post comments