从本地code回到垫目标在OpenCV的在java [英] Returning Mat object from native code to java in OpenCV

查看:136
本文介绍了从本地code回到垫目标在OpenCV的在java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OpenCV的Andr​​oid应用程序。它的大多数code是在Java中,但我有 一个功能是在C 该函数获取一个垫对象,并返回一个新的。

I have an OpenCV Android app. Most of its code is in Java but I have one function that is in C. The function gets a Mat object and returns a new one.

我的问题是我怎么返回垫从本地code到Java? 找不到任何例子。

My question is how do I return a Mat from the native code to Java? Couldn't find any example of that.

感谢。

推荐答案

今天,我不得不返回从本地code一垫。我开始与教程2高级 - 2.混合的Java +本地OpenCV的,它已经将两个垫(从相机拍摄的图像)对象以本地code。 但是,我想回提取的特征,因此我加入 jlong​​ addrDescriptor 的签名:

Today I had to return a Mat from native code. I started with "Tutorial 2 Advanced - 2. Mix Java+Native OpenCV" it already passes two Mat (Images captured from camera) objects to the native code. But I wanted to return extracted feature, thus I added jlong addrDescriptor to the signature:

extern "C" {
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_Sample4View_FindFeatures(JNIEnv* env, jobject thiz, jlong addrGray, jlong addrRgba, jlong addrDescriptor)
{
    Mat* pMatGr=(Mat*)addrGray;
    Mat* pMatRgb=(Mat*)addrRgba;
    Mat* pMatDesc=(Mat*)addrDescriptor;
    vector<KeyPoint> v;

    //OrbFeatureDetector detector(50);
    OrbFeatureDetector detector;
    OrbDescriptorExtractor  extractor;
    detector.detect(*pMatGr, v);
    extractor.compute( *pMatGr, v, *pMatDesc );
    circle(*pMatRgb, Point(100,100), 10, Scalar(5,128,255,255));
    for( size_t i = 0; i < v.size(); i++ ) {
        circle(*pMatRgb, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(255,128,0,255));
    }
    }
}

在Java的一部分,我加了垫子

In the java part I added the Mat

private Mat descriptor;
descriptor = new Mat();

方法 getNativeObjAddr()的伎俩。该垫被分配在java和它的地址被传递到本地code,因而没有任何明确的返回

The method getNativeObjAddr() does the trick. The Mat is allocated in java and its address is passed to the native code, thus there isn't any explicit returning.

FindFeatures(mGraySubmat.getNativeObjAddr(), mRgba.getNativeObjAddr(), descriptor.getNativeObjAddr());
Log.i("desc:"  , descriptor.dump());

在垫子里充满了所需的数据,是在JNI invokation返回后的Java code直接访问。

The Mat was filled with the required data and is directly accessible in the java code after the JNI invokation returns.

Somwhere否则在C垫$ C $发布:

Somwhere else in the code the Mat is released:

if ( descriptor != null) 
  descriptor.release();
descriptor = null;

这篇关于从本地code回到垫目标在OpenCV的在java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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