Ask Your Question

Wrong data after passing matrix native address to JNI function

asked 2013-11-18 03:37:58 -0500

itay gravatar image

updated 2013-11-18 03:50:02 -0500

berak gravatar image


I'm trying to pass a matrix to JNI function, but I've got garbage values.

I checked the native function by send and received an integer and it works fine, but when I trying to pass matrix the problem start.

this is my jni function:

JNIEXPORT void JNICALL Java_com_example_mytest1_NativeClass_DetectSpermMaskMex(JNIEnv * env, jclass cls,jlong InMatAddress, jlong OutMatAddress, int brdr,int grayThresh, int maxDist, jintArray _matrix)
        Mat* Outmat = (Mat*)OutMatAddress;
        Mat* Inmat =  (Mat*)InMatAddress;

        jint* matrixarray = env->GetIntArrayElements(_matrix,0);
        Mat matrix(701,701, CV_8UC1,(unsigned char *) matrixarray);

        int val =<int>(0,0);
        __android_log_print(ANDROID_LOG_VERBOSE, "My Error", "The value is %d",val);


and this is the Java code:

    Bitmap bmp = Bitmap.createBitmap(701, 701,Bitmap.Config.RGB_565);
    Imgproc.cvtColor(F, F, Imgproc.COLOR_GRAY2BGRA,4);
    //bmp = toGrayscale(bmp);
    Utils.matToBitmap(F, bmp);

    int[] Matarray = new int[F.width()*F.height()];

    bmp.getPixels(Matarray, 0,F.width() , 0, 0, F.width(), F.height());
    NativeClass.DetectSpermMaskMex(F.getNativeObjAddr(),F.getNativeObjAddr() ,1, 2, 3, Matarray);

while F is 701x701/8UC1 matrix.

The output of the log is:

"The value is -4343107" while the real value is 185;

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-11-18 03:44:59 -0500

updated 2013-11-18 03:46:06 -0500

int is 2bite or 4bite, I think,,, try to use char or uchar

uchar[] Matarray = new uchar[F.width()*F.height()];

p.p. if F is grayscale matrix, Can't you use for the native function ?

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-11-18 03:37:58 -0500

Seen: 146 times

Last updated: Nov 18 '13