JNI的某些数组和字符串类型转换

十年热恋 提交于 2019-12-04 07:51:35

1、jbytearray转c++byte数组 


jbyte * arrayBody = env->GetByteArrayElements(data,0); 
jsize theArrayLengthJ = env->GetArrayLength(data); 
BYTE * starter = (BYTE *)arrayBody; 

2、jbyteArray 转 c++中的BYTE[]


//jbytearray strIn
jbyte * olddata = (jbyte*)env->GetByteArrayElements(strIn, 0);
jsize  oldsize = env->GetArrayLength(strIn);
BYTE* bytearr = (BYTE*)olddata;
int len = (int)oldsize;

3、C++中的BYTE[]转jbyteArray


//nOutSize是BYTE数组的长度 BYTE pData[]
jbyte *by = (jbyte*)pData;
jbyteArray jarray = env->NewByteArray(nOutSize);
env->SetByteArrayRegin(jarray, 0, nOutSize, by);

4、jbyteArray 转 char * 

char* data = (char*)env->GetByteArrayElements(strIn, 0);

5、char* 转jstring 


jstring WindowsTojstring(JNIEnv* env, char* str_tmp)
{
 jstring rtn=0;
 int slen = (int)strlen(str_tmp);
 unsigned short* buffer=0;
 if(slen == 0)
 {
  rtn = env->NewStringUTF(str_tmp);
 }
 else
 {
  int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str_tmp, slen, NULL, 0);
  buffer = (unsigned short*)malloc(length*2+1);
  if(MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str_tmp, slen, (LPWSTR)buffer, length) > 0)
  {
   rtn = env->NewString((jchar*)buffer, length);
  }
 }
 if(buffer)
 {
  free(buffer);
 }
 return rtn;
}

下面这个没有用过,刚看到,也写进来,以后如果遇到可以验证下看。 
6、jstring 转 char* 或者 const char* 


// jstring str
const char *key = env->GetStringUTFChars(str, 0);
//jboolean isOffer
jsClient->modify(key, isOffer);
env->ReleaseStringUTFChars(str, key);

7、JNI 返回 jbyteArray 


JNIEXPORT jbyteArray JNICALL Java_Test_getByteArray(JNIEnv *env, jobject obj)
{
    jbyteArray firstMacArray = env->NewByteArray( 6 );
    ......
    jbyte *bytes = env->GetByteArrayElements( firstMacArray, 0);
    for ( int i = 0; i < sizeof( pAdapterInfo->Address ); i++ )
    {
       bytes[ i ] = pAdapterInfo->Address[ i ];
    }

    env->SetByteArrayRegion(firstMacArray, 0, 6, bytes );
    return firstMacArray;
}

8、jstring to char*


char* jstringTostring(JNIEnv* env, jstring jstr)
{        
  char* rtn = NULL;
  jclass clsstring = env->FindClass("java/lang/String");
  jstring strencode = env->NewStringUTF("utf-8");
  jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
  jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
  jsize alen = env->GetArrayLength(barr);
  jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
  if (alen > 0)
  {
    rtn = (char*)malloc(alen + 1);
    memcpy(rtn, ba, alen);
    rtn[alen] = 0;
  }
  env->ReleaseByteArrayElements(barr, ba, 0);
  return rtn;
}

 9、char* to jstring 


jstring stoJstring(JNIEnv* env, const char* pat)
{
jclass strClass = env->FindClass("Ljava/lang/String;");
jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
jstring encoding = env->NewStringUTF("utf-8");
return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
} 

10、将jstring类型转换成windows类型 


char* jstringToWindows( JNIEnv *env, jstring jstr )
{
int length = (env)->GetStringLength(jstr );
const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
char* rtn = (char*)malloc( length*2+1 );
int size = 0;
size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL );
if( size <= 0 )
return NULL;
(env)->ReleaseStringChars(jstr, jcstr );
rtn[size] = 0;
return rtn;
}

11、将windows类型转换成jstring类型 


