Skip to content

Add Android Mat constructor with support for native buffer - #10050

Merged
alalek merged 8 commits into
opencv:masterfrom
floe:android-studio-3.3.1
Nov 10, 2017
Merged

alalek merged 8 commits into
opencv:masterfrom
floe:android-studio-3.3.1

Conversation

@floe

@floe floe commented Nov 8, 2017 •

Copy link
Copy Markdown
Contributor

As a prerequisite for properly supporting the Camera2 API, this PR adds an Android Mat constructor with support for passing native buffers.

force_builders=Android pack

@alalek

alalek commented Nov 8, 2017

Copy link
Copy Markdown
Member

What part of Android API provides access to native buffers?
How to control lifetime of this native buffer?

@floe

floe commented Nov 8, 2017 •

Copy link
Copy Markdown
Contributor Author

Apologies in advance, I just realized this won't work as intended. Needs to pass a jobject instead of a jlong and then call env->getDirectBufferAddress(..).

@alalek The Camera2 API (https://developer.android.com/reference/android/hardware/camera2/package-summary.html) allows you to select an ImageReader as a preview surface, and from there, you can get a direct mapped ByteBuffer.

@floe

floe commented Nov 8, 2017

Copy link
Copy Markdown
Contributor Author

@alalek side question: I haven't yet managed to build the Android OpenCV package locally. Can I download the results from the buildbot somewhere?

@floe

floe commented Nov 8, 2017

Copy link
Copy Markdown
Contributor Author

Just for the record, I have a (mostly) finished implementation of a JavaCamera2View, but I'd like to get this changeset merged separately first, as it changes a core component.

@alalek

alalek commented Nov 9, 2017

Copy link
Copy Markdown
Member

Could you please add some simple Java test for new functionality?

Something like this:

1. Create ByteBuffer with direct memory access
2. Fill it with some values
3. Create `Mat m` (2D is preferable)
4. Call `norm(m, NORM_L2)` or `countNonZero(m)`
5. check result
6. Modify Mat contents (cv::add)
7. m.release()
8. Check ByteBuffer updated values (first / last is enough)

Comment thread modules/java/generator/src/cpp/Mat.cpp Outdated
JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIIO
(JNIEnv* env, jclass, jint rows, jint cols, jint type, jobject data);

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIIO

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper signature generated by "javah" is:

/*
 * Class:     org_opencv_core_Mat
 * Method:    n_Mat
 * Signature: (IIILjava/nio/ByteBuffer;)J
 */
JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIILjava_nio_ByteBuffer_2
  (JNIEnv *, jclass, jint, jint, jint, jobject);

@floe

floe commented Nov 9, 2017

Copy link
Copy Markdown
Contributor Author

Thanks, updated. I still can't build/test locally, since I have a newer version of the Android SDK installed that doesn't support the android command anymore - so I have to rely on the buildbot ATM.

@alalek

alalek commented Nov 10, 2017

Copy link
Copy Markdown
Member

Check "Android pack" builder artifacts (step 14) from here: http://pullrequest.opencv.org/#/summary/
(file is too large (~300Mb) - please let know if there are some issues - there is build for armv7/aarch64 arch only (32Mb)).

@floe

floe commented Nov 10, 2017

Copy link
Copy Markdown
Contributor Author

Seems to work so far, all tests have passed and I can build my JavaCamera2Source with the new pack. I haven't tested the source itself yet, but I would suggest to do that in a new PR.

@alalek

alalek commented Nov 10, 2017

Copy link
Copy Markdown
Member

BTW, There is some information why memory pointer from GetDirectBufferAddress() can't be moved by JVM garbage collector for ByteBuffer.allocateDirect() objects: https://stackoverflow.com/a/5671880

@alalek
alalek merged commit 2e77251 into opencv:master Nov 10, 2017
@floe
floe deleted the android-studio-3.3.1 branch November 11, 2017 13:24
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Add Android Mat constructor with support for native buffer (opencv#10050)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants