Native memory mapped BufferedImage with NIO

Dear all,

I work on an application which grabs/creates images in a native DLL

(written in C++) and displays them in a Java Swing GUI, using

a class which displays a given BufferedImage.

This, of course, involves some conversions:

1) On the Java side: Call

((DataBufferByte)bufferedImage.getRaster().getDataBuffer()).getData()

to obtain a "pointer" to the byte array which backs the BufferedImage.

2) Pass that pointer to the JNI side and copy the contents of the native

image into that arry, something like

env->SetByteArrayRegion( (jbyteArray)target, 0, size, native_data );

I wonder whether copying in step 2) could be avoided.

java.nio is said to provide direct byte buffers for exactly this purpose.

However, I could not figure out the details, so I need some help.

Seems like there are two possibilites:

1) On the JNI side, create a ByteBuffer with NewDirectByteBuffer();

hopefully, this buffer is backed by an array (?!), so that one

can call the array() method on the Java side and use this byte[]

to create a BufferedImage.

2) Create a memory mapped file / MappedByteBuffer on the Java side;

get its address calling GetDirectBufferAddress() on the JNI side,

and use this memory area for the native images.

Both approaches seem error-prone to me; sure I'm missing something?

Any help is highly appreciated.

- Roland

[1495 byte] By [rrichter74] at [2007-9-30 20:31:54]
# 1
No idea about NIO, but JNI provides functions which directly use a buffer.
jschell at 2007-7-7 1:21:30 > top of Java-index,Core,Core APIs...