jstring WindowsTojstring( JNIEnv* env, char* str )
{
jstring rtn = 0;
int slen = strlen(str);
unsigned short * buffer = 0;
if( slen == 0 )
rtn = (env)->NewStringUTF(str );
else
{
int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
buffer = (unsigned short *)malloc( length*2 + 1 );
if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )
rtn = (env)->NewString( (jchar*)buffer, length );
}
if( buffer )
free( buffer );
return rtn;
}
/*JNIEXPORT jstring JNICALL Java_test_cs_web_SWIFTAlianceCASmfTest_strcal
(JNIEnv *env, jclass obj, jstring jstr1, jstring jstr2)
{
jbyteArray bytes = 0;
jthrowable exc;
char *pszResult = NULL;    
char *pszSTR1 = NULL;
char *pszSTR2 = NULL;

pszSTR1 = jstringTostring(env, jstr1);
pszSTR2 = jstringTostring(env, jstr2);


int nlen = sizeof(char)*(strlen(pszSTR1)+strlen(pszSTR2));
pszResult = (char*)malloc(nlen);
strcpy(pszResult, pszSTR1);
strcat(pszResult, pszSTR2);

jstring jstrRe = stoJstring(env, pszResult);
free(pszSTR1);
free(pszSTR2);
free(pszResult);
return(jstrRe);
}
*/

12、jni object的使用 

每一个jni格式的dll中的object对应该java里面的一个类。 
如下例有一个 ObjData类,类中有成员bData ,Len 
public class ObjData { 
  public byte[]  bData; 
  public int Len;        

//------------------------jni获得传过来的Object类型的变量objDataIn-------- 

jclass clazz =(env)->FindClass("ObjData"); 

//从传进来的对象中取出byte[] 


jfieldID byteData = (env)->GetFieldID(clazz,"bData","[B");
jbyteArray pDataIn = (jbyteArray) (env)->GetObjectField(objDataIn, byteData);
jsize theArrayLeng = env->GetArrayLength(pDataIn);

13、byte[]转为BYTE[] 


jbyte * arrayBody = env->GetByteArrayElements(pDataIn,0); 
BYTE * jDataIn = (BYTE *)arrayBody; 

14、将BYTE数组转为jarray 


jbyte* byte = (jbyte*)jDataOut;    
jbyteArray jarray = env->NewByteArray(theArrayLeng);
env->SetByteArrayRegion(jarray, 0, theArrayLeng, byte);

15、给每一个实例的变量赋值 


(env)->SetObjectField(objDataIn,byteData,jarray);
(env)->SetIntField(objDataIn,pDataInLen,jDataInLen);
(env)->ReleaseByteArrayElements(pDataIn, arrayBody, 0); 

16、btye数组处理,形参作为输入或输出,返回btye数组



JNIEXPORTjbyteArray JNICALL Java_com_jinhill_util_NativeModule_testByte
(JNIEnv *env, jobject jo, jbyteArray jbArr)
{
    char chTmp[] = "Hello JNI!";
    int nTmpLen = strlen(chTmp);

    //获取jbyteArray
    char *chArr = (char*)env->GetByteArrayElements(jbArr,0);

    //获取jbyteArray长度
    int nArrLen = env->GetArrayLength(jbArr);
    char *szStrBuf =(char*)malloc(nArrLen*2+10);
    memset(szStrBuf, 0, nArrLen*2+10);
    Bytes2String(chArr, nArrLen, szStrBuf,nArrLen*2+10);
    Trace("jbArr=%s", szStrBuf);
    //将jbArr作为输出形参
    memset(chArr, 0, nArrLen);
    memcpy(chArr, chTmp, nTmpLen);

    //返回jbyteArray
    jbyteArray jarrRV =env->NewByteArray(nTmpLen); 
    jbyte *jby =env->GetByteArrayElements(jarrRV, 0); 
    memcpy(jby, chTmp, strlen(chTmp));
    env->SetByteArrayRegion(jarrRV, 0,nTmpLen, jby); 
    return jarrRV;
}

17、Java String与C char数组类型转换实例

//String 和String[]处理
JNIEXPORTjobjectArray JNICALL Java_com_jinhill_util_NativeModule_testString
(JNIEnv *env, jobject jo, jstring jstr,jobjectArray joarr)
{
    int i = 0;
    char chTmp[50] = {0};

    //获取jstring值
    const char* pszStr = (char*)env->GetStringUTFChars(jstr, 0);
    Trace("jstr=%s", pszStr);

    //获取jobjectArray值
    int nArrLen =env->GetArrayLength(joarr);
    Trace("joarr len=%d",nArrLen);
    for(i=0; i<nArrLen; i++)
    {
        jstring js =(jstring)env->GetObjectArrayElement(joarr, i);
        const char* psz = (char*)env->GetStringUTFChars(js, 0);
        Trace("joarr[%d]=%s",i, psz);
    }
    
    //将joarr作为输出形参
    jstring jstrTmp = NULL;
    for(i=0; i<nArrLen; i++)
    {
        sprintf(chTmp, "No.%dHello JNI!", i);
        jstrTmp =env->NewStringUTF(chTmp);
        env->SetObjectArrayElement(joarr,i, jstrTmp);
        env->DeleteLocalRef(jstrTmp);
    }

    //返回jobjectArray
    jclass jstrCls =env->FindClass("Ljava/lang/String;");
    jobjectArray jstrArray =env->NewObjectArray(2, jstrCls, NULL);

    for(i=0; i<2; i++)
    {
        sprintf(chTmp, "No. %dReturn JNI!", i);
        jstrTmp =env->NewStringUTF(chTmp);
        env->SetObjectArrayElement(jstrArray,i, jstrTmp);
        env->DeleteLocalRef(jstrTmp);
    }
    return jstrArray;
}

18、Java与C不同类型参数转换实例

//不同类型参数处理

JNIEXPORT jintJNICALL Java_com_jinhill_util_NativeModule_testArg
(JNIEnv *env, jobject jo, jint ji, jbooleanjb, jchar jc, jdouble jd)
{
    //获取jint型值
    int i = ji;

    //获取jboolean型值
    BOOL b = jb;

    //获取jdouble型值
    double d = jd;

    //获取jchar型值,Java的char两字节
    char ch[5] = {0};

    int size = 0;

    size = WideCharToMultiByte(CP_ACP,NULL, (LPCWSTR)&jc, -1, ch, 5, NULL, FALSE);

    if(size <= 0)
    {
        return -1;
    }

    Trace("ji=%d,jb=%d,jc=%s,jd=%lf",i, b, ch, d);
    return 0;
}

19、Java 类与C结构体类型转换实例

JNIEXPORT jint JNICALL Java_com_jinhill_util_NativeModule_setInfo
(JNIEnv *env, jobject jo, jobject jobj)
{
    char chHexTmp[512] = {0};
    //将Java类转换成C结构体
    MyInfo mi;

    //获取Java中的实例类Record
    jclass jcRec = env->FindClass("com/jinhill/util/Record");
    //int id

    jfieldID jfid = env->GetFieldID(jcRec, "id", "I");
    //String name

    jfieldID jfname = env->GetFieldID(jcRec, "name", "Ljava/lang/String;");
    //byte[] data;

    jfieldID jfdata = env->GetFieldID(jcRec, "data", "[B");
    //获取Java中的实例类MyInfo

    jclass jcInfo = env->FindClass("com/jinhill/util/MyInfo");
    //获取类中每一个变量的定义

    //boolean b
    jfieldID jfb = env->GetFieldID(jcInfo, "b", "Z");

    //char c
    jfieldID jfc = env->GetFieldID(jcInfo, "c", "C");

    //double d
    jfieldID jfd = env->GetFieldID(jcInfo, "d", "D");

    //int i
    jfieldID jfi = env->GetFieldID(jcInfo, "i", "I");

    //byte[] array
    jfieldID jfa = env->GetFieldID(jcInfo, "array", "[B");

    //String s
    jfieldID jfs = env->GetFieldID(jcInfo, "s", "Ljava/lang/String;");

    //Record rec;
    jfieldID jfrec = env->GetFieldID(jcInfo, "rec", "Lcom/jinhill/util/Record;");
    //获取实例的变量b的值
    mi.b = env->GetBooleanField(jobj, jfb);
    
    //获取实例的变量c的值
    jchar jc = env->GetCharField(jobj, jfc);
    char ch[5] = {0};
    int size = 0;
    size = WideCharToMultiByte(CP_ACP, NULL, (LPCWSTR)&jc, -1, ch, 5, NULL, FALSE);
    mi.c = ch[0];
    
    //获取实例的变量d的值
    mi.d = env->GetDoubleField(jobj, jfd);
    
    //获取实例的变量i的值
    mi.i = env->GetIntField(jobj, jfi);
    
    //获取实例的变量array的值
    jbyteArray ja = (jbyteArray)env->GetObjectField(jobj, jfa);
    int nArrLen = env->GetArrayLength(ja);
    char *chArr = (char*)env->GetByteArrayElements(ja, 0);
    memcpy(mi.arr, chArr, nArrLen);
    
    //获取实例的变量s的值
    jstring jstr = (jstring)env->GetObjectField(jobj, jfs);
    const char* pszStr = (char*)env->GetStringUTFChars(jstr, 0);
    strcpy(mi.sz, pszStr);

    //获取Record对象
    jobject joRec = env->GetObjectField(jobj, jfrec);
    
    //获取Record对象id值
    mi.rec.id = env->GetIntField(joRec, jfid);
    Trace("mi.rec.id=%d",mi.rec.id);
    
    //获取Record对象name值
    jstring jstrn = (jstring)env->GetObjectField(joRec, jfname);
    pszStr = (char*)env->GetStringUTFChars(jstrn, 0);
    strcpy(mi.rec.name, pszStr);
    
    //获取Record对象data值
    jbyteArray jbd = (jbyteArray)env->GetObjectField(joRec, jfdata);
    nArrLen = env->GetArrayLength(jbd);
    chArr = (char*)env->GetByteArrayElements(jbd, 0);
    memcpy(mi.rec.data, chArr, nArrLen);
    
    //日志输出
    Bytes2String(mi.arr, nArrLen, chHexTmp, sizeof(chHexTmp)); 
    Trace("mi.arr=%s, mi.b=%d, mi.c=%c, mi.d=%lf, mi.i=%d, \n mi.sz=%s\n mi.rec.id=%d, mi.rec.name=%s", chHexTmp, mi.b, mi.c, mi.d, mi.i, mi.sz, mi.rec.id, mi.rec.name);
    return 0;
}

20、 C结构体类型与Java 类转换实例

JNIEXPORT jobject JNICALL Java_com_jinhill_util_NativeModule_getInfo
  (JNIEnv *env, jobject jo)
{
    wchar_t wStr[255] = {0};
    char chTmp[] = "Hello JNI";
    int nTmpLen = strlen(chTmp);
    //将C结构体转换成Java类

    MyInfo mi;
    memcpy(mi.arr, chTmp, strlen(chTmp));
    mi.b = TRUE;
    mi.c = 'B';
    mi.d = 2000.9;
    mi.i = 8;
    strcpy(mi.sz, "Hello World!");
    mi.rec.id = 2011;
    memcpy(mi.rec.data, "\x01\x02\x03\x04\x05\x06", 6);
    strcpy(mi.rec.name, "My JNI");

    //获取Java中的实例类Record
    jclass jcRec = env->FindClass("com/jinhill/util/Record");

    //int id
    jfieldID jfid = env->GetFieldID(jcRec, "id", "I");

    //String name
    jfieldID jfname = env->GetFieldID(jcRec, "name", "Ljava/lang/String;");

    //byte[] data;
    jfieldID jfdata = env->GetFieldID(jcRec, "data", "[B");


    //获取Java中的实例类
    jclass jcInfo = env->FindClass("com/jinhill/util/MyInfo");

    //获取类中每一个变量的定义
    //boolean b
    jfieldID jfb = env->GetFieldID(jcInfo, "b", "Z");

    //char c
    jfieldID jfc = env->GetFieldID(jcInfo, "c", "C");

    //double d
    jfieldID jfd = env->GetFieldID(jcInfo, "d", "D");

    //int i
    jfieldID jfi = env->GetFieldID(jcInfo, "i", "I");

    //byte[] array
    jfieldID jfa = env->GetFieldID(jcInfo, "array", "[B");

    //String s
    jfieldID jfs = env->GetFieldID(jcInfo, "s", "Ljava/lang/String;");

    //Record rec;
    jfieldID jfrec = env->GetFieldID(jcInfo, "rec", "Lcom/jinhill/util/Record;");

    //创建新的对象
    jobject joRec = env->AllocObject(jcRec);
    env->SetIntField(joRec, jfid, mi.rec.id);
    jstring jstrn = env->NewStringUTF(mi.rec.name);
    env->SetObjectField(joRec, jfname, jstrn);
    jbyteArray jbarr = env->NewByteArray(6); 
    jbyte *jb = env->GetByteArrayElements(jbarr, 0); 
    memcpy(jb, mi.rec.data, 6);
    env->SetByteArrayRegion(jbarr, 0, 6, jb); 
    env->SetObjectField(joRec, jfdata, jbarr);
    //创建新的对象
    jobject joInfo = env->AllocObject(jcInfo);
    //给类成员赋值
    env->SetBooleanField(joInfo, jfb, mi.b);
    // MultiByteToWideChar (CP_ACP, 0, mi.c, -1, wStr, 255);

    // env->SetCharField(joInfo, jfc, (jchar)wStr);
    env->SetCharField(joInfo, jfc, (jchar)mi.c);
    env->SetDoubleField(joInfo, jfd, mi.d);
    env->SetIntField(joInfo, jfi, mi.i);
    jbyteArray jarr = env->NewByteArray(nTmpLen); 
    jbyte *jby = env->GetByteArrayElements(jarr, 0); 
    memcpy(jby, mi.arr, nTmpLen);
    env->SetByteArrayRegion(jarr, 0, nTmpLen, jby); 
    env->SetObjectField(joInfo, jfa, jarr);
    jstring jstrTmp = env->NewStringUTF(chTmp);
    env->SetObjectField(joInfo, jfs, jstrTmp);
    env->SetObjectField(joInfo, jfrec, joRec);
    return joInfo;
}

21、Java String与 C char数组转换时的中文问题

//将jstring类型转换成windows类型
char* jstringToWindows( JNIEnv *env, jstring jstr )
{
    int length = (env)->GetStringLength(jstr );
    const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
    char* rtn = (char*)malloc( length*2+1 );
    int size = 0;
    size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length,rtn,(length*2+1), NULL, NULL );
    if( size <= 0 )
        return NULL;
    (env)->ReleaseStringChars(jstr, jcstr );
    rtn[size] = 0;
    return rtn;
}

//将windows类型转换成jstring类型
jstring WindowsTojstring( JNIEnv* env, char* str )
{
    jstring rtn = 0;
    int slen = strlen(str);
    unsigned short * buffer = 0;
    if( slen == 0 )
        rtn = (env)->NewStringUTF(str );
    else
    {
        int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
        buffer = (unsigned short *)malloc( length*2 + 1 );
        if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length )>0 )
        rtn = (env)->NewString( (jchar*)buffer, length );
    }
    if( buffer )
        free( buffer );
    return rtn;
}

22、关于vs2015生成软件在不同机器上运行缺少库(dll)

    代码生成->运行库->改为“多线程(MT)”


其他参考:

Java 通过JNI调用C或者CPP代码 

1、Java学习之通过JNI调用C/C++编写的dll链接库(图文教程)

2、JAVA中利用JNI与VS2012实现C/C++的DLL调用

3、Java调用C++动态链接库dll,有详细过程。VS2015+Eclipse以及失败解决方案

4、关于vs2015生成软件在不同机器上运行缺少库(dll)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!