C++ hBitmap -> JNI -> Java image

i’l looking for a way to transfer an image(hBitmap) i created in a C++ DLL to Java2D in a java application.
there seem to be no jni type which represent an image. i am able to send a java.awt.Canvas to the C++ DLL
and let C++ draw on the Canvas, but that is not what i want.

any ideas out there? :slight_smile:

as far as I know its not that easy and I think its a good descision that there are no native accessors which would allow that.

Why not simply get the bytes of your windows image, send it through JNI and create a BufferedImage out of the image data?

lg Clemens

is that a simple operation?
it is not my area of expertise at all. when i make a byte array of the image in C++,
shouldn’t i also transfer something like a color table to the java program?

To be honest I don’t really now about windows images so I can’t give you any tipps in how nto grab the actual pixel data out of the windows image, sorry.

But its just my recommandation to not waste time with color tables or stuff like that, just convert the image on native side to a INT_RGB format (that means first-byte form left is untouched, 2nd is red, 3rd is green and last is blue) create an array in JNI (or better a buffer) and pass it to java. In java you could draw the int[] then with drawRGB or something similar to a BUfferedImage.
However it really depends on the performance you need since there’s a lot of copying stuff involved.

lg Clemens

thanks for the feedback.
i think i can use something like “GetDIBits(hdc, hand, 0, srcehgt, srceqarr(1, 1), srcDIB, DIB_RGB_COLORS)”
to get some pixel data. the only thing to figure out is what actual data is stored in the resulting byte array.

for performance, i have no clue on what the impact will be.
current situation:
-read bitmap in C++
-draw to canvas in C++

new situation:
-read bitmap in C++
-get pixel data in C++ (very fast operation)
-return pixel data to Java (copy? i think so)
-draw image in Java (slow)

i figure most slowdown will be drawing the image in Java. Once i get this working,
i will do some benchmarking and see where the bottleneck is.

To be honest I think you’re quite a bit wrong, I think the problematic areas are somewhere else…
-read bitmap in C++ ( don’t know how fast this is)
-get pixel data in C++ (very SLOW operation if image is stored in VRAM, since a lot of traffic has to travel over several busses)
-return pixel data to Java ( a bit slow, quite a lot of JNI overhead when using arrays, NIO buffers should give decsent perfgormance withough copying)
-draw image in Java (VERY fast if you can manage the image to get accerlated)

lg Clemens

can’t you “just” create a Canvas and have your C++ component directly drawing into it ? ;D

have a look at these links :

http://www.javaworld.com/javaworld/javatips/jw-javatip86.html

and

http://java.sun.com/j2se/1.3/docs/guide/awt/AWT_Native_Interface.html

Lilian

well, you’re right. i had some time on my hands yesterday and started testing the performance, it’s not what i’m looking for.
i’m doing all my image drawing/scaling in Java now, but i have an issue with memory leaks using buffered images. i will open a new topic for that.

thanks for the help! :slight_smile: