构建opencv Rect包装器C++;Java(swig)

构建opencv Rect包装器C++;Java(swig),java,c++,opencv,swig,Java,C++,Opencv,Swig,嗨,我处理这件事已经有一段时间了。我想为C++中的 CV::Rect< /Cort>对象和 > org .opencv.Corr.RcT< 包装。 我返回一个STD::C++中的向量,我想用SWIG创建一个模板构建,称为矩形向量(%模板(矩形向量):ST::vector)。 目前,我有以下代码: %typemap(jstype) cv::Rect "org.opencv.core.Rect" %typemap(jtype) cv::Rect "org.opencv.core.Rect" %t

嗨,我处理这件事已经有一段时间了。我想为C++中的<代码> CV::Rect< /Cort>对象和 > org .opencv.Corr.RcT< <代码>包装。

我返回一个STD::C++中的向量,我想用SWIG创建一个模板构建,称为矩形向量(%模板(矩形向量):ST::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;"
我认为我应该为
cv::Rect
实现
%typemap
(javain)和
%typemap
(in)类型映射,但我不确定如何实现

我基于以下链接(因为我也在使用dlib、cmake和swig):


从外观上看,您正在创建用于按值传递的包装器,但不用于传递常量引用。您可以在同一类型映射中添加引用。例如:
%typemap(javaout)cv::Rect,cv::Rect&{…
希望这能有所帮助
%{
#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>;
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);