关闭

JNI hashmap object callback to java

标签: jnijava
492人阅读 评论(0) 收藏 举报
分类:


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;
}














1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

java 使用反射转换对象 Object to Map

Object to Map 转换的三种方法 就是把对象以及其对应的属性名 和属性值, 映射到一个Map中 import java.beans.BeanInfo; import java.bea...
  • hpf911
  • hpf911
  • 2014-05-08 15:04
  • 9592

java实现map和object互转的三种方法

/** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToO...
  • waysoflife
  • waysoflife
  • 2015-08-30 15:54
  • 15786

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

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

JNI处理各种类型数据,不包含获取Java的HashMap

  • 2016-01-14 21:18
  • 1.09MB
  • 下载

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

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

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

错误出在多线程下访问SQLite数据库,同一个sqlite对象已经关闭 别的正在使用。 把接口缓存了,虽然响应速度加快了,但是占用了更多的客户端资源。 android设备空间复杂度增加,由于数...
  • dingsai88
  • dingsai88
  • 2014-12-02 11:57
  • 3289

java+opencv写homography to find a known object

偶然在opencv的API中看到了一个利用图像匹配识别object的程序,甚感兴趣,只不过是C++版本的,决心一下,便准备改编成java版本,艰险丛生。。。 看看源程序 #include #...
  • WakaW
  • WakaW
  • 2015-11-07 10:33
  • 738

SQLite 异常java.lang.IllegalStateException: attempt to re-open an already-closed object

这个错误出现的原因是因为我在一个数据库查询方法中调用了另一个数据库查询方法,我的数据库查询方法都是在开始的时候获取SQLiteDatabase对象,在结束的时候关闭SQLiteDabse对象,结果内部...
  • xianglin2111
  • xianglin2111
  • 2016-12-04 11:25
  • 66

黑马程序员-java垃圾回收原理、组合设计模式、Object中的两个方法(to String() finalize())总结

---------------------- android培训、java培训、期待与您交流! ---------------------- 1、  垃圾回收: (1)       对象销...
  • huoyun19880910
  • huoyun19880910
  • 2012-08-02 17:02
  • 556

java 使用反射转换对象 Object to Map

Object to Map 转换的三种方法 就是把对象以及其对应的属性名 和属性值, 映射到一个Map中 [java] view plainco...
  • zheng963
  • zheng963
  • 2015-12-23 18:23
  • 298
    文章分类
    最新评论