Ask Your Question

How pass Rect from Java code to Jni?

asked 2014-07-18 14:39:38 -0500

K_T gravatar image

Hi :) I have a problem... How can I pass Rect object from Java to C ?

If I want pass Mat... I do something like this:


Mat mat = new Mat()


nativeMethod(JNIEnv * jenv, jclass, jlong matFromJava){
  Mat mat = *(Mat*)matFromJava;

but getNativeObjAddr() for Rect objects doesn't exist, so how can I do this?

edit retag flag offensive close merge delete


funny, i can't find any example of a Rect passed in the generated java wrappers.

they all pass x,y,w,h instead.

berak gravatar imageberak ( 2014-07-18 15:35:29 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-07-19 12:15:27 -0500

rwong gravatar image

updated 2014-07-19 12:21:48 -0500

For the purpose of passing a Rect by value (of its coordinates), I think the integer approach is the best.

This is because the other approaches for passing a Rect may incur more runtime cost, such as additional temporary object creation (if an integer or double array of size 4 is used) or JNI calls into Java object (if the Rect object is passed into JNI as-is).

For the Sun (now Oracle) Java 6 and up, temporary objects are not an overhead, thanks to Escape Analysis, implemented in Java compilers. However, there are certain "Java-like" mobile VM platforms which do not perform compilation or Ahead-of-Time analysis, so for these platforms the overhead exists.

If one has to pass a mutable Rect by reference (to be modified by OpenCV in JNI, and have the modification propagated back to the same Rect object passed in by the Java API caller), my suggestion is to use either a Rect or a MatOfRect. The runtime overhead will be inevitable in this case.


edit flag offensive delete link more

Question Tools


Asked: 2014-07-18 14:39:38 -0500

Seen: 1,586 times

Last updated: Jul 19 '14