如何从JNI垫返回数组到Java [英] how to return array of Mat from JNI to Java

查看:195
本文介绍了如何从JNI垫返回数组到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能写我的C ++ JNI函数,以便它返回垫数组到Java code?
我在编程的Andr​​oid环境,NDK的帮助下也使用OpenCV的一些功能。

How can I write my c++ JNI function so that it returns an array of Mat to Java code? I am programming in Android environment, with the help of NDK to use also some functions of OpenCV.

我的C ++函数是:

 JNIEXPORT void JNICALL Java_com_micaela_myapp_MainActivity2_getFrames(JNIEnv* env, jobject object, jstring path)
{
    const char *str;
    str = env->GetStringUTFChars(path, NULL);   
    VideoCapture input_video;
    if(input_video.open(str)){
        cout<<"Video File Opened"<<endl;
    }else{
        cout<<"Video File Not Found"<<endl;
    }
    Mat image;
    Mat frameBuffer[1000];  
    int i=0;
    while(input_video.read(image)==true){
        image.copyTo(frameBuffer[i]);
        i++;
    }
}

在Java中我有:

In Java I have:

static{
    System.loadLibrary("myapp");
}
public static native void getFrames(String path);

这个函数现在返回void,并且工作正常。不过,我的目的是为了从中获得数组帧缓冲,以便在Java中使用它。我怎样才能做到这一点?

This function now returns void and works properly. However, my purpose is to obtain the array frameBuffer from it, in order to use it in Java. How can I do this?

推荐答案

解决方案之一是分配在Java中同样大小的数组,它传递给你的本地 getFrames()功能,充气对象单独使用帧缓冲区。请参阅这个帖子获取传递的一个例子数组本土code和的一种方式来夸大一个Java 垫这个 从本机之一。

One solution is to allocate an array of equal size in Java, pass it to your native getFrames() function, and inflate the Mat objects individually using your frame buffer. Please see this post for an example of passing an array to native code, and this one for a way to inflate a Java Mat from a native one.

如果你真的需要创建本地code数组并返回它,请看看通过JNI是可用的 NewObjectArray 方法。 (<一href=\"http://stackoverflow.com/questions/11666821/array-of-object-array-2d-arrays-jni\">Example)

If you really need to create the array in native code and return it, please have a look at the NewObjectArray method that's available through JNI. (Example)

这篇关于如何从JNI垫返回数组到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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