Getting an int array in JNI
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?
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.
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
精彩评论