Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:44:55+00:00 2026-06-01T10:44:55+00:00

I have an OpenCV Android app. Most of its code is in Java but

  • 0

I have an OpenCV Android app. Most of its code is in Java but I have
one function that is in C.
The function gets a Mat object and returns a new one.

My question is how do I return a Mat from the native code to Java?
Couldn’t find any example of that.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:44:57+00:00Added an answer on June 1, 2026 at 10:44 am

    Today I had to return a Mat from native code. I started with “Tutorial 2 Advanced – 2. Mix Java+Native OpenCV” it already passes two Mat (Images captured from camera) objects to the native code.
    But I wanted to return extracted feature, thus I added jlong addrDescriptor to the signature:

    extern "C" {
    JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_Sample4View_FindFeatures(JNIEnv* env, jobject thiz, jlong addrGray, jlong addrRgba, jlong addrDescriptor)
    {
        Mat* pMatGr=(Mat*)addrGray;
        Mat* pMatRgb=(Mat*)addrRgba;
        Mat* pMatDesc=(Mat*)addrDescriptor;
        vector<KeyPoint> v;
    
        //OrbFeatureDetector detector(50);
        OrbFeatureDetector detector;
        OrbDescriptorExtractor  extractor;
        detector.detect(*pMatGr, v);
        extractor.compute( *pMatGr, v, *pMatDesc );
        circle(*pMatRgb, Point(100,100), 10, Scalar(5,128,255,255));
        for( size_t i = 0; i < v.size(); i++ ) {
            circle(*pMatRgb, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(255,128,0,255));
        }
        }
    }
    

    In the java part I added the Mat

    private Mat descriptor;
    descriptor = new Mat();
    

    The method getNativeObjAddr() does the trick. The Mat is allocated in java and its address is passed to the native code, thus there isn’t any explicit returning.

    FindFeatures(mGraySubmat.getNativeObjAddr(), mRgba.getNativeObjAddr(), descriptor.getNativeObjAddr());
    Log.i("desc:"  , descriptor.dump());
    

    The Mat was filled with the required data and is directly accessible in the java code after the JNI invokation returns.

    Somwhere else in the code the Mat is released:

    if ( descriptor != null) 
      descriptor.release();
    descriptor = null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an iPhone app that heavily relies on the OpenCV library; as such,
I have some (OpenCV) code that generates images. I'm displaying these using OpenGL. When
I have an app in android using camera with opencv which runs for about
I have installed OpenCV. I have been able to compile some code but sometimes
I have an OpenCV function which return type is Mat. How do I convert
I have made modifications to several example OpenCV projects within Android, however I am
I am using OpenCV library. It doesn't have a modulo operator that can be
I'm trying to implement SVM in opencv for features that I have extracted features
I have an IplImage from openCV, which stores its data in a row-ordered format;
I have a code written using OpenCV in C++, and this code uses a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.