Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Wrong data after passing matrix native address to JNI function

Hi,

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 = matrix.at<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;

click to hide/show revision 2
retagged

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

berak gravatar image

Wrong data after passing matrix native address to JNI function

Hi,

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 = matrix.at<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;