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














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

相关文章推荐

C++ hash_map 与 Java HashMap 的区别

与Java中的HashMap比起来, C++ STL 中hash_map的实现并不是很完美。原因在于一些小的细节: 1. Hash表的增长方式:      hash_map: 当元素数量eleme...

ubuntu下,C/C++通过JNI调用 JAVA类

之前写过一个实现了某种功能的java程序,但由于近日“工作”需要,又需要在c/c++中重新使用该功能。为了节省时间,我采取了在c/c++中调用java程序的方法,但之前没有接触过类似的东西,到网上一查...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

JNI hashmap object callback to java

/**Called from native code.*/ protected void onVideoReceive(HashMap hashMap) { // byte[] ...
  • hhbgk
  • hhbgk
  • 2016-06-02 11:28
  • 341

jQuery.each( object, callback,arg)

jQuery.each( object, callback,arg)each()函数是基本上所有的框架都提供了的一个工具类函数,通过它,你可以遍历对象、数组的属性值并进行处理。 jQuery和jQue...

java与c之间的参数转换

http://blog.csdn.net/vrix/article/details/5960442 1:c中返回一个字符串        ...................     ...

JNI中java类型与C/C++类型对应关系 ------经典文章

转自:http://www.cnblogs.com/nicholas_f/archive/2010/11/30/1892124.html JNI中java类型与C/C++类型对应关系 在如...

JNI入门介绍下

3.Java类型和本地类型对应  在如下情况下,需要在本地方法中应用java对象的引用,就会用到类型之间的转换:  1)java方法里面将参数传入本地方法;  2)在本地方法里面创建java对象;  ...

Linux/windows下java调用lingo

最近有一些朋友问我关于java程序调用lingo的问题,为了方便大家了解一些过程,下面就将java调用的详细过程重新写一下。在介绍java调用前首先介绍下,他们之间调用的原理。 原理:一个完整的...

A guide to object cloning in java

A clone is an exact copy of the original. In java, it essentially means the ability to create an obj...

How to convert Java object to / from JSON (Gson)

JSON is stand for JavaScript Object Notation, it is a lightweight data-interchange format. You can s...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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