开发者

jint problem with NDK

I'm trying to make my first native function with NDK and I'm in trouble with very basic stuff.

Please consider the following c code:

#include <jni.h>
#incl开发者_运维百科ude <string.h>

JNIEXPORT jint JNICALL Java_eu_elevelcbt_sm_YCrCbUtils_toARGB(
        JNIEnv* env, jbyteArray src, jintArray out, jint width, jint height){


    jbyte *c_src = (*env)->GetByteArrayElements(env, src, NULL);
    jint *c_out = (*env)->GetDirectBufferAddress(env, out);

    if (c_out==NULL)
        return -1;

    int length = width * height;
    int co;
    unsigned int color;

    for (co=0; co<length; co++) {
        color = c_src[co] & 0xFF;
        color = 0xFF000000 | (color<<16) | (color<<8) | color;
        c_out[co] = color;
    }

    (*env)->ReleaseByteArrayElements(env, src, c_src, 0);

    return 0;
}

JNIEXPORT jint JNICALL Java_eu_elevelcbt_sm_YCrCbUtils_sum(jint a, jint b){
    return a+b;
}

and the following Java class:

public class YCrCbUtils {

    public native int toARGB(byte[] src, int[] out, final int width, final int height);

    public native int sum(int a, int b);

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

Problem 1: If I call the second function

Log.v("DBG", "sum is: " + new YCrCbUtils().sum(10, 5));

This is what I get: "sum is 1079199776" !!!! WHY?!??!? :(

If I try calling first function like this:

int[] colors = new int[size.width * size.height]; // where width=800 and height=480
new YCrCbUtils().toARGB(data, colors, size.width, size.height); // data is a byte[]

I get a SIGSEGV error...

HELP please!!!

PS: my dev environment is MAC OSX Snow Leopard, NDK-r5b. My runtime env is Nexus One 2.3.3


...ok I'm stupid...

My methods signatures were WRONG... They always must have "JNIEnv* env, jobject obj" as first two members... well I spend an afternoon on this but the good thing is that now I'll never forget it!

Also, on my first method I had to change

jint *c_out = (*env)->GetDirectBufferAddress(env, out);

with

jint *c_out = (*env)->GetIntArrayElements(env, out, NULL);

as the previous one was returning a NULL pointer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