您好我已经处理了一段时间了。我想为C ++中的cv::Rect
对象和Java中的org.opencv.core.Rect
创建一个包装器。
我在c ++中返回一个std :: vector,我希望得到一个名为RectangleVector的模板构建(%template(RectangleVector)std :: vector)。
现在我有以下代码:
%typemap(jstype) cv::Rect "org.opencv.core.Rect"
%typemap(jtype) cv::Rect "org.opencv.core.Rect"
%typemap(jni) cv::Rect "jobject"
%typemap(javaout) cv::Rect {
return $jnicall;
}
%typemap(out) cv::Rect{
jclass jRectClass = jenv->FindClass("org/opencv/core/Rect");
jmethodID jRectConstructor = jenv->GetMethodID(jRectClass, "<init>", "(IIII)V");
jobject jRect = jenv->NewObject(jRectClass, jRectConstructor, $1.x, $1.y, $1.width, $1.height);
$result = jRect;
}
%typemap(javaimports) FaceDetector "import org.opencv.core.Mat;import org.opencv.core.Rect;"
为了处理std :: vector,我跟着这个链接[SWIG (v1.3.29) generated C++ to Java Vector class not acting properly。用cv :: Rect替换Double:
%{
#include <vector>
#include <stdexcept>
#include <opencv2/opencv.hpp>
%}
%include <stdint.i>
%include <std_except.i>
namespace std {
template<class T> class vector {
public:
typedef size_t size_type;
typedef T value_type;
typedef const value_type& const_reference;
%rename(size_impl) size;
vector();
vector(size_type n);
size_type size() const;
size_type capacity() const;
void reserve(size_type n);
%rename(isEmpty) empty;
bool empty() const;
void clear();
void push_back(const value_type& x);
%extend {
const_reference get_impl(int i) throw (std::out_of_range) {
// at will throw if needed, swig will handle
return self->at(i);
}
void set_impl(int i, const value_type& val) throw (std::out_of_range) {
// at can throw
self->at(i) = val;
}
}
};
}
%typemap(javabase) std::vector<cv::Rect> "java.util.AbstractList<org.opencv.core.Rect>"
%typemap(javainterface) std::vector<cv::Rect> "java.util.RandomAccess"
%typemap(javacode) std::vector<cv::Rect> %{
public org.opencv.core.Rect get(int idx) {
return get_impl(idx);
}
public int size() {
return (int)size_impl();
}
public org.opencv.core.Rect set(int idx, org.opencv.core.Rect r) {
org.opencv.core.Rect old = get_impl(idx);
set_impl(idx, r);
return old;
}
%}
%template(RectangleVector) std::vector<cv::Rect>;
当我编译Java文件时,我收到以下错误:
RectangleVector.java:39: error: incompatible types: SWIGTYPE_p_cv__Rect cannot be converted to Rect
return get_impl(idx);
^
RectangleVector.java:45: error: incompatible types: SWIGTYPE_p_cv__Rect cannot be converted to Rect
org.opencv.core.Rect old = get_impl(idx);
^
RectangleVector.java:46: error: incompatible types: Rect cannot be converted to SWIGTYPE_p_cv__Rect
set_impl(idx, r);
我认为我应该为%typemap
实施%typemap
(javain)和cv::Rect
(in)类型地图,但我不确定该怎么做。
我基于以下链接(因为我也使用dlib,cmake和swig):
https://www.anyline.io/blog/2016/02/25/how-to-glue-java-and-c-together-using-swig-and-type-maps-part-i/ https://github.com/mit-nlp/MITIE/tree/master/mitielib/java