JNIEXPORT jint JNICALL Java_com_example_duanjiwei_ndkdemoapp_MainActivity_initProc(JNIEnv *env, jclass obj, jstring jstr){
jclass cls = env->GetObjectClass(obj); // 得到代表Java类的jclass
jfieldID fid1 = env->GetStaticFieldID(cls, "cstr", "Ljava/lang/String");//得到Java端保存分类器路径String静态变量的jfiledID
jstring jst1 = (jstring)env->GetStaticObjectField(cls, fid1); //得到Java端的String实例jstring
const char* cstring = env->GetStringUTFChars(jstr, NULL); //得到利用UTF-8编码的C++字符串,此字符串串为存储分类器的路径
env->ReleaseStringUTFChars(jstr, cstring); //释放拷贝的内存
bool bIsLoad = face_cascade.load( cstring );
if( bIsLoad == false ){
return -1;
}
return 0;
}
jclass clsstring = env->FindClass("java/lang/String"); //获取java.lang.String的类型
jstring strencode = env->NewStringUTF("utf-8"); //转码
//获取clssting(byte[],String) 的构造器,将本地byte[]数组转换成一个新String
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr = (jbyteArray)env->CallObjectMethod(jstr, mid, strencode); //建立barr数组
jsize alen = env->GetArrayLength(barr); //获取数组barr的长度
jbyte* ba = env->GetByteArrayElement(barr, JNI_FALSE); //将jbyteArray转换成jbyte*
char* rtn;
if(alen>0){
rtn = (char*)malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
env->ReleaseByteArrayElement(barr, ba, 0);