从 JNI/NDK 将 2D 原始数组从 C 返回到 Java

Return a 2D primitive array from C to Java from JNI/NDK(从 JNI/NDK 将 2D 原始数组从 C 返回到 Java)
本文介绍了从 JNI/NDK 将 2D 原始数组从 C 返回到 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我找到了大量关于如何在 JNI 中生成 2D 原始数组并将其返回给 Java 的文档.但是这些信息无法描述如何在 C 中给定上下文来传递已经存在的 2D 浮点数组 (float**).

I have found large amounts of documentation on how to generate a 2D primitive array in JNI and returning it to Java. But these pieces of information fail to describe how to pass an already existing 2D float array (float**) given a context in C.

为了明确描述我的问题,我将添加一些我想要实现的 C 伪代码:

To describe my issue explicitly, I'll add some C pseudo code of what I would like to implement:

// Returns a 2D float array from C to Java
jfloatArray ndk_test_getMy2DArray(JNIEnv* env, jobject thiz, jlong context)
{
    // Cast my context reference
    MyContextRef contextRef = (MyContextRef) context;

    // In case we need it below
    unsigned int length = MyContextGet1DLength(contextRef);

    // Get the 2D Array we want to "Cast"
    float** primitive2DArray = MyContextGet2DArray(contextRef);

    // Hokus pokus...
    // We do something to create the returnable data to Java
    //
    // Below is the missing piece that would convert the primitive
    // 2D array into something that can be returned consumed and consumed
    // by Java

    jfloatArray myReturnable2DArray

    return myReturnable2DArray;
}

我假设这不是直截了当的,因为我找不到任何描述这种情况的东西.

I'm assuming this is not straight forward, given I haven't been able to find anything describing this scenario.

感谢您提供任何有用的信息.

Thanks for any helpful information.

推荐答案

感谢 Timo 的帮助和链接.对于后代,我将添加一个完整的代码集,该代码集将通过从现有的 C 2D 原始数组生成 Java 可使用的 2D 原始数组的过程.

Thanks Timo for your help and link. For posterity, I'm adding a complete code set that would go through the process of generating a 2D primitive array consumable by Java, from an existing C 2D primitive array.

// Returns a 2D float array from C to Java
jobjectArray ndk_test_getMy2DArray(JNIEnv* env, jobject thiz, jlong context)
{
    // Cast my context reference
    MyContextRef contextRef = (MyContextRef) context;

    // Get the length for the first and second dimensions
    unsigned int length1D = MyContextGet1DLength(contextRef);
    unsigned int length2D = MyContextGet2DLength(contextRef);

    // Get the 2D float array we want to "Cast"
    float** primitive2DArray = MyContextGet2DArray(contextRef);

    // Get the float array class
    jclass floatArrayClass = (*env)->FindClass(env, "[F");

    // Check if we properly got the float array class
    if (floatArrayClass == NULL)
    {
        // Ooops
        return NULL;
    }

    // Create the returnable 2D array
    jobjectArray myReturnable2DArray = (*env)->NewObjectArray(env, (jsize) length1D, floatArrayClass, NULL);

    // Go through the firs dimension and add the second dimension arrays
    for (unsigned int i = 0; i < length1D; i++)
    {
        jfloatArray floatArray = (*env)->NewFloatArray(env, length2D);
        (*env)->SetFloatArrayRegion(env, floatArray, (jsize) 0, (jsize) length2D, (jfloat*) primitive2DArray[i]);
        (*env)->SetObjectArrayElement(env, myReturnable2DArray, (jsize) i, floatArray);
        (*env)->DeleteLocalRef(env, floatArray);
    }

    // Return a Java consumable 2D float array
    return myReturnable2DArray;
}

这篇关于从 JNI/NDK 将 2D 原始数组从 C 返回到 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)