From 2e772510ea1a26c4b248e7ea06206e04e5369211 Mon Sep 17 00:00:00 2001 From: Florian Echtler Date: Fri, 10 Nov 2017 20:35:32 +0100 Subject: [PATCH] Merge pull request #10050 from floe/android-studio-3.3.1 Add Android Mat constructor with support for native buffer (#10050) --- modules/core/misc/java/src/java/core+Mat.java | 18 ++++++++++++++++++ modules/core/misc/java/test/MatTest.java | 13 +++++++++++++ modules/java/generator/src/cpp/Mat.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/modules/core/misc/java/src/java/core+Mat.java b/modules/core/misc/java/src/java/core+Mat.java index 6db2554..98cdba9 100644 --- a/modules/core/misc/java/src/java/core+Mat.java +++ b/modules/core/misc/java/src/java/core+Mat.java @@ -1,5 +1,7 @@ package org.opencv.core; +import java.nio.ByteBuffer; + // C++: class Mat //javadoc: Mat public class Mat { @@ -40,6 +42,19 @@ public class Mat { } // + // C++: Mat::Mat(int rows, int cols, int type, void* data) + // + + // javadoc: Mat::Mat(rows, cols, type, data) + public Mat(int rows, int cols, int type, ByteBuffer data) + { + + nativeObj = n_Mat(rows, cols, type, data); + + return; + } + + // // C++: Mat::Mat(Size size, int type) // @@ -1101,6 +1116,9 @@ public class Mat { // C++: Mat::Mat(int rows, int cols, int type) private static native long n_Mat(int rows, int cols, int type); + // C++: Mat::Mat(int rows, int cols, int type, void* data) + private static native long n_Mat(int rows, int cols, int type, ByteBuffer data); + // C++: Mat::Mat(Size size, int type) private static native long n_Mat(double size_width, double size_height, int type); diff --git a/modules/core/misc/java/test/MatTest.java b/modules/core/misc/java/test/MatTest.java index 3ff0c8b..7d0731a 100644 --- a/modules/core/misc/java/test/MatTest.java +++ b/modules/core/misc/java/test/MatTest.java @@ -1,6 +1,7 @@ package org.opencv.test.core; import java.util.Arrays; +import java.nio.ByteBuffer; import org.opencv.core.Core; import org.opencv.core.CvException; @@ -1001,4 +1002,16 @@ public class MatTest extends OpenCVTestCase { assertMatEqual(truth, dst); } + public void testMatFromByteBuffer() { + ByteBuffer bbuf = ByteBuffer.allocateDirect(64*64); + bbuf.putInt(0x01010101); + Mat m = new Mat(64,64,CvType.CV_8UC1,bbuf); + assertEquals(4, Core.countNonZero(m)); + Core.add(m, new Scalar(1), m); + assertEquals(4096, Core.countNonZero(m)); + m.release(); + assertEquals(2, bbuf.get(0)); + assertEquals(1, bbuf.get(4095)); + } + } diff --git a/modules/java/generator/src/cpp/Mat.cpp b/modules/java/generator/src/cpp/Mat.cpp index c85a3d7..6122a7e 100644 --- a/modules/java/generator/src/cpp/Mat.cpp +++ b/modules/java/generator/src/cpp/Mat.cpp @@ -52,6 +52,31 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__ // +// Mat::Mat(int rows, int cols, int type, void* data) +// + +JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIILjava_nio_ByteBuffer_2 + (JNIEnv* env, jclass, jint rows, jint cols, jint type, jobject data); + +JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIILjava_nio_ByteBuffer_2 + (JNIEnv* env, jclass, jint rows, jint cols, jint type, jobject data) +{ + static const char method_name[] = "Mat::n_1Mat__IIILByteBuffer()"; + try { + LOGD("%s", method_name); + return (jlong) new Mat( rows, cols, type, (void*)env->GetDirectBufferAddress(data) ); + } catch(const std::exception &e) { + throwJavaException(env, &e, method_name); + } catch (...) { + throwJavaException(env, 0, method_name); + } + + return 0; +} + + + +// // Mat::Mat(int rows, int cols, int type) // -- 2.7.4