转自:http://www.cnweblog.com/fly2700/archive/2012/03/21/320083.html
1,java 代码 结构体定义
![](https://i-blog.csdnimg.cn/blog_migrate/773ae66b7206f4db6286910ff9e6e4a8.gif)
1 public class MediaInfo 2 { 3 public int nWidth; 4 public int nHeight; 5 public String strStream; 6 }
java jni接口定义
public native static int GetMediaInfo(int handle, MediaInfo info); //
c++ ndk 实现代码
![](https://i-blog.csdnimg.cn/blog_migrate/773ae66b7206f4db6286910ff9e6e4a8.gif)
1 JNIEXPORT jint JNICALL Java_src_jni_RtspConnect_GetMediaInfo 2 (JNIEnv *e, jclass c, jint h, jobject o) 3 { 4 //获取Java中的实例类 5 jclass objectClass = (e)->FindClass("src/jni/MediaInfo"); 6 7 //获取类中每一个变量的定义 8 jfieldID strStream = (e)->GetFieldID(objectClass, "strStream", "Ljava/lang/String;"); 9 jfieldID nHeight = (e)->GetFieldID(objectClass, "nHeight", "I"); 10 jfieldID nWidth = (e)->GetFieldID(objectClass, "nWidth", "I"); 11 12 13 //给每一个实例的变量付值 14 (e)->SetIntField(o, nHeight, 10); 15 (e)->SetIntField(o, nWidth, 100); 16 (e)->SetObjectField(o, strStream, (e)->NewStringUTF("my stream")); 17 18 return 0; 19 }
Java 调用
1 MediaInfo info = new MediaInfo(); 2 RtspConnect.GetMediaInfo(0, info); 3 Log.i("info", ""+info.nHeight); 4 Log.i("info", ""+info.nWidth); 5 Log.i("info", ""+info.strStream);