Java API: added int64 and Vec3d support; small code refactoring performed

This commit is contained in:
Andrey Pavlenko 2011-07-19 08:56:31 +00:00
parent 5e3e283e30
commit 343bba93db
2 changed files with 56 additions and 54 deletions

View File

@ -116,6 +116,7 @@ type_dict = {
"double" : { "j_type" : "double", "jn_type" : "double", "jni_type" : "jdouble", "suffix" : "D" },
"size_t" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },
"__int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },
"int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },
"double[]": { "j_type" : "double[]", "jn_type" : "double[]", "jni_type" : "jdoubleArray", "suffix" : "_3D" },
"vector_Point": { "j_type" : "java.util.List<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point> %(n)s", "suffix" : "J" },
"vector_Mat" : { "j_type" : "java.util.List<Mat>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Mat> %(n)s", "suffix" : "J" },
@ -184,6 +185,10 @@ type_dict = {
"TermCriteria": { "j_type" : "TermCriteria", "jn_args" : (("int", ".type"), ("int", ".maxCount"), ("double", ".epsilon")),
"jni_var" : "TermCriteria %(n)s(%(n)s_type, %(n)s_maxCount, %(n)s_epsilon)",
"suffix" : "IID"},
"Vec3d" : { "j_type" : "double[]", "jn_args" : (("double", ".val[0]"), ("double", ".val[1]"), ("double", ".val[2]")),
"jn_type" : "double[]",
"jni_var" : "Vec3d %(n)s(%(n)s_val0, %(n)s_val1, %(n)s_val2)", "jni_type" : "jdoubleArray",
"suffix" : "DDD"},
}
@ -510,7 +515,9 @@ class JavaWrapperGenerator(object):
#include <android/log.h>
#define MODULE_LOG_TAG "OpenCV.%s"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))
#endif // DEBUG
#else //DEBUG
#define LOGD(...)
#endif //DEBUG
#include "utils.h"
""" % module)
@ -535,10 +542,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
(JNIEnv* env, jclass cls, jlong src_nativeObj, jlong mask_nativeObj)
{
try {
#ifdef DEBUG
LOGD("core::n_1minMaxLoc()");
#endif // DEBUG
jdoubleArray result;
result = env->NewDoubleArray(6);
if (result == NULL) {
@ -569,17 +573,13 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
return result;
} catch(cv::Exception e) {
#ifdef DEBUG
LOGD("core::n_1minMaxLoc() catched cv::Exception: %s", e.what());
#endif // DEBUG
jclass je = env->FindClass("org/opencv/CvException");
if(!je) je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, e.what());
return NULL;
} catch (...) {
#ifdef DEBUG
LOGD("core::n_1minMaxLoc() catched unknown exception (...)");
#endif // DEBUG
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "Unknown exception in JNI code {core::minMaxLoc()}");
return NULL;
@ -590,10 +590,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1getTextSize
(JNIEnv* env, jclass cls, jstring text, jint fontFace, jdouble fontScale, jint thickness, jintArray baseLine)
{
try {
#ifdef DEBUG
LOGD("core::n_1getTextSize()");
#endif // DEBUG
jdoubleArray result;
result = env->NewDoubleArray(2);
if (result == NULL) {
@ -624,17 +621,13 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1getTextSize
return result;
} catch(cv::Exception e) {
#ifdef DEBUG
LOGD("core::n_1getTextSize() catched cv::Exception: %s", e.what());
#endif // DEBUG
jclass je = env->FindClass("org/opencv/CvException");
if(!je) je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, e.what());
return NULL;
} catch (...) {
#ifdef DEBUG
LOGD("core::n_1getTextSize() catched unknown exception (...)");
#endif // DEBUG
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "Unknown exception in JNI code {core::getTextSize()}");
return NULL;
@ -768,33 +761,27 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1getTextSize
c_epilogue.append( "%(t)s_to_Mat( %(n)s, %(n)s_mat );" % {"n" : a.name, "t" : a.ctype} )
else:
fields = type_dict[a.ctype].get("jn_args")
if fields: # complex type
if "I" in a.out or not a.out or a.ctype in self.classes: # input arg, pass by primitive fields
for f in fields:
jn_args.append ( ArgInfo([ f[0], a.name + f[1], "", [], "" ]) )
jni_args.append( ArgInfo([ f[0], a.name + f[1].replace(".","_").replace("[","").replace("]",""), "", [], "" ]) )
if a.out and a.ctype not in self.classes: # out args, pass as double[]
jn_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
jni_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
j_prologue.append( "double[] %s_out = new double[%i];" % (a.name, len(fields)) )
j_epilogue.append("%s.set(%s_out);" % (a.name, a.name))
c_epilogue.append( \
"jdouble tmp_%(n)s[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, %(cnt)i, tmp_%(n)s);" %
{ "n" : a.name, "cnt" : len(fields), "args" : ", ".join([a.name + f[1] for f in fields]) } )
else: # primitive type
if "I" in a.out or not a.out: # input arg, pass by primitive fields
jn_args.append(a)
jni_args.append(a)
if a.out and a.ctype not in self.classes: # out args, pass as double[]
jn_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
jni_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
j_prologue.append( "double[] %s_out = new double[1];" % a.name )
j_epilogue.append("%s = %s_out[0];" % (a.name, a.name))
c_epilogue.append( \
"jdouble tmp_%(n)s[1] = {%(n)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, 1, tmp_%(n)s);" %
{ "n" : a.name } )
fields = type_dict[a.ctype].get("jn_args", ((a.ctype, ""),))
if "I" in a.out or not a.out or a.ctype in self.classes: # input arg, pass by primitive fields
for f in fields:
jn_args.append ( ArgInfo([ f[0], a.name + f[1], "", [], "" ]) )
jni_args.append( ArgInfo([ f[0], a.name + f[1].replace(".","_").replace("[","").replace("]",""), "", [], "" ]) )
if a.out and a.ctype not in self.classes: # out arg, pass as double[]
jn_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
jni_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
j_prologue.append( "double[] %s_out = new double[%i];" % (a.name, len(fields)) )
set_vals = []
i = 0
for f in fields:
set_vals.append( "%(n)s%(f)s = %(t)s%(n)s_out[%(i)i]" %
{"n" : a.name, "t": ("("+type_dict[f[0]]["j_type"]+")", "")[f[0]=="double"], "f" : f[1], "i" : i}
)
i += 1
#j_epilogue.append("%s.set(%s_out);" % (a.name, a.name))
j_epilogue.append("; ".join(set_vals) + "; ")
c_epilogue.append( \
"jdouble tmp_%(n)s[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, %(cnt)i, tmp_%(n)s);" %
{ "n" : a.name, "cnt" : len(fields), "args" : ", ".join([a.name + f[1] for f in fields]) } )
# java part:
@ -923,25 +910,19 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
($args)
{
try {
#ifdef DEBUG
LOGD("$module::$fname()");
#endif // DEBUG
$prologue
$retval$cvname( $cvargs );
$epilogue
$ret
} catch(cv::Exception e) {
#ifdef DEBUG
LOGD("$module::$fname() catched cv::Exception: %s", e.what());
#endif // DEBUG
jclass je = env->FindClass("org/opencv/CvException");
if(!je) je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, e.what());
$default
} catch (...) {
#ifdef DEBUG
LOGD("$module::$fname() catched unknown exception (...)");
#endif // DEBUG
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "Unknown exception in JNI code {$module::$fname()}");
$default

View File

@ -1,8 +1,12 @@
#include "utils.h"
#ifdef DEBUG
#include <android/log.h>
#define MODULE_LOG_TAG "OpenCV.utils.cpp"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))
#else //DEBUG
#define LOGD(...)
#endif //DEBUG
using namespace cv;
@ -15,16 +19,21 @@ void Mat_to_vector_int(Mat& mat, vector<int>& v_int)
if(mat.type()!= CV_32SC1 || mat.rows!=1)
return;
/*
for(int i=0; i<mat.cols; i++)
v_int.push_back( mat.at< int >(0, i) );
*/
v_int = (vector<int>) mat;
}
void vector_int_to_Mat(vector<int>& v_int, Mat& mat)
{
/*
mat.create(1, v_int.size(), CV_32SC1);
for(size_t i=0; i<v_int.size(); i++)
mat.at< int >(0, i) = v_int[i];
*/
mat = Mat(v_int);
}
@ -37,16 +46,21 @@ void Mat_to_vector_double(Mat& mat, vector<double>& v_double)
if(mat.type()!= CV_64FC1 || mat.rows!=1)
return;
/*
for(int i=0; i<mat.cols; i++)
v_double.push_back( mat.at< double >(0, i) );
*/
v_double = (vector<double>) mat;
}
void vector_double_to_Mat(vector<double>& v_double, Mat& mat)
{
/*
mat.create(1, v_double.size(), CV_64FC1);
for(size_t i=0; i<v_double.size(); i++)
mat.at< double >(0, i) = v_double[i];
*/
mat = Mat(v_double);
}
@ -59,31 +73,38 @@ void Mat_to_vector_float(Mat& mat, vector<float>& v_float)
if(mat.type()!= CV_32FC1 || mat.rows!=1)
return;
/*
for(int i=0; i<mat.cols; i++)
v_float.push_back( mat.at< float >(0, i) );
*/
v_float = (vector<float>) mat;
}
void vector_float_to_Mat(vector<float>& v_float, Mat& mat)
{
/*
mat.create(1, v_float.size(), CV_32FC1);
for(size_t i=0; i<v_float.size(); i++)
mat.at< float >(0, i) = v_float[i];
*/
mat = Mat(v_float);
}
//vector_uchar
void Mat_to_vector_uchar(cv::Mat& mat, std::vector<uchar>& v_uchar)
void Mat_to_vector_uchar(Mat& mat, vector<uchar>& v_uchar)
{
v_uchar.clear();
if(mat.type()!= CV_8UC1 || mat.rows!=1)
return;
/*
for(int i=0; i<mat.cols; i++)
v_uchar.push_back( mat.at< uchar >(0, i) );
*/
v_uchar = (vector<uchar>) mat;
}