JNI hashmap object callback to java

原创 2016年06月02日 11:28:15


1.JAVA code

/**Called from native code.*/
    protected void onVideoReceive(HashMap hashMap) {

        Integer timestamp = (Integer) hashMap.get("time");
        int yearCode  = ((timestamp >> 26) & 0b111111) + 2015;
        int monthCode = (timestamp >> 22) & 0b1111;
        int dayCode   = (timestamp >> 17) & 0b11111;
        int hourCode  = (timestamp >> 12) & 0b11111;
        int minCode   = (timestamp >> 6)  & 0b111111;
        int secCode   = (timestamp)  & 0b111111;
        String dateStr = String.format(Locale.getDefault(), "%04d%02d%02d%02d%02d%02d", yearCode, monthCode, dayCode, hourCode, minCode, secCode);

        Date date = null;
        try {
            date = yyyyMMddHHmmss.parse(dateStr);
//            Dbug.i(tag, "onVideoReceive============date=" + dateStr + ", format time=" + date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**Called from native code.*/
    protected void onAudioReceive(HashMap hashMap) {

        Dbug.i(tag, "onAudioReceive============seq=" + hashMap.get("seq") + ", data=" + hashMap.get("data"));
    }


2.native code


static jobject makeIntegerObject(JNIEnv *env, int32_t value) {/**Convert to Java Integer type*/
    jclass clazz = (*env)->FindClass(env, "java/lang/Integer");
    jmethodID integerConstructID = (*env)->GetMethodID(env, clazz, "<init>", "(I)V");
    return (*env)->NewObject(env, clazz, integerConstructID, value);
}
/**C buffer convert to JAVA byte type*/
static jobject makeByteBufferObject(JNIEnv *env, const void *data, size_t size) {
    jbyteArray byteArrayObj = (*env)->NewByteArray(env, size);

    (*env)->SetByteArrayRegion(env, byteArrayObj, 0, size, (const jbyte *)data);
    jclass clazz = (*env)->FindClass(env, "java/nio/ByteBuffer");

    jmethodID byteBufWrapID = (*env)->GetStaticMethodID(env, clazz, "wrap", "([B)Ljava/nio/ByteBuffer;");
    jobject byteBufObj = (*env)->CallStaticObjectMethod(env, clazz, byteBufWrapID, byteArrayObj);
    (*env)->DeleteLocalRef(env, byteArrayObj);
    byteArrayObj = NULL;
    return byteBufObj;
}


static int on_video_rcv (void *data){
 
    struct _data *pd = (struct _data *) data;

    JNIEnv *env = NULL;

    assert(java_vm != NULL);

    (*java_vm)->AttachCurrentThread(java_vm, &env, NULL);

    assert(env != NULL);

    assert(pd != NULL);

    jbyteArray jArray = (*env)->NewByteArray(env, pd->size);
    assert(pd->data != NULL);
    (*env)->SetByteArrayRegion (env, jArray, 0, pd->size, (jbyte*)(pd->data));
    jclass class_hashmap = (*env)->FindClass(env, "java/util/HashMap");
    jmethodID hashmap_init_id = (*env)->GetMethodID(env, class_hashmap, "<init>", "()V");
    jobject hashMapObj = (*env)->NewObject(env, class_hashmap, hashmap_init_id, "");
    jmethodID hashMap_put_id = (*env)->GetMethodID(env, class_hashmap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
    (*env)->CallObjectMethod(env, hashMapObj, hashMap_put_id, (*env)->NewStringUTF(env, "seq"), makeIntegerObject(env, pd->head.seq));
    (*env)->CallObjectMethod(env, hashMapObj, hashMap_put_id, (*env)->NewStringUTF(env, "data"), jArray);

    (*env)->CallObjectMethod(env, hashMapObj, hashMap_put_id, (*env)->NewStringUTF(env, "time"), makeIntegerObject(env, pd->head.date));

    //(*env)->CallObjectMethod(env, hashMapObj, hashMap_put_id, (*env)->NewStringUTF(env, "type"), makeByteBufferObject(env, pd->head.type, 1));
    (*env)->CallVoidMethod (env, gJavaObject, on_video_receive_method_id, hashMapObj);
    (*env)->DeleteLocalRef(env, jArray);
    (*env)->DeleteLocalRef(env, hashMapObj);
    (*env)->DeleteLocalRef(env, class_hashmap);

    if ((*env)->ExceptionCheck (env)) {
      LOGE ("on_video_rcv: Failed to call ExceptionCheck");
      (*env)->ExceptionClear (env);
    }

    //LOGE ("on_video_rcv ..end..");
    (*java_vm)->DetachCurrentThread(java_vm);
    return 1;
}

static int on_audio_rcv (void *data){

    struct player_data *pd = (struct player_data *) data;

    JNIEnv *env = NULL;
    assert(java_vm != NULL);
    (*java_vm)->AttachCurrentThread(java_vm, &env, NULL);
    assert(env != NULL);
    assert(pd != NULL);
    jbyteArray jArray = (*env)->NewByteArray(env, pd->size);
    assert(pd->data != NULL);
    (*env)->SetByteArrayRegion (env, jArray, 0, pd->size, (jbyte*)(pd->data));
    jclass class_hashmap = (*env)->FindClass(env, "java/util/HashMap");
    jmethodID hashmap_init_id = (*env)->GetMethodID(env, class_hashmap, "<init>", "()V");
    jobject hashMapObj = (*env)->NewObject(env, class_hashmap, hashmap_init_id, "");
    jmethodID hashMap_put_id = (*env)->GetMethodID(env, class_hashmap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
    (*env)->CallObjectMethod(env, hashMapObj, hashMap_put_id, (*env)->NewStringUTF(env, "seq"), makeIntegerObject(env, pd->head.seq));
    (*env)->CallObjectMethod(env, hashMapObj, hashMap_put_id, (*env)->NewStringUTF(env, "data"), jArray);
    (*env)->CallObjectMethod(env, hashMapObj, hashMap_put_id, (*env)->NewStringUTF(env, "time"), makeIntegerObject(env, pd->head.date));
    (*env)->CallVoidMethod (env, gJavaObject, on_audio_receive_method_id, hashMapObj);
    (*env)->DeleteLocalRef(env, jArray);
    (*env)->DeleteLocalRef(env, hashMapObj);
    (*env)->DeleteLocalRef(env, class_hashmap);
    if ((*env)->ExceptionCheck (env)) {
      LOGE ("on_audio_rcv: Failed to call ExceptionCheck");
      (*env)->ExceptionClear (env);
    }

    (*java_vm)->DetachCurrentThread(java_vm);
    return 1;
}














版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

JNI中map转HashMap

JNI中map转HashMap

JNI返回复杂对象之一

JNI返回复杂对象,对象里面的变量是Hashmap里面装ArrayList

JNI jobject 与 map互转

//从c语言的map转java hashmap jobject JavaHelper::cmap2jhashmap(std::map cmap){ JNIEnv* env = JNU_GetEnv...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

android关于jni反射的一些操作

android关于jni反射的一些操作 本文说明一下当用到jni的时候需要的反射功能及一些操作,应用场景在于c++反射调用java的一些变量及类的操作,下面截取项目中的代码片段。具体的可以看注释 ...

为什么Java中的HashMap<K, V>的get函数是get(Object key),而不是get(K key)?

帮别人的代码改bug,发现有一大堆bug是由get或者remove传递进去的参数类型不匹配而造成的。 比如: Map m = new HashMap(); m.put(new Sho...

java中如何把hashmap转换成object数组

java中如何把hashmap转换成object数组 kevintian511 | 浏览 9730 次  2007-12-10 16:37 2015-06-22 22:50...

attempting to use incompatible return type(返回类型boolean与java.lang.Object不兼容)

attempting to use incompatible return type(返回类型boolean与java.lang.Object不兼容)

SQLiteHelper类||java.lang.IllegalStateException: attempt to re-open an already-closed object

在实际的android开发项目中很少使用SQ
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JNI hashmap object callback to java
举报原因:
原因补充:

(最多只允许输入30个字)