Android Studio開發之JNI層開發 --- jni層返回二維數組對象

說明:本博客記錄一下,jni層返回二維PointF對象數組。

1、Java層接口定義:

public native PointF[][] GetMultiArrayPf();

2、JNI層實現:

extern "C"
JNIEXPORT jobjectArray JNICALL
Java_com_terawins_www_demo007_Java_1Interface_GetMultiArrayPf(JNIEnv *env, jobject instance) {

    // TODO
    jobjectArray oa_ret = NULL;
    jclass cls = env->FindClass("android/graphics/PointF");
    jmethodID mi_construct = env->GetMethodID(cls, "<init>", "(FF)V"); //PointF的構造函數
    float szTmp[2][3] = {0};
    int nRow = 2;
    int nCol = 3;
    for (int i = 0; i < nRow; ++i) {
        for (int j = 0; j < nCol; ++j) {
            szTmp[i][j] = i * nCol + j;
        }
    }
    jobjectArray jforGet1cls = env->NewObjectArray(nCol, cls, NULL);
    oa_ret = env->NewObjectArray(nRow, env->GetObjectClass(jforGet1cls), NULL);
    env->DeleteLocalRef(jforGet1cls);
    for (int i = 0; i < nRow; ++i) {
        jobjectArray oa_tmp = env->NewObjectArray(nCol, cls, NULL);
        for (int j = 0; j < nCol; ++j) {
            jobject ob_tmp = env->NewObject(cls, mi_construct, szTmp[i][j], szTmp[i][j]);
            env->SetObjectArrayElement(oa_tmp, j, ob_tmp);
            env->DeleteLocalRef(ob_tmp);
        }
        env->SetObjectArrayElement(oa_ret, i, oa_tmp);
        env->DeleteLocalRef(oa_tmp);
    }
    return oa_ret;
}

3、Java層測試:

String str = "";
        PointF [][]pt = mm_ji.GetMultiArrayPf();
        int nRow = pt.length;
        int nCol = pt[0].length;
        for (int i = 0; i < nRow; i++)
        {
            for (int j = 0; j < nCol; j++)
            {
                str += String.format("pt[%d][%d]: x = %f, y = %f\n", i, j, pt[i][j].x, pt[i][j].y);
            }
        }
        tv_show.setText(str);

備註:略。詳見:https://blog.csdn.net/qq_41811438/article/details/103308505

---- The End.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章