3

I am trying to use openCV in my android project and trying to run this native code but I don't know how to use this parameter

JNIEXPORT jint JNICALL Java_com_example_helloopencvactivity_nativecalls_filepath
    (JNIEnv * env, jobject jo, jstring str1, jstring str2) {
    cv::Mat img1 = cv::imread("");
}

I tried using this

const char *nativeString = (*env)->GetStringUTFChars(env, str1, 0);
cv::Mat img1 = cv::imread(nativeString);

but i am getting this error error: no matching function for call to '_JNIEnv::GetStringUTFChars

I need to pass the file path from android file system to openCV's native code for processing, the passing element is string and should be read by imread

1 Answer 1

1

first in the java side, my codes looks like:

 private String path="/mnt/sdcard/";

 InitFeature(width,height,path);

 public native void InitFeature(int width, int height,String path);

then in the jni side, it's:

//passed from the java part and release after InitFreature
const char* nPath;
//to save the globle path
char* g_path;

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial6_Tutorial2Activity_InitFeature(JNIEnv* env, jobject,jint width,jint height,jstring path);

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial6_Tutorial2Activity_InitFeature(JNIEnv* env, jobject,jint width,jint height,jstring path)
{

//jsize path_size;
nPath=env->GetStringUTFChars(path,NULL);
//path_size=env->GetArrayLength(path);

LOGD("path: %s \n",nPath);

int length=strlen(nPath);

LOGD("length: %d \n",length);

g_path=new char[length+1];

memcpy(g_path,nPath,length+1);

LOGD("path_2: %s \n",g_path);
LOGD("length: %d \n",strlen(g_path));

char l_path[128];

strcpy(l_path,g_path);
strcat(l_path,"color.jpg");

LOGD("path_3: %s \n",l_path);
LOGD("length: %d \n",sizeof(l_path));

m_width=width;
m_height=height;

center.x=float (m_width/2.0+0.5);//float (Img_tmp->width/2.0+0.5);
center.y=float (m_width/2.0+0.5);//float (Img_tmp->height/2.0+0.5);

    env->ReleaseStringUTFChars(path,nPath);

}

since I have different native calls, one to initiate features(shown here) and others to process every frame, and after you env->ReleaseStringUTFChars(path,nPath); the string would be invisible to jni part. I have the copy the string to a global char* g_path;

and a little sample is here as well, the file path is "/mnt/sdcard/color.jpg" and check those logs.

then you can use imread() to get this jpg. I use other libs like libjpg, so I am not showing the codes here.

Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to write cv::imwrite(jenv->GetStringUTFChars(file_Path_Android, NULL),Mat_File) but always get false ? how to write a Mat to android's cache directory from jni code ??

Your Answer

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