通过垫的数组本土code [英] Pass an array of Mats to native code

查看:121
本文介绍了通过垫的数组本土code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本机的功能,我想使用要求的阵列7 S:

The native function I want to use requires an array of 7 Mats:

static int nativeCode(cv::Mat &inputImg, cv::Mat bufImgs[7]);

jni_part 我:

Mat& mRgba = *(Mat*)inRgba;
Mat& bufImgs[7] = *(Mat*)inBufImgs;
nativeCode(mRgba,bufImgs);

在我的Java code,我宣布我的领域:

In my Java code I'm declaring my fields:

private Mat mRgba;
private Mat[] bufImgs = new Mat[7];

我可以叫 getNativeObjAddr mRgba ,但我该怎么做,相当于为阵?

I can call getNativeObjAddr on mRgba, but How do I do the equivalent for the array?

一些背景资料:

我用OpenCV的,干的活图像处理与相机。我号召每一帧的功能需要一些额外的对象。为prevent创建和销毁这些对象的每一帧,我想,一旦创建它们,只是通过在每一个时间基准。

I'm using OpenCV, doing live image processing with the camera. The function I'm calling on each frame requires some extra objects. To prevent creating and destroying these objects every frame, I'd like to create them once, and just pass references across each time.

推荐答案

您需要的Java对象引用的数组转换成土生土长的阵列简历::垫。既然你有一个方法来获取本地对象的地址,这是相当简单的。创建在Java端一个本地方法:

You have to convert the array of Java object references into a native array of cv::Mat. Since you have a method to get the address of the native object, that's quite simple. You create a native method on the java side:

public class MyClass {
    private Mat mRgba;
    private Mat[] bufImgs = new Mat[7];

    // set the fields and all...
    // ...

    // Call the native method
    private native int callNativeCode();
}

然后在C / C ++的一面,你实施 callNative code 是这样的:

JNIEXPORT jint JNICALL Java_MyClass_callNativeCode(::JNIEnv* env, jobject thisobject)
{
    // Find the required classes
    jclass thisclass = env->GetObjectClass(thisobject);
    jclass matclass = env->FindClass("org/opencv/core/Mat");

    // Get methods and fields
    jmethodID getPtrMethod = env->GetMethodID(matclass, "getNativeObjAddr", "()J");
    jfieldID mrgbafieldid = env->GetFieldID(thisclass, "mRgba", "Lorg/opencv/core/Mat;");
    jfieldID bufimgsfieldid = env->GetFieldID(thisclass, "bufImgs", "[Lorg/opencv/core/Mat;");

    // Let's start: Get the fields
    jobject mrgba = env->GetObjectField(thisobject, mrgbafieldid);
    jobjectArray bufimgsArray = (jobjectArray)env->GetObjectField(thisobject, bufimgsfieldid);

    // Convert the array
    cv::Mat nativeBufImgs[7];
    for (int i = 0; i < 7; i++)
        nativeBufImgs[i] = *(cv::Mat*)env->CallLongMethod(env->GetObjectArrayElement(bufimgsArray, i), getPtrMethod);

    // Get the address for mRgba
    cv::Mat* mrgbaptr = (cv::Mat*)env->CallLongMethod(mrgba, getPtrMethod);

    // We're done! Call the method and return!
    return nativeCode(mrgbaptr, nativeBufImgs);
}

这篇关于通过垫的数组本土code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