使用SWIG包装第三方类/数据类型 我的问题是,这里有一个C++类,其中包含第三方库(OpenCV)。我需要处理它,并在java应用程序中使用这个类,我提出了SWIG来包装所有内容,以便在java代码中使用它

使用SWIG包装第三方类/数据类型 我的问题是,这里有一个C++类,其中包含第三方库(OpenCV)。我需要处理它,并在java应用程序中使用这个类,我提出了SWIG来包装所有内容,以便在java代码中使用它,java,c++,opencv,swig,Java,C++,Opencv,Swig,它工作得很好,但是当它涉及到一个函数时,我遇到了一个问题,在这个函数中,我需要一个cv::Mat(openCV中的矩阵数据类型)作为输入参数。看看下面的 这是我的C++头信息: class bridge { public: cv::Mat IfindReciept(cv::Mat); } 我的SWIG接口文件如下所示,用于为cv::Mat数据类型定义类型映射: %typemap(jstype) cv::Mat "org.opencv.core.Mat" %typemap(jty

它工作得很好,但是当它涉及到一个函数时,我遇到了一个问题,在这个函数中,我需要一个cv::Mat(openCV中的矩阵数据类型)作为输入参数。看看下面的

这是我的C++头信息:

class bridge
{
  public:
      cv::Mat IfindReciept(cv::Mat);
}
我的SWIG接口文件如下所示,用于为cv::Mat数据类型定义类型映射:

%typemap(jstype) cv::Mat "org.opencv.core.Mat"
%typemap(jtype) cv::Mat "long"
%typemap(jni) cv::Mat "jlong"

%typemap(in) cv::Mat {
    $1 = cv::Mat($input);
}
当我通过SWIG生成包装器时,我得到一个名为SWIGTYPE_p_cv_Mat.java的文件,该文件定义了如下数据类型:

public class SWIGTYPE_p_cv__Mat {
  private long swigCPtr;

  protected SWIGTYPE_p_cv__Mat(long cPtr, boolean futureUse) {
    swigCPtr = cPtr;
  }

  protected SWIGTYPE_p_cv__Mat() {
    swigCPtr = 0;
  }

  protected static long getCPtr(SWIGTYPE_p_cv__Mat obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }
}
根据SWIG文档,这是在SWIG无法识别类型时完成的

我做错了什么?也许我监督了一些事情,因为我整晚都在做这个

不适合我


希望有人能帮助我。

嗨,要为java构建cv::Mat包装器,我做了以下工作:

%typemap(jstype) cv::Mat "org.opencv.core.Mat" // the C/C++ type cv::Mat corresponds to the JAVA type org.opencv.core.Mat (jstype: C++ type corresponds to JAVA type)
%typemap(javain) cv::Mat "$javainput.getNativeObjAddr()" // javain tells SWIG how to pass the JAVA object to the intermediary JNI class (e.g. swig_exampleJNI.java); see next step also
%typemap(jtype) cv::Mat "long" // the C/C++ type cv::Mat corresponds to the JAVA intermediary type long. JAVA intermediary types are used in the intermediary JNI class (e.g. swig_exampleJNI.java)
// the typemap for in specifies how to create the C/C++ object out of the datatype specified in jni
// this is C/C++ code which is injected in the C/C++ JNI function to create the cv::Mat for further processing in the C/C++ code
%typemap(in) cv::Mat {
    $1 = **(cv::Mat **)&$input;
}
%typemap(javaout) cv::Mat {
    return new org.opencv.core.Mat($jnicall);
}
您可以在每一行上看到描述。这将允许您在java中传递和返回Mat对象