Hi I've been dealing with this for some time now. I would like to make a wrapper for the cv::Rect
object in C++ and the org.opencv.core.Rect
in Java.
I'm returning a std::vector in c++ and I would like to get a template build with swig called RectangleVector (%template(RectangleVector) std::vector).
For now I have the following code:
%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;"
To deal with the std::vector I followed this link [SWIG (v1.3.29) generated C++ to Java Vector class not acting properly. Replacing the Double by the 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>;
When i'm compiling the Java files I get the following error:
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);
I think I should implement the %typemap
(javain) and %typemap
(in) typesmaps for cv::Rect
but I'm not certain how to do it.
I based myself on the following links (since i'm using dlib, cmake and swig too):
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