Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedImage image pointer data as a JNI parameter for C function

I'm developing a Java application that uses JNI (bridge to C dynamic/shared library). In the Java side of the application, I am generating a series of images, and in the C code, I am encoding them into a video.

I know I could just save the images in a temporary file of some sort and pass the file path into C, but that seems hackish and slow.

Is it possible for me to get the BufferedImage RGB24 array data and pass that into C?

I am fairly new at Java and JNI.

like image 480
OzBarry Avatar asked Nov 29 '25 04:11

OzBarry


1 Answers

it's entirely possible to transfer byte[] from Java to C.

its actually simple but you really really need to watch your method signatures. looking for example code..

  JNIEXPORT jbyteArray passToC(JNIEnv* env, jobject obj, jbyteArray array)
  {
      int length = (*env)->GetArrayLength(env, array);
      byte data[256];
      (*env)->GetByteArrayRegion(env, array, 0, 256, data);
      //data should hold your bytes now.
  }

here's a useful link too http://docs.oracle.com/javase/1.4.2/docs/guide/jni/spec/functions.html

edit: but like i said, be very very careful about the method name, as it needs to have a package/class somewhere in it...

look at this one too, JNI Calls different in C vs C++? it'll shed great light on JNI as tutorials online never really seem to bring up the fact that JNI IS DIFFERENT FOR C++ than C clear enough...

like image 51
Shark Avatar answered Nov 30 '25 16:11

Shark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!