2016-06-07 82 views
4

我想传递一个简单的java数组到c。传递简单的java数组到原始的c数组swig

现在我用下面的.i文件来做。

%module example 

%include "arrays_java.i" 
%include "example.h" 
%{ 
#include "example.h" 
%} 

与arrays_java.i标头java数组被接受。 但它使数组的完整副本,这对我来说很慢。 我试图用这些构建一个typemap,我可以使用GetDoubleArray函数来获取java数组的指针。 在我使用swig之前,我使用JNI GetDoubleArrayElements构建了自己的包装,而且速度更快。

下面您可以看到我尝试在类型映射中使用Jni函数。

%typemap(jtype) (double *values, int lenght) "double[]" 
%typemap(jstype) (double *values, int lenght) "double[]" 
%typemap(javain) (double *values, int lenght) "$javainput" 
%typemap(jni) (double *values, int lenght) "jdoubleArray" 
%typemap(in) (double *values, int lenght) { 

    $1 = (double *)JCALL2(GetDoubleArrayElements, jenv, $input, NULL); 
    $2 = (int) JCALL1(GetArrayLength,  jenv, $input); 
} 

%apply (double * DOUBLE, int LENGTH) { (double * doubleArray, long len) }; 

但痛饮建立我只是一个javaclass(SWIGTYPE_p_double),我应该使用。 我只想传递一个简单的java数组,并在c中使用这个java数组指针。

希望你能帮助我THX

编辑:

在这里你可以看到我的完整.i文件。我纠正了长久的失败。同样的问题

%module exampleModule 
%include "std_string.i" 
%include "std_vector.i" 
%include "arrays_java.i" 
#include <string> 
#include <vector> 





%template(nativeDoubleVector) std::vector<double>; 



%typemap(jtype) (double *values, int length) "double[]" 
%typemap(jstype) (double *values, int length) "double[]" 
%typemap(javain) (double *values, int length) "$javainput" 
%typemap(jni) (double *values, int length) "jdoubleArray" 
%typemap(in) (double *values, int length) { 

    $1 = (double *)JCALL2(GetDoubleArrayElements, jenv, $input, NULL); 
    $2 = (int) JCALL1(GetArrayLength,  jenv, $input); 
} 

%apply (double * values, int length) { (double * doubleArray, long len) }; 




/* Let's just grab the original header file here */ 
%include "example.h" 
%{ 
    #include "example" 
%} 

和.h文件

class example 
{ 
public: 
    example(int width, int height); 
    ~example(); 
    test(double *values, int length); 
}; 

是我能看到-Wall输出?我尝试它,但我成为没有输出在Android工作室终端

+0

看起来像拼写单词“length”时出现拼写错误,但也存在更多问题。使用-Wall参数运行SWIG,并记下我很确定您会在'%apply'上看到的警告。你可以向我们展示example.h和包含你的类型映射的.i文件吗? – Flexo

+0

我编辑了我的帖子。 thx回答 –

回答

0

我得到了第一个一半的解决方案。

//java float[] in c float arr 
%typemap(jstype) double[] "double[]" 
%typemap(jtype) double[] "double[]" 
%typemap(jni) double[] "jdoubleArray" 
%typemap(javain) double[] "$javainput" 
%typemap(in) double[] { 
    $1 = JCALL2(GetDoubleArrayElements, jenv, $input, NULL); 
} 

此代码允许我使用java数组而不需要它的副本。 但我不能改变c侧的值,而java侧知道它。 Java方面有旧值。

我知道故障。在wrap.cpp中,函数ReleasDoubleArrayElements缺失。 当我在wrap.cpp中插入它时,一切都很好。在下面你可以看到由swig生成的wrap.cpp。

SWIGEXPORT void JNICALL  Java_com_example_glm_ndk_1swig_1example_exampleJNI_Circle_1testfunktion(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jfloatArray jarg2, jint jarg3) { 
    Circle *arg1 = (Circle *) 0 ; 
    float *arg2 ; 
    int arg3 ; 

    (void)jenv; 
    (void)jcls; 
    (void)jarg1_; 
    arg1 = *(Circle **)&jarg1; 
    { 
    arg2 = jenv->GetFloatArrayElements(jarg2, NULL); 
    } 
    arg3 = (int)jarg3; 
    (arg1)->testfunktion(arg2,arg3); 
    jenv->ReleaseFloatArrayElements(jarg2, arg2, NULL);//insert manually 

}

但我必须告诉痛饮插入这个功能?

编辑:

我得到了解决方案。谢谢Flexo。你帮助我的另一个线程。 python arg out 我必须使用%typemap(argout)。下面你可以看到我的解决方案为我工作。

%typemap(jstype) double[] "double[]" 
%typemap(jtype) double[] "double[]" 
%typemap(jni) double[] "jdoubleArray" 
%typemap(javain) double[] "$javainput" 
%typemap(in) double[] { 
    $1 = JCALL2(GetDoubleArrayElements, jenv, $input, NULL); 
} 
%typemap(argout) double[]{ 
    JCALL3(ReleaseDoubleArrayElements,jenv, $input, $1, 0); 
} 

这比将数组复制到c要快得多。