Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1728088
  • 博文数量: 347
  • 博客积分: 9328
  • 博客等级: 中将
  • 技术积分: 2680
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 23:45
文章分类

全部博文(347)

文章存档

2016年(1)

2013年(4)

2012年(207)

2011年(85)

2010年(50)

分类: C/C++

2012-05-19 14:41:04


最近做Android开发的人越来越多,Android开发难免会遇到调用本地库,这就需要采用JNI技术,JNI本身并不复杂,但大多数开发者在类型转换上遇到麻烦,今天特地将几种常用类型转换写成一个实例来告诉大家如何转换,尤其是Java的类和C的结构的转换,结构体中嵌套结构体如何处理,这部分网上的资料也比较少。

1. 编写Java类

点击(此处)折叠或打开

  1. package com.jinhill.util;
  2. public class NativeModule
  3. {
  4.     public native int testArg(int i, boolean b, char c, double d);
  5.     public native byte[] testByte(byte[] b);
  6.     public native String[] testString(String s, String[] sarr);
  7.     public native int setInfo(MyInfo info);
  8.     public native MyInfo getInfo();

  9.     static
  10.     {
  11.         System.loadLibrary("NativeModule");
  12.     }
  13. }

其中MyInfo类定义如下:

点击(此处)折叠或打开

  1. public class Record
  2. {
  3.     int id;
  4.     String name;
  5.     byte[] data;
  6. }

  7. public class MyInfo
  8. {
  9.     public boolean b;
  10.     public char c;
  11.     public double d;
  12.     public int i;
  13.     public byte[] array;
  14.     public String s;
  15.     public Record rec;
  16. }

C自定义结构体 

点击(此处)折叠或打开

  1. typedef struct
  2. {
  3.     int id;
  4.     char name[255];
  5.     char data[255];
  6. } Record;

  7. typedef struct
  8. {
  9.     BOOL b;
  10.     char c;
  11.     double d;
  12.     int i;
  13.     char arr[255];
  14.     char sz[255];
  15.     Record rec;
  16. }MyInfo;

2. 生成jni头文件

1) 编译javac com/jinhill/util/NativeModule.java

2) javah –jni com.jinhill.util.NativeModule

这样com_jinhill_util_NativeModule.h文件就生成好了。

3. 编写C库

1) Java与C不同类型参数转换实例

点击(此处)折叠或打开

  1. //不同类型参数处理

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

  7.     //获取jboolean型值
  8.     BOOL b = jb;

  9.     //获取jdouble型值
  10.     double d = jd;

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

  13.     int size = 0;

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

  15.     if(size <= 0)
  16.     {
  17.         return -1;
  18.     }

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

2) Java byte与C char数组类型数组转换实例

点击(此处)折叠或打开

  1. //btye数组处理,形参作为输入或输出,返回btye数组

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

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

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

  18.     //返回jbyteArray
  19.     jbyteArray jarrRV =env->NewByteArray(nTmpLen);
  20.     jbyte *jby =env->GetByteArrayElements(jarrRV, 0);
  21.     memcpy(jby, chTmp, strlen(chTmp));
  22.     env->SetByteArrayRegion(jarrRV, 0,nTmpLen, jby);
  23.     return jarrRV;
  24. }
3) Java String与C char数组类型转换实例

点击(此处)折叠或打开

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

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

  10.     //获取jobjectArray值
  11.     int nArrLen =env->GetArrayLength(joarr);
  12.     Trace("joarr len=%d",nArrLen);
  13.     for(i=0; i<nArrLen; i++)
  14.     {
  15.         jstring js =(jstring)env->GetObjectArrayElement(joarr, i);
  16.         const char* psz = (char*)env->GetStringUTFChars(js, 0);
  17.         Trace("joarr[%d]=%s",i, psz);
  18.     }
  19.     
  20.     //将joarr作为输出形参
  21.     jstring jstrTmp = NULL;
  22.     for(i=0; i<nArrLen; i++)
  23.     {
  24.         sprintf(chTmp, "No.%dHello JNI!", i);
  25.         jstrTmp =env->NewStringUTF(chTmp);
  26.         env->SetObjectArrayElement(joarr,i, jstrTmp);
  27.         env->DeleteLocalRef(jstrTmp);
  28.     }

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

  32.     for(i=0; i<2; i++)
  33.     {
  34.         sprintf(chTmp, "No. %dReturn JNI!", i);
  35.         jstrTmp =env->NewStringUTF(chTmp);
  36.         env->SetObjectArrayElement(jstrArray,i, jstrTmp);
  37.         env->DeleteLocalRef(jstrTmp);
  38.     }
  39.     return jstrArray;
  40. }

4) Java 类与C结构体类型转换实例

