1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 07:13:02 +04:00

Merge pull request #28159 from asmorkalov:as/java_cleaners

Introduce option to generate Java code with finalize() or Cleaners interface #28159
 
Closes https://github.com/opencv/opencv/issues/22260
Replaces https://github.com/opencv/opencv/pull/23467

The PR introduce configuration option to generate Java code with Cleaner interface for Java 9+ and old-fashion finalize() method for old Java and Android. Mat class and derivatives are manually written. The PR introduce 2 base classes for it depending on the generator configuration.

Pros:
1. No need to implement complex and error prone cleaner on library side.
2. No new CMake templates, easier to modify code in IDE.

Cons:
1. More generator branches and different code for modern desktop and Android.

TODO: 
- [x] Add Java version check to cmake
- [x] Use Cleaners for ANDROID API 33+

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Alexander Smorkalov
2025-12-16 14:21:34 +03:00
committed by GitHub
parent 7ca9d9ce03
commit c03734475f
10 changed files with 145 additions and 57 deletions
+15 -28
View File
@@ -4,14 +4,10 @@ import java.nio.ByteBuffer;
// C++: class Mat
//javadoc: Mat
public class Mat {
public final long nativeObj;
public class Mat extends CleanableMat {
public Mat(long addr) {
if (addr == 0)
throw new UnsupportedOperationException("Native object address is NULL");
nativeObj = addr;
super(addr);
}
//
@@ -20,7 +16,7 @@ public class Mat {
// javadoc: Mat::Mat()
public Mat() {
nativeObj = n_Mat();
super(n_Mat());
}
//
@@ -29,7 +25,7 @@ public class Mat {
// javadoc: Mat::Mat(rows, cols, type)
public Mat(int rows, int cols, int type) {
nativeObj = n_Mat(rows, cols, type);
super(n_Mat(rows, cols, type));
}
//
@@ -38,7 +34,7 @@ public class Mat {
// javadoc: Mat::Mat(rows, cols, type, data)
public Mat(int rows, int cols, int type, ByteBuffer data) {
nativeObj = n_Mat(rows, cols, type, data);
super(n_Mat(rows, cols, type, data));
}
//
@@ -47,7 +43,7 @@ public class Mat {
// javadoc: Mat::Mat(rows, cols, type, data, step)
public Mat(int rows, int cols, int type, ByteBuffer data, long step) {
nativeObj = n_Mat(rows, cols, type, data, step);
super(n_Mat(rows, cols, type, data, step));
}
//
@@ -56,7 +52,7 @@ public class Mat {
// javadoc: Mat::Mat(size, type)
public Mat(Size size, int type) {
nativeObj = n_Mat(size.width, size.height, type);
super(n_Mat(size.width, size.height, type));
}
//
@@ -65,7 +61,7 @@ public class Mat {
// javadoc: Mat::Mat(sizes, type)
public Mat(int[] sizes, int type) {
nativeObj = n_Mat(sizes.length, sizes, type);
super(n_Mat(sizes.length, sizes, type));
}
//
@@ -74,7 +70,7 @@ public class Mat {
// javadoc: Mat::Mat(rows, cols, type, s)
public Mat(int rows, int cols, int type, Scalar s) {
nativeObj = n_Mat(rows, cols, type, s.val[0], s.val[1], s.val[2], s.val[3]);
super(n_Mat(rows, cols, type, s.val[0], s.val[1], s.val[2], s.val[3]));
}
//
@@ -83,7 +79,7 @@ public class Mat {
// javadoc: Mat::Mat(size, type, s)
public Mat(Size size, int type, Scalar s) {
nativeObj = n_Mat(size.width, size.height, type, s.val[0], s.val[1], s.val[2], s.val[3]);
super(n_Mat(size.width, size.height, type, s.val[0], s.val[1], s.val[2], s.val[3]));
}
//
@@ -92,7 +88,7 @@ public class Mat {
// javadoc: Mat::Mat(sizes, type, s)
public Mat(int[] sizes, int type, Scalar s) {
nativeObj = n_Mat(sizes.length, sizes, type, s.val[0], s.val[1], s.val[2], s.val[3]);
super(n_Mat(sizes.length, sizes, type, s.val[0], s.val[1], s.val[2], s.val[3]));
}
//
@@ -101,12 +97,12 @@ public class Mat {
// javadoc: Mat::Mat(m, rowRange, colRange)
public Mat(Mat m, Range rowRange, Range colRange) {
nativeObj = n_Mat(m.nativeObj, rowRange.start, rowRange.end, colRange.start, colRange.end);
super(n_Mat(m.nativeObj, rowRange.start, rowRange.end, colRange.start, colRange.end));
}
// javadoc: Mat::Mat(m, rowRange)
public Mat(Mat m, Range rowRange) {
nativeObj = n_Mat(m.nativeObj, rowRange.start, rowRange.end);
super(n_Mat(m.nativeObj, rowRange.start, rowRange.end));
}
//
@@ -115,7 +111,7 @@ public class Mat {
// javadoc: Mat::Mat(m, ranges)
public Mat(Mat m, Range[] ranges) {
nativeObj = n_Mat(m.nativeObj, ranges);
super(n_Mat(m.nativeObj, ranges));
}
//
@@ -124,7 +120,7 @@ public class Mat {
// javadoc: Mat::Mat(m, roi)
public Mat(Mat m, Rect roi) {
nativeObj = n_Mat(m.nativeObj, roi.y, roi.y + roi.height, roi.x, roi.x + roi.width);
super(n_Mat(m.nativeObj, roi.y, roi.y + roi.height, roi.x, roi.x + roi.width));
}
//
@@ -754,12 +750,6 @@ public class Mat {
return new Mat(n_zeros(sizes.length, sizes, type));
}
@Override
protected void finalize() throws Throwable {
n_delete(nativeObj);
super.finalize();
}
// javadoc:Mat::toString()
@Override
public String toString() {
@@ -1834,9 +1824,6 @@ public class Mat {
// C++: static Mat Mat::zeros(int ndims, const int* sizes, int type)
private static native long n_zeros(int ndims, int[] sizes, int type);
// native support for java finalize()
private static native void n_delete(long nativeObj);
private static native int nPutD(long self, int row, int col, int count, double[] data);
private static native int nPutDIdx(long self, int[] idx, int count, double[] data);