0

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*;

5
  • 2
    you should learn c/c++ basics ... reinterpret_cast should help Commented Sep 19, 2014 at 13:50
  • Do you have any idea how big that buffer is? And more important, how much valid data it contains? Commented Sep 19, 2014 at 13:53
  • I have tried to cast void * to jbytearray. But in android side I need Byte[]. Commented Sep 19, 2014 at 13:55
  • The raw data has been written into file. The data size depends on the image size @Deduplicator Commented Sep 19, 2014 at 13:57
  • reinterpret_cast is the natural cast to use when going back from void*, because it expresses the intent. and high level language is used primarily to communicate to humans, not to the compiler. still opinions differ and it's not technically wrong to use static_cast (it does exactly the same here), just very sub-optimal in terms of human communication, which is the main point of the named casts. Commented Sep 19, 2014 at 14:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick reply. Void * is returned from cpp layer which is JPEG data and I need this image data in Java layer through the Jni.
Java do not handle void*, the closest thing you get is a long integer to hold the pointer address, but strictly speaking it's UD. What I suggest above is to pass a ByteBuffer to java and create the Bitmap in java side, which is less messy than creating a Bitmap with JNI calling lots of Java's object methods.
If I am returning ByteBuffer to the java side from Jni, which type of return type can I use for JNI function.
your JNI function should return jobject, note that you need to manage the object reference and the real underlying buffer memory, to make GC happy. But doing proper object reference is another BIG question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.