点击(此处)折叠或打开

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

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

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

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

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

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

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

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

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

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

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

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

  30.     //Record rec;
  31.     jfieldID jfrec = env->GetFieldID(jcInfo, "rec", "Lcom/jinhill/util/Record;");
  32.     //获取实例的变量b的值
  33.     mi.b = env->GetBooleanField(jobj, jfb);
  34.     
  35.     //获取实例的变量c的值
  36.     jchar jc = env->GetCharField(jobj, jfc);
  37.     char ch[5] = {0};
  38.     int size = 0;
  39.     size = WideCharToMultiByte(CP_ACP, NULL, (LPCWSTR)&jc, -1, ch, 5, NULL, FALSE);
  40.     mi.c = ch[0];
  41.     
  42.     //获取实例的变量d的值
  43.     mi.d = env->GetDoubleField(jobj, jfd);
  44.     
  45.     //获取实例的变量i的值
  46.     mi.i = env->GetIntField(jobj, jfi);
  47.     
  48.     //获取实例的变量array的值
  49.     jbyteArray ja = (jbyteArray)env->GetObjectField(jobj, jfa);
  50.     int nArrLen = env->GetArrayLength(ja);
  51.     char *chArr = (char*)env->GetByteArrayElements(ja, 0);
  52.     memcpy(mi.arr, chArr, nArrLen);
  53.     
  54.     //获取实例的变量s的值
  55.     jstring jstr = (jstring)env->GetObjectField(jobj, jfs);
  56.     const char* pszStr = (char*)env->GetStringUTFChars(jstr, 0);
  57.     strcpy(mi.sz, pszStr);

  58.     //获取Record对象
  59.     jobject joRec = env->GetObjectField(jobj, jfrec);
  60.     
  61.     //获取Record对象id值
  62.     mi.rec.id = env->GetIntField(joRec, jfid);
  63.     Trace("mi.rec.id=%d",mi.rec.id);
  64.     
  65.     //获取Record对象name值
  66.     jstring jstrn = (jstring)env->GetObjectField(joRec, jfname);
  67.     pszStr = (char*)env->GetStringUTFChars(jstrn, 0);
  68.     strcpy(mi.rec.name, pszStr);
  69.     
  70.     //获取Record对象data值
  71.     jbyteArray jbd = (jbyteArray)env->GetObjectField(joRec, jfdata);
  72.     nArrLen = env->GetArrayLength(jbd);
  73.     chArr = (char*)env->GetByteArrayElements(jbd, 0);
  74.     memcpy(mi.rec.data, chArr, nArrLen);
  75.     
  76.     //日志输出
  77.     Bytes2String(mi.arr, nArrLen, chHexTmp, sizeof(chHexTmp));
  78.     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);
  79.     return 0;
  80. }

5) C结构体类型与Java 类转换实例

点击(此处)折叠或打开

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

  58.     // env->SetCharField(joInfo, jfc, (jchar)wStr);
  59.     env->SetCharField(joInfo, jfc, (jchar)mi.c);
  60.     env->SetDoubleField(joInfo, jfd, mi.d);
  61.     env->SetIntField(joInfo, jfi, mi.i);
  62.     jbyteArray jarr = env->NewByteArray(nTmpLen);
  63.     jbyte *jby = env->GetByteArrayElements(jarr, 0);
  64.     memcpy(jby, mi.arr, nTmpLen);
  65.     env->SetByteArrayRegion(jarr, 0, nTmpLen, jby);
  66.     env->SetObjectField(joInfo, jfa, jarr);
  67.     jstring jstrTmp = env->NewStringUTF(chTmp);
  68.     env->SetObjectField(joInfo, jfs, jstrTmp);
  69.     env->SetObjectField(joInfo, jfrec, joRec);
  70.     return joInfo;
  71. }
4. 编写Java测试代码

点击(此处)折叠或打开

  1. public class TestInfo {

  2.     public static void main(String[] args)
  3.     {
  4.         int i =0;
  5.         String[] sArr = new String[2];
  6.         for(i=0; i<2; i++)
  7.         {
  8.             sArr[i] = "ID=" + i;
  9.         }

  10.         byte[] b = new byte[10];
  11.         for(i=0; i<10; i++)
  12.         {
  13.             b[i] = (byte)i;
  14.         }

  15.         MyInfo mi = new MyInfo();
  16.         mi.array = b;
  17.         mi.b = false;
  18.         mi.c = 'C';
  19.         mi.d = 2011.11;
  20.         mi.i = 1752;
  21.         mi.s = "Hello World!";
  22.         mi.rec = new Record();
  23.         mi.rec.id = 2012;
  24.         mi.rec.name = "Record";
  25.         mi.rec.data = b;
  26.         NativeModule nm = new NativeModule();
  27.         nm.testArg(mi.i, mi.b, mi.c, mi.d);
  28.         byte[] b2 = nm.testByte(mi.array);
  29.         String[] s = nm.testString(mi.s, sArr);
  30.         nm.setInfo(mi);
  31.         MyInfo mi2 = nm.getInfo();
  32.         System.out.println("finish");
  33.     }
  34. }
5. Java String与 C char数组转换时的中文问题

点击(此处)折叠或打开

  1. //将jstring类型转换成windows类型
  2. char* jstringToWindows( JNIEnv *env, jstring jstr )
  3. {
  4.     int length = (env)->GetStringLength(jstr );
  5.     const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
  6.     char* rtn = (char*)malloc( length*2+1 );
  7.     int size = 0;
  8.     size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length,rtn,(length*2+1), NULL, NULL );
  9.     if( size <= 0 )
  10.         return NULL;
  11.     (env)->ReleaseStringChars(jstr, jcstr );
  12.     rtn[size] = 0;
  13.     return rtn;
  14. }

  15. //将windows类型转换成jstring类型
  16. jstring WindowsTojstring( JNIEnv* env, char* str )
  17. {
  18.     jstring rtn = 0;
  19.     int slen = strlen(str);
  20.     unsigned short * buffer = 0;
  21.     if( slen == 0 )
  22.         rtn = (env)->NewStringUTF(str );
  23.     else
  24.     {
  25.         int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
  26.         buffer = (unsigned short *)malloc( length*2 + 1 );
  27.         if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length )>0 )
  28.         rtn = (env)->NewString( (jchar*)buffer, length );
  29.     }
  30.     if( buffer )
  31.         free( buffer );
  32.     return rtn;
  33. }

阅读(3234) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~