【问题标题】:Wrapping a 3rd party class / data type with SWIG使用 SWIG 包装第 3 方类/数据类型
【发布时间】:2014-05-16 13:49:13
【问题描述】:

我的问题是我在这里有一个 C++ 类,其中包含一个 3rd 方库 (openCV)。我需要处理它并在 java 应用程序中使用这个类,我想出了 SWIG 将所有东西包装在一起以便在我的 java 代码中使用它。

它工作得很好,但是当它到达一个需要 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 无法识别类型时完成的。

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

Mevatron's answer 对我不起作用。

希望有人可以帮助我。

【问题讨论】:

    标签: java c++ opencv swig


    【解决方案1】:

    您好,要为 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 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-10
      • 2011-09-07
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多