tags:

views:

53

answers:

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?

+1  A: 

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.

Chris Dennett
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.
marcus
Cool, no worries :)
Chris Dennett
A: 

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

David Sauter