在SWIG的外部库中引用特定类 [英] Referencing specific classes in an external library with SWIG
问题描述
目前,我试图包装我的自定义C ++库以供Java(最终Android)使用。我的库使用OpenCV Mat
和 Size
类作为输入。下面是我迄今为止的一个示例SWIG界面:
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有 Mat
和 Size
已经;我想知道我在这里做了什么会导致命名冲突一旦我添加OpenCV自己的Java框架。这是通过SWIG包括对外部库类的引用的正确方法吗?
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?
推荐答案
我想到了这一切!我花了一些时间来理解SWIG的Java类型打印系统是如何工作的(大多数示例都会覆盖一些重要的概念)。
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).
如果其他人无法理解如何编写Java类型的图形,这里是一种帮助我的方法。我决定最容易从自上而下的角度(即Java - >中间JNI - > JNI)查看类型映射。首先,将 jstype
定义为要在Java类中查看的类型(例如module_name.java)。使用 javain
告诉SWIG如何将您的变量从Java类传递到中间JNI类(例如module_nameJNI.java)。接下来,将 jtype
定义为与 javain
相同的类型。例如, $ javainput.getNativeObjectAddr()
返回 long
,因此这将成为我的 jtype
。最后,定义 jni
作为实际的JNI函数将使用什么。
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.
下面是我在OpenCV的Java界面(2.4.5版)上运行的SWIG类型图:
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;
}
这篇关于在SWIG的外部库中引用特定类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!