2

I've seen some questions on how to properly pass a C array into Java using JNI, but I have the reverse problem: How do I properly call an int array getter method in C using JNI. Specifically, I want to pass a BufferedImage instance into C and call the "public int[] getRGB()" method on this BufferedImage instance.

My understanding is that all arrays are objects in Java, so I presume that I should be calling: (*env)->CallObjectMethod() in order to get this array, but when I try this, my JVM crashes. Any suggestions?

2 Answers 2

1

The env pointer is probably invalid, if you're calling from inside C++. You must bind a JVM instance manually. Something like the following in C:

JNIEnv *env;
(*g_vm)->AttachCurrentThread (g_vm, (void **) &env, NULL);

Your g_vm pointer should come from the JNI setup function call in the DLL, and you need to store it for later.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion. I think you only need to attach the current thread if you are invoking the JVM from C (not 100% about this though). I'm starting the JVM with ordinary Java classes. At any rate, I think I've solved the problem by creating a little helper method in my classes to make accessing the rgb values in BufferedImage simpler.
1

Just for the record, I think what you did was correct. The following code would do the trick I guess (I don't know what you called exactly since you didn't provide the code):

jobject jBufferedImage = ...;
...
jclass clazz = (*env)->FindClass("java/awt/Image/BufferedImage");
jmethodID jMID = (*env)->GetMethodID(clazz, "getRGB", "()[I");
jintArray rgbValues = (jintArray) (*env)->CallObjectMethod(jBufferedImageObject, jMID);

Haven't tested and compiled, but that's how I'd do it :)

Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.