How can I create a Java Image out of an integer array that I got from native code (using JNI)?.

Davanum Srinivas

You can create an ImageIcon from a Byte Array. So all you have to do is get the Byte Array from using JNI and create an ImageIcon from it. Here's how the code looks on the java side.

import javax.swing.*;

public class MyImage 
 static { System.loadLibrary("imagelib"); }
 ImageIcon getImage()
   return new ImageIcon(getMyImage());
 static native byte [] getMyImage(); 
and here's the native code
JNIEXPORT jbyteArray JNICALL Java_MyImage_getMyImage (JNIEnv *env, jclass jC) 
 jbyte *buf ; 
 jsize size ;
 jbyteArray jb;

 // fill the buf
 buf  = ....
 size = ....

 // create the byte array
 jb=(*env)->NewByteArray(env, size);

 // fill the byte array
 (*env)->SetByteArrayRegion(env, jb, 0, size, buf);
 return jb;
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact