在java和c之间进行数据传递 之 JNI

来源:互联网 发布:java 读取文本文件 编辑:程序博客网 时间:2024/05/21 06:11

                                                                                         JNI:在java和c之间进行数据传递 


【推荐】初学者需要看的JNI开发文档:

http://wiki.jikexueyuan.com/project/jni-ndk-developer-guide/function.html


在java和c之间进行数据传递,两者之间数据的传递有这样几种情况:java和c之间基本数据类型 的交互,java向c传递对象类型,c向java返回对象类型,c调用java类。下面就这样几种情况分类说明。

1、java 向c传递基本数据类型

对于基本数据类型,java和c是相互对应的,所以可以直接使用。它们的对应关系为;

------------------------------------------------------------------------

Java类型      本地类型   字节(bit)

-------------------------------------------------------------------------
  
  boolean   jboolean   8, unsigned
  byte    jbyte     8
  char    jchar    16, unsigned
  short    jshort    16
  int     jint     32
  long    jlong    64
  float    jfloat    32
  double   jdouble   64
  void    void     n/a

------------------------------------------------------------------------

2.java向c传递对象类型

对于java传递进来的java对象模型,c要加载java类的原型,根据创建相应的c对象,获取java对象的方法的id,然后调用java对象 的方法。举例说明:比如有个java类customer对象作为jni参数传递到c程序,customer有方法String getName()。


  1. JNIEXPORT jobject JNICALL Java_com_oracle_estt_sc_db_impl_SCQueryODBC__1getCustomer
  2. (JNIEnv *env, jobject, jobject customer){

  3.     jmethodID methodId; 
  4.     //获得customer对象的句柄
  5.     jclass cls_objClass=env->GetObjectClass(customer); 
  6.     //获得customer对象中特定方法getName的id 
  7.     methodId=env->GetMethodID(cls_objClass,"getName","()Ljava/lang/String;");
  8.     //调用customer对象的特定方法getName
  9.     jstring js_name=(jstring)env->CallObjectMethod(customer,methodId,NULL);

  10.     ...

  11. }

3.c向java返回对象类型

在c程序中首先要创建要返回的java对象,得到每个属性的id,然后给每个属性赋值,最后返回。举例说明:同样是customer对象,有name等属性值,需要在c程序中给每个属性赋值后返回。


  1. JNIEXPORT jobject JNICALL Java_com_oracle_estt_sc_db_impl_SCQueryODBC__1getCustomer
  2. (JNIEnv *env, jobject, jobject customer){

  3.     ......

  4.         //发现java Customer类,如果失败,程序返回
  5.         jclass clazz = env->FindClass("com/oracle/estt/sc/busi/Customer"); 
  6.     if(clazz == 0) 
  7.         return 0; 
  8.     //为新的java类对象obj分配内存 
  9.     jobject obj = env->AllocObject(clazz); 
  10.     //发现类中的属性,如果失败,程序返回 
  11.     jfieldID fid_id = env->GetFieldID(clazz,"customerID","I"); 
  12.     if (fid_id == 0) 
  13.         return 0;
  14.     jfieldID fid_name = env->GetFieldID(clazz,"name","Ljava/lang/String;"); 
  15.     if (fid_name == 0) 
  16.         return 0;
  17.     ......

  18.         env->SetIntField(obj, fid_id, 1
  19.         env->SetObjectField(obj, fid_name, jname);

  20.     ......

  21.         return obj;

  22. }

4.c向java传递一个含有java对象的数组

对于这种情况,先得到数组的大小,接下来取出数组中的对象,取得对象的属性值或者调用对象的方法,将获得值存到本地数组中,然后可以灵活使用这些数 据了。举例说明:java向c传递一个含有多个customer对象的数组,在c中将这个数组的分解出来,存到本地的临时数组中去。


  1. JNIEXPORT void JNICALL Java_com_oracle_estt_sc_db_impl_SCInsertODBC__1insertCustomeRequest___3Lcom_oracle_estt_sc_busi_CustomerRequest_2
  2. (JNIEnv *env, jobject, jobjectArray oa){

  3.     ...... 

  4.         //声明customerrequest对象
  5.         jobject o_customer;

  6.     int i;
  7.     jmethodID methodId; 
  8.     jint size=env->GetArrayLength(oa);

  9.     _tmp_bind[0]= (char *)malloc(size*sizeof(int));
  10.     _tmp_bind[1]= (char *)malloc(size*sizeof(char)*( 20 + 1));

  11.     ...

  12.         //将输入数组的数据拷贝到临时数组中去
  13.         for(i=0;i<size;i++){
  14.             //从数组中获得customerrequest对象
  15.             o_request=env->GetObjectArrayElement(oa,i);
  16.             //获得customerrequest对象的句柄
  17.             jclass cls_objClass=env->GetObjectClass(o_request);

  18.             //获得customerrequest对象的特定方法getCustomerID的id
  19.             methodId=env->GetMethodID(cls_objClass,"getCustomerID","()I");
  20.             //调用customerrequest对象的特定方法getCustomerID
  21.             int_customerID=env->CallIntMethod(o_request,methodId,NULL); 
  22.             //获得customerrequest对象中特定方法getTelNum的id 
  23.             methodId=env->GetMethodID(cls_objClass,"getTelNum","()Ljava/lang/String;");
  24.             //调用customerrequest对象的特定方法getTelNum
  25.             str_telNum=(jstring)env->CallObjectMethod(o_request,methodId,NULL); 

  26.             ...

  27.                 //将用户id拷贝到临时数组
  28.                 memcpy(_tmp_bind[0]+i*sizeof(int),&int_customerID,sizeof(int));

  29.             //将电话号码拷贝到临时数组,如果电话号码字符串超长,报错返回
  30.             if(sizeof(char)*strlen(chr_tel)<=sizeof(char)*( 20 + 1)){
  31.                 memcpy(_tmp_bind[1]+i*sizeof(char)*( 20+),chr_tel,strlen(chr_tel)+1);
  32.             }else{
  33.                 printf("%s too long!\n",chr_tel);
  34.                 return;
  35.             }

  36.             ...

  37.         }

  38.         ...

  39. }

5.c向java返回一个数组

先创建数组,然后加载java对象,给每个java对象的属性赋值,添加到数组中,最后返回数组。如下例:


  1. JNIEXPORT jobjectArray JNICALL Java_com_oracle_estt_sc_db_impl_SCQueryODBC__1getCustomerRequest
  2. (JNIEnv *env, jobject, jint customerid){

  3.     ......

  4.         //声明存放查询结果的objectarray
  5.         jobjectArray jo_array = env->NewObjectArray(MAX_LINE,env->FindClass("com/oracle/estt/sc/busi/CustomerRequest"), 0); jobject obj;
  6.     //发现java Customerrequest类,如果失败,程序返回
  7.     jclass clazz = env->FindClass("com/oracle/estt/sc/busi/CustomerRequest"); 
  8.     if(clazz == 0) 
  9.         return 0;

  10.     while ((rc = SQLFetch(hstmt)) == SQL_SUCCESS ||rc == SQL_SUCCESS_WITH_INFO) {

  11.         obj = env->AllocObject(clazz);

  12.         jfieldID fid_customerID = env->GetFieldID(clazz,"customerID","I"); 
  13.         if (fid_customerID == 0) 
  14.             return 0;

  15.         jfieldID fid_priority = env->GetFieldID(clazz,"priority","I"); 
  16.         if (fid_priority == 0) 
  17.             return 0;

  18.         ...

  19.             env->SetIntField(obj, fid_customerID, col_customerID);

  20.         env->SetIntField(obj, fid_priority, col_priority);

  21.         ...

  22.             //将对象obj添加到object array中
  23.             if(j<MAX_LINE){
  24.                 env->SetObjectArrayElement(jo_array, j, obj);
  25.             }else{
  26.                 break;
  27.             }

  28.     }

  29.     return jo_array;

  30. }

6.jstring向char* 的转换

jstring不能直接在c程序中使用,需要转换成char*。重要的一点是,在使用完char*之后,一定要记得将其释放,以免发生内存泄漏。如下例:


  1. JNIEXPORT jobjectArray JNICALL Java_com_oracle_estt_sc_db_impl_SCQueryODBC__1getCustomerRequestByCondition
  2. (JNIEnv *env, jobject, jstring condition, jint customerid){

  3. //将jstring转换为cha*
  4.  char* str_condition=(char*) env->GetStringUTFChars(condition,JNI_FALSE);

  5. ......

  6. //释放变量
  7.  env->ReleaseStringUTFChars(condition,str_condition);

  8. ......

  9. }

7.char*转换成jstring

这个转换就比较麻烦了,但是在数据库操作时会用到。比如,从数据库查询得到的是char*,但是给对象属性赋值的时候需要用jstring,这是需要用到这种转换。具体如下例:


  1. char* col_timestamp=.....;

  2. //加载string类
  3.  jclass strClass = env->FindClass("Ljava/lang/String;");
  4.  //获得方法id
  5.  jmethodID ctorID = env->GetMethodID(strClass, "", "([BLjava/lang/String;)V");

  6. //将字符串转换为jstring 
  7.   bytes_time = env->NewByteArray(strlen(col_timestamp));
  8.   env->SetByteArrayRegion(bytes_time, 0, strlen(col_timestamp), (jbyte*)col_timestamp);
  9.   jstring js_time = env->NewStringUTF("utf-8");

  10. js_time=(jstring)env->NewObject(strClass, ctorID, bytes_time, js_time)

8.java类的原型获取方法

在c中创建java对象和调用java对象方法时需要用到java类的原型,特别是其方法签名。具体办法是:到java类所在的目录下,键入名命令:

>javap -s -p 包路径.java类名


//补充HashMap JNI 的使用


下面是具体的操作代码
//将java jobject(Hashmap)转为 c++用的Module_Info(用于init,需要分配内存)static int Jobject2Module_Info(JNIEnv * env, jobject thiz,jobject jobj, Module_Info ** module_info,int * Length,    int useModuleStreamInfo_In,int useModuleStreamInfo_Out,UseInfoType useinfotype){    int ret = 0;    //查看传进来的object是否是空    if (jobj == NULL)    {        LOGD("Jobject2Module_Info jobj == NULL");        return 1;    }    //得到map    jclass jmapclass = env->FindClass("java/util/HashMap");    jmethodID jkeysetmid = env->GetMethodID(jmapclass, "keySet", "()Ljava/util/Set;");    jmethodID jgetmid = env->GetMethodID(jmapclass,"get", "(Ljava/lang/Object;)Ljava/lang/Object;");    jobject jsetkey = env->CallObjectMethod(jobj,jkeysetmid);    //得到key数组(只有key没有value)    jclass jsetclass = env->FindClass("java/util/Set");    jmethodID jtoArraymid = env->GetMethodID(jsetclass, "toArray", "()[Ljava/lang/Object;");    jobjectArray jobjArray = (jobjectArray)env->CallObjectMethod(jsetkey,jtoArraymid);    if(jobjArray == NULL)    {        LOGD("error :jobjArray");        return -1;    }    jsize arraysize = env->GetArrayLength(jobjArray);    LOGD("******************arraysize:%d",(jint)arraysize);    //申请传出的数据信息结构体    Module_Info * pmodule_info = NULL;  //数据信息结构体    int needlength = 0;                 //需要传给下层的长度    int count_info = 0;                 //info标号    if(arraysize > 0)    {        pmodule_info = *module_info;        pmodule_info = (Module_Info *)realloc(pmodule_info,arraysize * sizeof(Module_Info));for (int i = 0; i< arraysize ;i++){pmodule_info[i].ModuleStreamInfo_In = NULL;pmodule_info[i].ModuleStreamInfo_Out = NULL;}    }    //HashMap<Integer,LvsAndroidInfo>    //通过循环key数组得到value    for( int i=0; i < arraysize; i++)    {        //得到key        jobject jkeyobject = env->GetObjectArrayElement(jobjArray,i);        jclass jintclass = env->FindClass("java/lang/Integer");        jmethodID jintId = env->GetMethodID(jintclass, "intValue", "()I");        jint jkey = (jint)env->CallIntMethod(jkeyobject,jintId);        LOGD("jkey:%d",jkey);        //得到value        jobject jvalueobject = (jobject)env->CallObjectMethod(jobj,jgetmid,jkeyobject);        jclass jvalueclass = env->GetObjectClass(jvalueobject);        //stream_id        jmethodID jstream_id = env->GetMethodID(jvalueclass, "getstream_id", "()I");   //最后的参数是括号内参数括号外返回值        if(jstream_id == NULL)        {            LOGD("error :stream_id");            return -1;        }        jint info_stream_id = (jint)env->CallIntMethod(jvalueobject,jstream_id);        LOGD("stream_id:%d",info_stream_id);        //Is_ToNext_Module        jmethodID jIs_ToNext_Module_id = env->GetMethodID(jvalueclass, "getIs_ToNext_Module", "()I");   //最后的参数是括号内参数括号外返回值        if(jIs_ToNext_Module_id == NULL)        {             LOGD("error :Is_ToNext_Module");             return -1;        }        jint info_Is_ToNext_Module = (jint)env->CallIntMethod(jvalueobject,jIs_ToNext_Module_id);        LOGD("Is_ToNext_Module:%d",info_Is_ToNext_Module);        //device_type        jmethodID jdevice_type_id = env->GetMethodID(jvalueclass, "getdevice_type", "()I");   //最后的参数是括号内参数括号外返回值        if(jdevice_type_id == NULL)        {            LOGD("error :device_type");            return -1;        }        jint info_device_type = (jint)env->CallIntMethod(jvalueobject,jdevice_type_id);        LOGD("device_type:%d",info_device_type);        //CodecType        jmethodID jCodecType_id = env->GetMethodID(jvalueclass, "getCodecType", "()I");   //最后的参数是括号内参数括号外返回值        if(jCodecType_id == NULL)        {            LOGD("error :CodecType");            return -1;        }        jint info_CodecType = (jint)env->CallIntMethod(jvalueobject,jCodecType_id);        LOGD("CodecType:%d",info_CodecType);        //timebase_num        jmethodID jtimebase_num_id = env->GetMethodID(jvalueclass, "gettimebase_num", "()I");   //最后的参数是括号内参数括号外返回值        if(jtimebase_num_id == NULL)        {            LOGD("error :timebase_num");            return -1;        }        jint info_timebase_num = (jint)env->CallIntMethod(jvalueobject,jtimebase_num_id);        LOGD("timebase_num:%d",info_timebase_num);        //timebase_den        jmethodID jtimebase_den_id = env->GetMethodID(jvalueclass, "gettimebase_den", "()I");   //最后的参数是括号内参数括号外返回值        if(jtimebase_den_id == NULL)        {            LOGD("error :timebase_den");            return -1;        }        jint info_timebase_den = (jint)env->CallIntMethod(jvalueobject,jtimebase_den_id);        LOGD("timebase_den:%d",info_timebase_den);        //Channel        jmethodID jChannel_id = env->GetMethodID(jvalueclass, "getChannel", "()I");   //最后的参数是括号内参数括号外返回值        if(jChannel_id == NULL)        {            LOGD("error :Channel");            return -1;        }        jint info_Channel = (jint)env->CallIntMethod(jvalueobject,jChannel_id);        LOGD("Channel:%d",info_Channel);        //SampleFormat        jmethodID jSampleFormat_id = env->GetMethodID(jvalueclass, "getSampleFormat", "()I");   //最后的参数是括号内参数括号外返回值        if(jSampleFormat_id == NULL)        {            LOGD("error :SampleFormat");            return -1;        }        jint info_SampleFormat = (jint)env->CallIntMethod(jvalueobject,jSampleFormat_id);        LOGD("SampleFormat:%d",info_SampleFormat);        //SampleRate        jmethodID jSampleRate_id = env->GetMethodID(jvalueclass, "getSampleRate", "()I");   //最后的参数是括号内参数括号外返回值        if(jSampleRate_id == NULL)        {            LOGD("error :SampleRate");            return -1;        }        jint info_SampleRate = (jint)env->CallIntMethod(jvalueobject,jSampleRate_id);        LOGD("SampleRate:%d",info_SampleRate);        //Stream_PixFormat        jmethodID jgetStream_PixFormat_id = env->GetMethodID(jvalueclass, "getStream_PixFormat", "()I");   //最后的参数是括号内参数括号外返回值        if(jgetStream_PixFormat_id == NULL)        {            LOGD("error :Stream_PixFormat");            return -1;        }        jint info_Stream_PixFormat = (jint)env->CallIntMethod(jvalueobject,jgetStream_PixFormat_id);        LOGD("Stream_PixFormat:%d",info_Stream_PixFormat);//WidthjmethodID jWidth_id = env->GetMethodID(jvalueclass, "getWidth", "()I");   //最后的参数是括号内参数括号外返回值if(jWidth_id == NULL){LOGD("error :Width");return -1;}jint info_Width = (jint)env->CallIntMethod(jvalueobject,jWidth_id);LOGD("Width:%d",info_Width);        //Height        jmethodID jHeight_id = env->GetMethodID(jvalueclass, "getHeight", "()I");   //最后的参数是括号内参数括号外返回值        if(jHeight_id == NULL)        {            LOGD("error :Height");            return -1;        }        jint info_Height = (jint)env->CallIntMethod(jvalueobject,jHeight_id);        LOGD("Height:%d",info_Height);//codec_formatjmethodID jcodec_format_id = env->GetMethodID(jvalueclass, "getcodec_format", "()I");   //最后的参数是括号内参数括号外返回值if(jcodec_format_id == NULL){LOGD("error :codec_format");return -1;}jint info_codec_format = (jint)env->CallIntMethod(jvalueobject,jcodec_format_id);LOGD("codec_format:%d",info_codec_format);//mux_formatjmethodID jmux_format_id = env->GetMethodID(jvalueclass, "getmux_format", "()I");   //最后的参数是括号内参数括号外返回值if(jmux_format_id == NULL){LOGD("error :mux_format");return -1;}jint info_mux_format = (jint)env->CallIntMethod(jvalueobject,jmux_format_id);LOGD("mux_format:%d",info_mux_format);        //pts        jmethodID jpts_id = env->GetMethodID(jvalueclass, "getpts", "()J");   //最后的参数是括号内参数括号外返回值        if(jpts_id == NULL)        {            LOGD("error :pts");            return -1;        }        jlong info_pts = (jlong)env->CallLongMethod(jvalueobject,jpts_id);        LOGD("pts:%lld",info_pts);        //dts        jmethodID jdts_id = env->GetMethodID(jvalueclass, "getdts", "()J");   //最后的参数是括号内参数括号外返回值        if(jdts_id == NULL)        {            LOGD("error :dts");            return -1;        }        jlong info_dts = (jlong)env->CallLongMethod(jvalueobject,jdts_id);        LOGD("dts:%lld",info_dts);        //BufLen        jmethodID jBufLen_id = env->GetMethodID(jvalueclass, "getBufLen", "()J");   //最后的参数是括号内参数括号外返回值        if(jBufLen_id == NULL)        {            LOGD("error :BufLen");            return -1;        }        jlong info_BufLen = (jlong)env->CallLongMethod(jvalueobject,jBufLen_id);        LOGD("BufLen:%lld",info_BufLen);        //ActualLen        jmethodID jActualLen_id = env->GetMethodID(jvalueclass, "getActualLen", "()J");   //最后的参数是括号内参数括号外返回值        if(jActualLen_id == NULL)        {           LOGD("error :ActualLen");           return -1;        }        jlong info_ActualLen = (jlong)env->CallLongMethod(jvalueobject,jActualLen_id);        LOGD("ActualLen:%lld",info_ActualLen);        //Buf        jmethodID jBuf_id = env->GetMethodID(jvalueclass, "getBuf", "()[B");   //最后的参数是括号内参数括号外返回值        if(jBuf_id == NULL)        {            LOGD("error :Buf");            return -1;        }        jbyteArray barr_Buf = (jbyteArray)env->CallObjectMethod(jvalueobject,jBuf_id,NULL);        jsize barr_Buf_size = env->GetArrayLength(barr_Buf);        jbyte* info_Buf = env->GetByteArrayElements(barr_Buf,0);        //LOGD("Buf:%s",info_Buf);        LOGD("barr_Buf_size:%d",barr_Buf_size);        /***********        //将上层传过来的value传到Module_Info中,每个模块用到的参数不同        ***********/        //传给下层并且是视频流        if (info_Is_ToNext_Module == 1 &&info_CodecType == CodecType_Video){    needlength ++;    pmodule_info[count_info].Is_ToNext_Module = 1;            //header            Module_HeaderInfo_FFmpeg_Video_Codec * pheaderinfo_ffmpeg_video_codec = NULL;            pheaderinfo_ffmpeg_video_codec = new Module_HeaderInfo_FFmpeg_Video_Codec();            pheaderinfo_ffmpeg_video_codec->stream_id = info_stream_id;            pmodule_info[count_info].ModuleHeaderInfo.HeaderInfo = pheaderinfo_ffmpeg_video_codec;            //StreamInfo            Module_StreamInfo * pmodulestreaminfo = NULL;            pmodulestreaminfo = new Module_StreamInfo();            pmodulestreaminfo->stream_id = info_stream_id;            pmodulestreaminfo->CodecType = (Stream_CodecType)info_CodecType;            pmodulestreaminfo->VideoInfo.PixFormat = (Stream_PixFormat)info_Stream_PixFormat;            pmodulestreaminfo->VideoInfo.Width = info_Width;            pmodulestreaminfo->VideoInfo.Height = info_Height;pmodulestreaminfo->VideoCodecInfo.codec_format = (Stream_CodecFormat)info_codec_format;pmodulestreaminfo->MuxInfo.mux_format = (Stream_MuxFormat)info_mux_format;//查看需要在哪个info中填写pts dtsif(useinfotype == UseInfoType_Stream){pmodulestreaminfo->VideoInfo.pts = info_pts;pmodulestreaminfo->VideoInfo.dts = info_dts;pmodulestreaminfo->VideoInfo.timebase_den = info_timebase_den;pmodulestreaminfo->VideoInfo.timebase_num = info_timebase_num;}else if(useinfotype == UseInfoType_CodecDecode){pmodulestreaminfo->VideoCodecInfo.pts = info_pts;pmodulestreaminfo->VideoCodecInfo.dts = info_dts;pmodulestreaminfo->VideoCodecInfo.timebase_den = info_timebase_den;pmodulestreaminfo->VideoCodecInfo.timebase_num = info_timebase_num;}else if(useinfotype == UseInfoType_MuxDemux){pmodulestreaminfo->MuxInfo.pts = info_pts;pmodulestreaminfo->MuxInfo.dts = info_dts;pmodulestreaminfo->MuxInfo.timebase_den = info_timebase_den;pmodulestreaminfo->MuxInfo.timebase_num = info_timebase_num;}else{pmodulestreaminfo->VideoInfo.pts = info_pts;pmodulestreaminfo->VideoInfo.dts = info_dts;pmodulestreaminfo->VideoInfo.timebase_den = info_timebase_den;pmodulestreaminfo->VideoInfo.timebase_num = info_timebase_num;}            pmodulestreaminfo->BufLen = info_BufLen;            if(pmodulestreaminfo->Buf == NULL)            {                pmodulestreaminfo->Buf =(char *)calloc(pmodulestreaminfo->BufLen,sizeof(char));            }            memcpy(pmodulestreaminfo->Buf,info_Buf,info_ActualLen);            pmodulestreaminfo->ActualLen = info_ActualLen;            //如果用到的是StreamInfo_In            if(useModuleStreamInfo_In)            {                pmodule_info[count_info].ModuleStreamInfo_In = pmodulestreaminfo;                LOGD("useModuleStreamInfo_In");            }            //如果用到的是StreamInfo_Out            if(useModuleStreamInfo_Out)            {                pmodule_info[count_info].ModuleStreamInfo_Out = pmodulestreaminfo;                LOGD("useModuleStreamInfo_Out");            }            count_info ++;    }    //释放        env->ReleaseByteArrayElements(barr_Buf, info_Buf, 0);//释放资源env->DeleteLocalRef(jkeyobject);env->DeleteLocalRef(jintclass);env->DeleteLocalRef(jvalueobject);env->DeleteLocalRef(jvalueclass);    }    *Length = needlength;    *module_info = pmodule_info;//释放env->DeleteLocalRef(jmapclass);env->DeleteLocalRef(jsetkey);env->DeleteLocalRef(jsetclass);env->DeleteLocalRef(jobjArray);    return 1;}

//将本模块的c++用的Module_Info转化为参数jobj_Current(Hashmap)static int Module_Info2Jobject(JNIEnv * env, jobject thiz,Module_Info * module_info,int Length,jobject jobj,UseInfoType useinfotype){    int ret = 0;    //查看传进来的object是否是空    if (module_info == NULL)    {        LOGD("Module_Info2Jobject module_info == NULL");        return 1;    }    //生成object(hashmap)    jclass class_hashmap = env->GetObjectClass(jobj);    jmethodID HashMap_put_id = env->GetMethodID(class_hashmap, "put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");    //查找要生成的vlaue类class    jclass class_LvsAndroidInfo = env->FindClass("com/android/lvs/lvsandroidinfo/LvsAndroidInfo");    if (class_LvsAndroidInfo == NULL)    {        LOGD("not found class_LvsAndroidInfo");        return -1;    }    else    {        LOGD("sucess found class_LvsAndroidInfo");    }    jclass class_LvsAndroidInfo_key = env->FindClass("java/lang/Integer");    if (class_LvsAndroidInfo_key == NULL)    {        LOGD("not found class_LvsAndroidInfo_key");        return -1;    }    else    {        LOGD("sucess found class_LvsAndroidInfo_key");    }    //生成map内容    for( int i=0; i < Length; i++)    {        int stream_id = ((Module_HeaderInfo_FFmpeg_Video_Codec *)(module_info[i].ModuleHeaderInfo.HeaderInfo))->stream_id;        LOGD("Module_HeaderInfo_FFmpeg_Video_Codec stream_id : %d",stream_id);        //key        LOGD("key");        jmethodID LvsAndroidInfo_key_id = env->GetMethodID(class_LvsAndroidInfo_key, "<init>","(I)V");                      //new这个key的id        jobject LvsAndroidInfo_keyobject = env->NewObject(class_LvsAndroidInfo_key,LvsAndroidInfo_key_id,stream_id);          //new一个object        //value        LOGD("value");        jmethodID LvsAndroidInfo_id = env->GetMethodID(class_LvsAndroidInfo, "<init>","()V");          //new这个class的id        jobject LvsAndroidInfoobject = env->NewObject(class_LvsAndroidInfo,LvsAndroidInfo_id, "");       //new一个object        //stream_id        jmethodID setstream_id_id = env->GetMethodID(class_LvsAndroidInfo, "setstream_id","(I)V");        if(setstream_id_id == NULL)        {            LOGD("error :setstream_id_id");            return NULL;        }        env->CallVoidMethod(LvsAndroidInfoobject,setstream_id_id,stream_id);        //Is_ToNext_Module        jmethodID setIs_ToNext_Module_id = env->GetMethodID(class_LvsAndroidInfo, "setIs_ToNext_Module","(I)V");        if(setIs_ToNext_Module_id == NULL)        {            LOGD("error :setIs_ToNext_Module_id");            return NULL;        }        env->CallVoidMethod(LvsAndroidInfoobject,setIs_ToNext_Module_id,module_info[i].Is_ToNext_Module);        //CodecType        jmethodID setCodecType_id = env->GetMethodID(class_LvsAndroidInfo, "setCodecType","(I)V");        if(setCodecType_id == NULL)        {            LOGD("error :setCodecType_id");            return NULL;        }        env->CallVoidMethod(LvsAndroidInfoobject,setCodecType_id,(int)module_info[i].ModuleStreamInfo_Out->CodecType);//ptsjmethodID setpts_id = env->GetMethodID(class_LvsAndroidInfo, "setpts","(J)V");if(setpts_id == NULL){LOGD("error :setpts_id");return NULL;}//dtsjmethodID setdts_id = env->GetMethodID(class_LvsAndroidInfo, "setdts","(J)V");if(setdts_id == NULL){LOGD("error :setdts_id");return NULL;}//timebase_numjmethodID settimebase_num_id = env->GetMethodID(class_LvsAndroidInfo, "settimebase_num","(I)V");if(settimebase_num_id == NULL){LOGD("error :settimebase_num_id");return NULL;}//timebase_denjmethodID settimebase_den_id = env->GetMethodID(class_LvsAndroidInfo, "settimebase_den","(I)V");if(settimebase_den_id == NULL){LOGD("error :settimebase_den_id");return NULL;}//查看需要在哪个info中填写pts dtsif(useinfotype == UseInfoType_Stream){env->CallVoidMethod(LvsAndroidInfoobject,setpts_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.pts);env->CallVoidMethod(LvsAndroidInfoobject,setdts_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.dts);env->CallVoidMethod(LvsAndroidInfoobject,settimebase_num_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.timebase_num);env->CallVoidMethod(LvsAndroidInfoobject,settimebase_den_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.timebase_den);}else if(useinfotype == UseInfoType_CodecDecode){env->CallVoidMethod(LvsAndroidInfoobject,setpts_id,module_info[i].ModuleStreamInfo_Out->VideoCodecInfo.pts);env->CallVoidMethod(LvsAndroidInfoobject,setdts_id,module_info[i].ModuleStreamInfo_Out->VideoCodecInfo.dts);env->CallVoidMethod(LvsAndroidInfoobject,settimebase_num_id,module_info[i].ModuleStreamInfo_Out->VideoCodecInfo.timebase_num);env->CallVoidMethod(LvsAndroidInfoobject,settimebase_den_id,module_info[i].ModuleStreamInfo_Out->VideoCodecInfo.timebase_den);}else if(useinfotype == UseInfoType_MuxDemux){env->CallVoidMethod(LvsAndroidInfoobject,setpts_id,module_info[i].ModuleStreamInfo_Out->MuxInfo.pts);env->CallVoidMethod(LvsAndroidInfoobject,setdts_id,module_info[i].ModuleStreamInfo_Out->MuxInfo.dts);env->CallVoidMethod(LvsAndroidInfoobject,settimebase_num_id,module_info[i].ModuleStreamInfo_Out->MuxInfo.timebase_num);env->CallVoidMethod(LvsAndroidInfoobject,settimebase_den_id,module_info[i].ModuleStreamInfo_Out->MuxInfo.timebase_den);}else{env->CallVoidMethod(LvsAndroidInfoobject,setpts_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.pts);env->CallVoidMethod(LvsAndroidInfoobject,setdts_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.dts);env->CallVoidMethod(LvsAndroidInfoobject,settimebase_num_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.timebase_num);env->CallVoidMethod(LvsAndroidInfoobject,settimebase_den_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.timebase_den);}        //BufLen        jmethodID setBufLen_id = env->GetMethodID(class_LvsAndroidInfo, "setBufLen","(J)V");        if(setpts_id == NULL)        {            LOGD("error :setBufLen_id");            return NULL;        }//这里一定故要用ModuleStreamInfo_In的buf长度,上层传过来的会比本层输出的buf大        env->CallVoidMethod(LvsAndroidInfoobject,setBufLen_id,module_info[i].ModuleStreamInfo_In->BufLen);        //ActualLen        jmethodID setActualLen_id = env->GetMethodID(class_LvsAndroidInfo, "setActualLen","(J)V");        if(setActualLen_id == NULL)        {            LOGD("error :setActualLen_id");            return NULL;        }        env->CallVoidMethod(LvsAndroidInfoobject,setActualLen_id,module_info[i].ModuleStreamInfo_Out->ActualLen);        //Buf        jmethodID setBuf_id = env->GetMethodID(class_LvsAndroidInfo, "setBuf","([B)V");        if(setBuf_id == NULL)        {            LOGD("error :setBuf_id");            return NULL;        }jbyteArray bufbyteArray = env->NewByteArray(module_info[i].ModuleStreamInfo_Out->ActualLen);env->SetByteArrayRegion(bufbyteArray,0,module_info[i].ModuleStreamInfo_Out->ActualLen,(const jbyte *)module_info[i].ModuleStreamInfo_Out->Buf);env->CallVoidMethod(LvsAndroidInfoobject,setBuf_id,bufbyteArray);        //Stream_PixFormat        jmethodID setStream_PixFormat_id = env->GetMethodID(class_LvsAndroidInfo, "setStream_PixFormat","(I)V");        if(setStream_PixFormat_id == NULL)        {            LOGD("error :setStream_PixFormat_id");            return NULL;        }        env->CallVoidMethod(LvsAndroidInfoobject,setStream_PixFormat_id,(int)module_info[i].ModuleStreamInfo_Out->VideoInfo.PixFormat);        //Width        jmethodID setWidth_id = env->GetMethodID(class_LvsAndroidInfo, "setWidth","(I)V");        if(setWidth_id == NULL)        {            LOGD("error :setWidth_id");            return NULL;        }        env->CallVoidMethod(LvsAndroidInfoobject,setWidth_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.Width);        //Height        jmethodID setHeight_id = env->GetMethodID(class_LvsAndroidInfo, "setHeight","(I)V");        if(setHeight_id == NULL)        {            LOGD("error :setHeight_id");            return NULL;        }        env->CallVoidMethod(LvsAndroidInfoobject,setHeight_id,module_info[i].ModuleStreamInfo_Out->VideoInfo.Height);        //codec_format        jmethodID setcodec_format_id = env->GetMethodID(class_LvsAndroidInfo, "setcodec_format","(I)V");        if(setcodec_format_id == NULL)        {            LOGD("error :setcodec_format_id");            return NULL;        }env->CallVoidMethod(LvsAndroidInfoobject,setcodec_format_id,(int)(module_info[i].ModuleStreamInfo_Out->VideoCodecInfo.codec_format));//mux_formatjmethodID setmux_format_id = env->GetMethodID(class_LvsAndroidInfo, "setmux_format","(I)V");if(setmux_format_id == NULL){LOGD("error :setmux_format_id");return NULL;}env->CallVoidMethod(LvsAndroidInfoobject,setmux_format_id,(int)module_info[i].ModuleStreamInfo_Out->MuxInfo.mux_format);        //调用hashmap put方法        env->CallObjectMethod(jobj,HashMap_put_id,LvsAndroidInfo_keyobject,LvsAndroidInfoobject);//释放env->DeleteLocalRef(LvsAndroidInfo_keyobject);env->DeleteLocalRef(LvsAndroidInfoobject);env->DeleteLocalRef(bufbyteArray);}//释放env->DeleteLocalRef(class_hashmap);env->DeleteLocalRef(class_LvsAndroidInfo);env->DeleteLocalRef(class_LvsAndroidInfo_key);    return 1;}