Skip to content

Commit 2e77251

Browse files
floealalek
authored andcommitted
Merge pull request opencv#10050 from floe/android-studio-3.3.1
Add Android Mat constructor with support for native buffer (opencv#10050)
1 parent c6fb993 commit 2e77251

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

modules/core/misc/java/src/java/core+Mat.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.opencv.core;
22

3+
import java.nio.ByteBuffer;
4+
35
// C++: class Mat
46
//javadoc: Mat
57
public class Mat {
@@ -39,6 +41,19 @@ public Mat(int rows, int cols, int type)
3941
return;
4042
}
4143

44+
//
45+
// C++: Mat::Mat(int rows, int cols, int type, void* data)
46+
//
47+
48+
// javadoc: Mat::Mat(rows, cols, type, data)
49+
public Mat(int rows, int cols, int type, ByteBuffer data)
50+
{
51+
52+
nativeObj = n_Mat(rows, cols, type, data);
53+
54+
return;
55+
}
56+
4257
//
4358
// C++: Mat::Mat(Size size, int type)
4459
//
@@ -1101,6 +1116,9 @@ public long getNativeObjAddr() {
11011116
// C++: Mat::Mat(int rows, int cols, int type)
11021117
private static native long n_Mat(int rows, int cols, int type);
11031118

1119+
// C++: Mat::Mat(int rows, int cols, int type, void* data)
1120+
private static native long n_Mat(int rows, int cols, int type, ByteBuffer data);
1121+
11041122
// C++: Mat::Mat(Size size, int type)
11051123
private static native long n_Mat(double size_width, double size_height, int type);
11061124

modules/core/misc/java/test/MatTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.opencv.test.core;
22

33
import java.util.Arrays;
4+
import java.nio.ByteBuffer;
45

56
import org.opencv.core.Core;
67
import org.opencv.core.CvException;
@@ -1001,4 +1002,16 @@ public void testZerosSizeInt() {
10011002
assertMatEqual(truth, dst);
10021003
}
10031004

1005+
public void testMatFromByteBuffer() {
1006+
ByteBuffer bbuf = ByteBuffer.allocateDirect(64*64);
1007+
bbuf.putInt(0x01010101);
1008+
Mat m = new Mat(64,64,CvType.CV_8UC1,bbuf);
1009+
assertEquals(4, Core.countNonZero(m));
1010+
Core.add(m, new Scalar(1), m);
1011+
assertEquals(4096, Core.countNonZero(m));
1012+
m.release();
1013+
assertEquals(2, bbuf.get(0));
1014+
assertEquals(1, bbuf.get(4095));
1015+
}
1016+
10041017
}

modules/java/generator/src/cpp/Mat.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__
5151

5252

5353

54+
//
55+
// Mat::Mat(int rows, int cols, int type, void* data)
56+
//
57+
58+
JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIILjava_nio_ByteBuffer_2
59+
(JNIEnv* env, jclass, jint rows, jint cols, jint type, jobject data);
60+
61+
JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIILjava_nio_ByteBuffer_2
62+
(JNIEnv* env, jclass, jint rows, jint cols, jint type, jobject data)
63+
{
64+
static const char method_name[] = "Mat::n_1Mat__IIILByteBuffer()";
65+
try {
66+
LOGD("%s", method_name);
67+
return (jlong) new Mat( rows, cols, type, (void*)env->GetDirectBufferAddress(data) );
68+
} catch(const std::exception &e) {
69+
throwJavaException(env, &e, method_name);
70+
} catch (...) {
71+
throwJavaException(env, 0, method_name);
72+
}
73+
74+
return 0;
75+
}
76+
77+
78+
5479
//
5580
// Mat::Mat(int rows, int cols, int type)
5681
//

0 commit comments

Comments
 (0)