0

I want to use function written in C in an Android app. The function is taken from here. I've learned some basics of android NDK, I can manage to use the function in my Java source, but the application freezes when I call it, than I have to wait until android offers me to kill the app.

The JNI signature is this:

JNIEXPORT void JNICALL Java_pda_lupa_callbacks_MySurfaceCallback_NativeYuv2rgb
  (JNIEnv *env, jclass clazz,
  jbyteArray imageIn, jint widthIn, jint heightIn,
  jobject imageOut, jint widthOut, jint heightOut) {

    jbyte *cImageIn = (*env)->GetByteArrayElements(env, imageIn, NULL);
    jbyte *cImageOut = (jbyte*)(*env)->GetDirectBufferAddress(env, imageOut);


    toRGB565((unsigned short*)cImageIn, widthIn, heightIn, (unsigned int*)cImageOut, widthOut, heightOut);

    (*env)->ReleaseByteArrayElements(env, imageIn, cImageIn, JNI_ABORT);
}

Then I load it in Java like this:

static {
    System.loadLibrary("rgb");
}

public native void NativeYuv2rgb(byte[] imageIn, int widthIn, int heightIn,
    java.nio.Buffer imageOut, int widthOut, int heightOut);

And when I call it, app freezes (I don't get any error):

NativeYuv2rgb(this.cameraFrame, this.prevX, this.prevY, 
              this.rgb, this.prevX, this.prevY);

I guess I might use wrong variable types for the imageIn and imageOut, but I don't know...

Thanks for your help!

edit: This is output from GDB:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 4731]
0x8083f0b0 in ?? ()
Jaa-c
  • 5,017
  • 4
  • 34
  • 64
  • I think debugging could help, I think there's a way to run GDB server on Linux in the device, and then debug it from your PC. try to investigate that. – stdcall Nov 28 '11 at 22:44
  • @Mellowcandle: thanks, I'll look into it. I'm debugging with DDM, but it doesn't give me anything useful in this case... – Jaa-c Nov 28 '11 at 22:53
  • So, GDB seems to work, see edit.. – Jaa-c Nov 30 '11 at 16:25

1 Answers1

1

OK. Segmentation fault, is a sign of bad pointer manipulation. Change the the code to the example below , and test to see if that works for you: You should change all the

jbyte *cImageIn = env->GetByteArrayElements(env, imageIn, NULL);
jbyte *cImageOut = (jbyte*) env->GetDirectBufferAddress(env, imageOut);
...
env->ReleaseByteArrayElements(env, imageIn, cImageIn, JNI_ABORT);
stdcall
  • 27,613
  • 18
  • 81
  • 125
  • This gives me errors while building it, same for all 3 lines: jni/rgb.c:63: error: request for member 'GetByteArrayElements' in something not a structure or union – Jaa-c Nov 30 '11 at 16:43
  • Because you need to write something like: env->GetByteArrayElements(imageIn, NULL); or: (*env)->GetByteArrayElements(env,imageIn, NULL) – Hitman Apr 16 '14 at 05:36