Using JNI

Mar 22, 2010 at 1:48 AM
Edited Mar 22, 2010 at 2:38 AM



I have very limited JNI experience and am trying to use CharLS methods from my Java code. 

I am having trouble figuring out how to convert from the custom structs like JLS_ERROR and JlsParameters to java obj ects and vice versa. Could somebody please help me out.

I need the glue code to call the C++ function. 




PS: more specifically, the method needs a pointer to pdataUncompressed (a pointer to the uncompressed bitmap image to encode). I have a 16bit monochrome image and I can have the pixel values in a short buffer, short[][], Raster, BufferedImage or create a Bitmap. 

How do I convert any one of these Java instances to C++ bitmap object ?

Mar 22, 2010 at 11:17 PM


Thanks for taking an interest in CharLS. I know not too much about JNI, but I'll try and answer as best as I can. From what I recall from JNI, you're supposed to define Java methods signatures (that accept Java types), which you can implement in C (or C++). Perhaps the most straightforward way is to stay close to the original API and use/define equivalents in Java, that would be:

  • a JlsParameters Java class (need to define yourself)
  • an int to pass the JLS_ERROR (need to define constants) 
  • a java byte/short array for the pixeldata (a single dimensional array for the entire image)

The actual pixel data (both in and output) is passed as a pointer to CharLS. You should copy them back/forward to a C/C++ array or pin them in memory (which I understand is possible nowadays with JNI).

If you're willing, I would be interested to add your glue code to CharLS going forward.

Mar 22, 2010 at 11:59 PM

Thanks for the reply.

I found 2 sources on the internet that give great tutorials for JNI. These would be helpful for anyone trying to get started with JNI.

and this book by the creator of JNI is a must have too (its free!) :

I think I will be able to go forward from here. Once my glue code is working and I test it, I'd be glad to contribute to CharLS. I will be able to confirm success in about two days time :D


Thanks again,


Apr 1, 2010 at 3:19 AM


 I have tried and tested this code with 8 bit data and it works like a charm! Thanks devs for this amazing library.

There is a bug with JNI. It cannot use "export" methods defined at different places in the same dll. interface.cpp already has exported functions. If I add another file (JNIinterface.cpp say) and export some more functions for my java code, they are not detected. 

There are 2 possible work arounds for this:

1. create a separate C++ project that takes CharLS.dll as a references library. You'll have to include two dll files in your java path; CharLS.dll and YourProject.dll

2. Add your JNI methods to interface.cpp itself. This is what I did because of its obvious simplicity. For the purpose of sharing I can send you separate cpp files with my JNI glue code but ultimately the user will have to choose one of the two methods. 


My JNI code will also be supplemented by the Java declaration of those methods, which should be a part of the Java code. I can write all of this in a bit of detail if you wish. Let me know how you would like me to share the code ?