1.数组array类型
JNIEXPORT float JNICALL Java_com_example_demo_DEMO_sample(JNIEnv * env,jobject,jintArray data)
jint *intdata;
intdata= env->GetIntArrayElements(data, NULL);
if (intdata == NULL) {
return 0;
}
int len_data = env->GetArrayLength(data);
....
env->ReleaseIntArrayElements(data, intdata, 0);
2.string类型
JNIEXPORT int JNICALL Java_com_example_demo_DEMO_sample(JNIEnv* env, jobject, jstring Path)
const char *nativeString = env->GetStringUTFChars(Path, 0);
...
env->ReleaseStringUTFChars(Path, nativeString);
LOGD("Method Description: %s", "loaded haar files...");
3.NIO 获取地址
c++:
JNIEXPORT float JNICALL Java_com_example_demo_DEMO_sample(JNIEnv * env,jobject instance,object directBuffer,jint len_array)
int len_data=len_array;
// unsigned double* buff;
// buff = (unsigned char*) env->GetDirectBufferAddress(directBuffer);
unsigned char * buff= (unsigned char*)env->GetDirectBufferAddress(directBuffer);
java:
java函数参数:sample(ByteBuffer buffer);
....
ByteBuffer buffer = ByteBuffer .allocaterDirect(1024);
String str="iiiiiiiiii";
buffer.put(str.getBytes());
this.sample(buffer);
一定要记得jobject instance,上次就是因为这个,找半天bug
4.新建byte缓存
unsigned char* buffer = (unsigned char*) malloc(1024);
jobject directBuffer;
directBuffer = env->NewDirectByteBuffer(buffer, 1024);
调用 Java 方法所需参数 : 调用 Java 方法需要 JNIEnv *env 参数 和 对应的 jobject instance Java 类参数 ;
① JNIEnv *env : JNI 环境 , 注意子线程的 JNI 环境需要获取 , 主线程的 JNI 环境可以直接从 Native 层实现的 Java 方法中获取 ;
② jobject instance : 在 Native 层的 Java 对象 ;
参考
https://www.dynamsoft.com/codepool/android-ndk-optimize-camera2-barcode.html
https://code.i-harness.com/ja-jp/q/acdfef
https://www.itread01.com/p/3467.html
https://ocw.cs.pub.ro/courses/_media/osp/lectures/lecture-ndk-2.pdf
https://blog.csdn.net/shulianghan/article/details/104644627


2369

被折叠的 条评论
为什么被折叠?



