Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Returning a Mat from native JNI to Java

I have a question about mixing OpenCV's C++ and Java API. I'm using the new desktop Java API to do some processing on images, but due to Java limitations I am forced to capture the images in C++. The images are in an OS-dependant format, so I thought it would be best to convert them to the OpenCV format before handing it to the OS-independant Java code. In the android tutorials (Tutorial 2 Advanced) I saw that they instantiate a Mat on the Java side, and use getNativeObjAddr() to pass a pointer to C++, where it is used as a native C++ object. However, since I can not make any assumptions on the dimensions or channels of the images, I can not take this approach. I have nu clue how to do this correctly. I have two ideas:

1) I create a Java Mat object from the C++ side ((*env)->NewObject), and then call getNativeObjAddr and work with that. While this might work, it seems so backwards...

2) I create a C++ Mat, and return the pointer to Java. I'm hoping there's some kind of functionality that allows me to wrap it in a Java Mat object. The documentation mentions the Mat(long addr) constructor, but there's no further explanation, and I have no idea what it's for. Can it be used to create a Mat object from a pointer to a native Mat object?

TL;DR: How do I get a Mat created in C++ to Java ?

Thank you for reading!

Returning a Mat from native JNI to Java

I have a question about mixing OpenCV's C++ and Java API. I'm using the new desktop Java API to do some processing on images, but due to Java limitations I am forced to capture the images in C++. The images are in an OS-dependant format, so I thought it would be best to convert them to the OpenCV format before handing it to the OS-independant Java code. In the android tutorials (Tutorial 2 Advanced) I saw that they instantiate a Mat on the Java side, and use getNativeObjAddr() to pass a pointer to C++, where it is used as a native C++ object. However, since I can not make any assumptions on the dimensions or channels of the images, I can not take this approach. I want Java to be able to receive the Mat object without knowing anything about it beforehand. I have nu clue how to do this correctly. I have two ideas:

1) I create a Java Mat object from the C++ side ((*env)->NewObject), and then call getNativeObjAddr and work with that. While this might work, it seems so backwards...

2) I create a C++ Mat, and return the pointer to Java. I'm hoping there's some kind of functionality that allows me to wrap it in a Java Mat object. The documentation mentions the Mat(long addr) constructor, but there's no further explanation, and I have no idea what it's for. Can it be used to create a Mat object from a pointer to a native Mat object?

TL;DR: How do I get a Mat created in C++ to Java ?

Thank you for reading!

Returning a Mat from native JNI to Java

I have a question about mixing OpenCV's C++ and Java API. I'm using the new desktop Java API to do some processing on images, but due to Java limitations I am forced to capture the images in C++. The images are in an OS-dependant format, so I thought it would be best to convert them to the OpenCV format before handing it to the OS-independant Java code. In the android tutorials (Tutorial 2 Advanced) I saw that they instantiate a Mat on the Java side, and use getNativeObjAddr() to pass a pointer to C++, where it is used as a native C++ object. However, since I can not make any assumptions on the dimensions or channels of the images, I can not take this approach. I want Java to be able to receive the Mat object without knowing anything about it beforehand. I have nu clue how to do this correctly. I have two ideas:

1) I create a Java Mat object from the C++ side ((*env)->NewObject), and then use JNI to call getNativeObjAddr and work with that. the returned pointer. While this might work, it seems so backwards...

2) I create a C++ Mat, and return the pointer to Java. I'm hoping there's some kind of functionality that allows me to wrap it in a Java Mat object. The documentation mentions the Mat(long addr) constructor, but there's no further explanation, and I have no idea what it's for. Can it be used to create a Mat object from a pointer to a native Mat object?

TL;DR: How do I get a Mat created in C++ to Java ?

Thank you for reading!

Returning a Mat from native JNI to Java

I have a question about mixing OpenCV's C++ and Java API. I'm using the new desktop Java API to do some processing on images, but due to Java limitations I am forced to capture the images in C++. The images are in an OS-dependant format, so I thought it would be best to convert them to the OpenCV format before handing it to the OS-independant Java code. In the android tutorials (Tutorial 2 Advanced) I saw that they instantiate a Mat on the Java side, and use getNativeObjAddr() to pass a pointer to C++, where it is used as a native C++ object. However, since I can not make any assumptions on the dimensions or channels of the images, I can not take this approach. I want Java to be able to receive the Mat object without knowing anything about it beforehand. I have nu clue how to do this correctly. I have two ideas:

1) I create a Java Mat object from the C++ side ((*env)->NewObject), use JNI to call getNativeObjAddr and work with the returned pointer. While this might work, it seems so backwards... Maybe there's a better way?

2) I create a C++ Mat, and return the pointer to Java. I'm hoping there's some kind of functionality that allows me to wrap it in a Java Mat object. The documentation mentions the Mat(long addr) constructor, but there's no further explanation, and I have no idea what it's for. Can it be used to create a Mat object from a pointer to a native Mat object?

TL;DR: How do I get a Mat created in C++ to Java ?

Thank you for reading!