简体   繁体   English

在Java中将C ++函数的返回类型映射为byte [] []

[英]map return type of C++ function to byte[][] in Java

I have a c++ function declared as 我有一个C ++函数声明为

unsigned char** classify 未签名的字符**分类

I am using the following interface file in SWIG 我在SWIG中使用以下接口文件

%module PWrap
%include "std_string.i"
%include "arrays_java.i"

%apply byte[][] {unsigned char**};

%{
#include "Classifier.h"
%}

%include "Classifier.h"

which generated some files, including a SWIGTYPE_p_p_unsigned_char object 生成了一些文件,包括SWIGTYPE_p_p_unsigned_char对象

Now, here's where I try to use this C++ function in Java: 现在,这里是我尝试在Java中使用此C ++函数的地方:

SWIGTYPE_p_p_unsigned_char data = pc.classify();//this works, but I can't do anything with the data object execept pass it to other C++ functions expecting unsigned char**
byte[][] data2 =pc.classify();//this does not work - throws compile time error

So what am I doing wrong to get this mapping working correctly? 那么,要使此映射正常工作,我在做错什么? I know the dimensions of the matrix, because I pass in the args to the C++ function to set everything up. 我知道矩阵的尺寸,因为我将args传递给C ++函数来设置所有东西。 In other words, I'd be happy with getting the data back in any way as long as I could cast it to byte somehow back in Java. 换句话说,只要可以将数据转换为Java中的字节,我都会很高兴以任何方式取回数据。

A char** is not a byte[][]. 字符**不是字节[] []。 Only single-dimensional arrays can decay into pointers. 只有一维数组可以衰减为指针。 What you have got back is an array of pointers to arrays, not an array of arrays. 您得到的是一个指向数组的指针数组,而不是一个数组数组。

I'd better use use direct JNI instead of SWIG to have full control over such data types. 我最好使用直接JNI而不是SWIG来完全控制此类数据类型。

Anyway returning char ** is not effective because array pinning can't be used. 无论如何,返回char **无效,因为无法使用数组固定 Probably that's why your SWIG wrapper does not do what you want - your classify should accept char ** as a parameter and not return it. 这可能就是为什么您的SWIG包装程序没有执行您想要的操作的原因-您的classify应接受char **作为参数,而不返回它。 I don't know SWIG so here is some JNI code. 我不知道SWIG,所以这里是一些JNI代码。

Java source: Java来源:

package my;

public class Classifier {
    public native void init(); // initialize _ptr with a new Classifier
    public native void cleanup(); // destroy Classifier
    public native byte[][] classify();
    private long _ptr;
}

Method definition in C/C++: C / C ++中的方法定义:

Classifier *getClassifierInstance(JNIEnv *env, jobject obj) {
    jfieldID id = env->GetFieldID(env->GetObjectClass(obj), "_ptr", "J");
    return (id == NULL) ? NULL : ((Classifier *)env->GetLongField(obj, id));
}

JNIEXPORT jobjectArray JNICALL
Java_my_Classifier_classify(JNIEnv *env, jobject obj) {
    Classifier *classifier = getClassifierInstance(env, obj);
    char **ptr = classifier->classify();
    jobjectArray result = NewObjectArray(env, MATRIX_HEIGHT, FindClass(env, "[B"), NewByteArray(env, 0));
    for (int i = 0; i < MATRIX_HEIGHT; ++i) {
        jbyteArray row = NewByteArray(env, MATRIX_WIDTH);
        SetByteArrayRegion(env, row, 0, MATRIX_WIDTH, ptr[i]);
        SetObjectArrayElement(env, result, i, row);
    }
    return result;
}

看看http://www.ibiblio.org/pub/languages/fortran/append-c.html “为什么不能将双指针用作2D数组?”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM