Govind
Govind

Reputation: 2348

Converting void* to byte array

I need to convert a void * which will be having a bitmap data. The void* is returned from a cpp function and what I need to do is to convert this void* which is passed to Jni and display this as a bitmap in Java.

Void * buffer = CppClass->getbuffer();
ByteArray byte[];

byte = the contents of void*;

Upvotes: 0

Views: 2156

Answers (1)

Non-maskable Interrupt
Non-maskable Interrupt

Reputation: 3911

Since java do not have void* and jni do not have ByteArray, it's not clear what's your execution environment.

Since the source of problem is the void* pixel map, I would assume you want to create a java Bitmap object with the pixels, with a mix of JNI and Java code.

First look at the Bitmap class, there is a convenient function named copyPixelsFromBuffer, looks like useful, it takes a Buffer.

Second, look at JNI function NewDirectByteBuffer, it takes a C pointer and create a ByteBuffer, which is also a Buffer needed by Bitmap.

Now it becomes clear, you just need to:

  • Create ByteBuffer with the pixel buffer with JNI code
  • Pass/return that ByteBuffer to Java land
  • Fill a Bitmap with such ByteBuffer.
  • Display it with ImageView or your paint routine.

P.S. It's left to OP's excise to handle object referencing to be GC friendly.

Upvotes: 2

Related Questions