Referencing specific classes in an external library with SWIG

和自甴很熟 提交于 2019-12-13 04:56:21

问题


Currently, I'm trying to wrap my custom C++ library for use in Java (and ultimately Android). My library uses the OpenCV Mat and Size classes as input. Below, is an example SWIG interface of what I have so far:

%module customLib

%include <std_vector.i>
%include <std_string.i>

%{
#include "opencv2/core/core.hpp"
#include "../include/myinc/CustomLib.h"
%}

namespace cv {
    class Mat {};
    class Size {};
}

namespace myinc {
    CustomType preprocessMatrix(const cv::Mat& src, cv::Mat& dst, const cv::Size& ksize);
}

OpenCV has Java wrappers for Mat and Size already; I'm wondering if what I've done here will cause naming conflicts once I add OpenCV's own Java framework. Is this the correct way to include references to an external library's classes via SWIG? Or, is there a better way to accomplish this type of thing?


回答1:


I figured this one out finally! It took me a bit to understand how SWIG's Java typemapping system worked (most of the examples gloss over some of the important concepts).

If others are having trouble understanding how to write Java typemaps, here is a way that helped me. I decided it was easiest to view the typemaps from a top-down perspective (i.e., Java -> intermediate JNI -> JNI). First, define the jstype as the type you want to see in your Java classes (e.g., module_name.java). Use javain to tell SWIG how to pass your the variable from the Java class to the intermediate JNI class (e.g., module_nameJNI.java). Next, define the jtype as the same type as javain would be. For example, $javainput.getNativeObjectAddr() returns a long, so this would become my jtype. Finally, define jni as what the actual JNI function will be using.

Below, are the SWIG typemaps I came up with for running on top of OpenCV's Java interface (as of 2.4.5):

    %typemap(jstype) cv::Mat& "org.opencv.core.Mat"
    %typemap(javain) cv::Mat& "$javainput.getNativeObjAddr()"
    %typemap(jtype) cv::Mat& "long"
    %typemap(jni) cv::Mat& "jlong"

    %typemap(in) cv::Mat& {
            $1 = *(cv::Mat **)&$input;
    }

    %typemap(jstype) cv::Size& "org.opencv.core.Size"
    %typemap(javain) cv::Size& "$javainput"
    %typemap(jtype) cv::Size& "org.opencv.core.Size"
    %typemap(jni) cv::Size& "jobject"

    %typemap(in) cv::Size& {
            jclass sizeClass = JCALL1(GetObjectClass, jenv, $input);
            jfieldID widthFieldId = JCALL3(GetFieldID, jenv, sizeClass, "width", "D");
            jfieldID heightFieldId = JCALL3(GetFieldID, jenv, sizeClass, "height", "D");
            double width = JCALL2(GetDoubleField, jenv, $input, widthFieldId);
            double height = JCALL2(GetDoubleField, jenv, $input, heightFieldId);
            $1 = new cv::Size((int)width, (int)height);
    }

    %typemap(freearg) cv::Size& {
            delete $1;
    }


来源:https://stackoverflow.com/questions/16125811/referencing-specific-classes-in-an-external-library-with-swig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!