2

I want to return finalResult array to java. Any help Appreciated.

extern "C"
JNIEXPORT void JNICALL
Java_com_testjniproject_MainActivity_AnalyseImageDisplay(JNIEnv *env, jobject instance, jlong sourceImageArray)
{
    Mat &srcInputImg = *(Mat*)sourceImageArray;
    vector<tuple<string, string, int>> result = AnalyseImage(srcInputImg);
    enter code here
    string finalResult[3];
    for (int i=0; i < result.size(); i++)
    {
        cout << get<0>(result[i]) << " : " << get<1>(result[i]) << " With confidence " << get<2>(result[i]) << "%" << endl;
        finalResult[i] = get<1>(result[i]);
    }
}

1 Answer 1

6

You can do this :

extern "C" {
JNIEXPORT jobjectArray JNICALL Java_com_testjniproject_MainActivity_AnalyseImageDisplay(
        JNIEnv *env,
        jobject instance,
        jlong sourceImageArray) {
    jobjectArray result;
    result = (jobjectArray)env->NewObjectArray(3,env->FindClass("java/lang/String"),env->NewStringUTF(""));
    for(int i=0; i<3; i++) {
         env->SetObjectArrayElement(result,i,env->NewStringUTF(finalResult[i].c_str()));
    }
    return result;
}
}
Sign up to request clarification or add additional context in comments.

10 Comments

You could pass nullptr or NULL as initial value of the NewObjectArray call instead of creating a dummy string
@Bruno Am getting some error as follows - error: no viable conversion from 'std::__ndk1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'const char *' env->SetObjectArrayElement(fresult,i,env->NewStringUTF(finalResult[i]))
use the c_str() method of std::string. Also make sure that your string is actually UTF-8 or ASCII.
My bad, I wrote my example to quickly. As @Botje said, use the c_str() method. I've edited my answer
My JNI not returning any result though i can run successfully. @Botje How do i convert string array to UTF- 8?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.