Passing an array of Mat addresses from Android to JNI

asked 2014-10-14 06:41:03 -0600

I have created an Android application that capture photo using Android Camera API and creates an Mat object from the Bitmap returned after each capture. I want to be able to send an array of Mat object to JNI. After some research I found out that I can retrieve the Mat object address using getNativeObjAddr(). So I've tried to add every Mat object address to a long array after every capture and pass this array to JNI as an argument method.In the native code I've created for every a Mat& for every Mat object address passed from android side and write the Mat to my phone storage. The problem is that the last captured picture is saved in place of every picture captured.Can anybody help me with a solution about how can I pass all the Mat objects created after capturing to JNI.

This is the Java code :

private long[] matAddr = new long[3];
private int matAddrIndex = 0;

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

    if (bitmap == null) {
        Toast.makeText(mContext.getApplicationContext(), "not taken",
                Toast.LENGTH_SHORT).show();
    } else {
        Bitmap bitmap32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        Mat bitmapToMat = new Mat();
        Utils.bitmapToMat(bitmap32, bitmapToMat);
        matAddr[matAddrIndex] = bitmapToMat.getNativeObjAddr() ;
        matAddrIndex++;
        Toast.makeText(mContext.getApplicationContext(), "taken",
                Toast.LENGTH_SHORT).show();

    }
    mCamera.startPreview();
}

The native code :

JNIEXPORT void JNICALL Java_opencv_arrayofmatadrr_nativecode_NativeCode_ArrayOfMatAddr(JNIEnv * env, jobject callingObject, jlongArray pImagesArray) {

    string first_image_name="/storage/sdcard0/Pictures/1.jpg";
    string second_image_name = "/storage/sdcard0/Pictures/2.jpg";
    string third_image_name = "/storage/sdcard0/Pictures/3.jpg";

    vector<Mat> imgsVector;
    jsize arrayLength = env->GetArrayLength(pImagesArray);
    jlong *imagesArrayData = env->GetLongArrayElements(pImagesArray, 0);

    Mat& matImage = *(Mat*)imagesArrayData[0];
    imwrite(first_image_name, matImage);
    Mat& matImage2 = *(Mat*)imagesArrayData[1];
    imwrite(second_image_name, matImage2);
    Mat& matImage3 = *(Mat*)imagesArrayData[2];
    imwrite(third_image_name, matImage3);}
edit retag flag offensive close merge delete