JNI——C/C++传递list到java

需求:C/C++传递list<Path> 到JAVA

一、java创建Path类

package com.anji.vms.routing;

public class Path {

    public String Id; //节点编号
    double posX; //节点gpsX坐标
    double posY; //节点gpsY坐标
    int i_pathType;

    public Path(String Id, double posX, double posY, int pathType) {
        super();
        this.Id = Id;
        this.posX = posX;
        this.posY = posY;
        this.i_pathType = pathType;
    }
}

二、java添加本地方法

package com.anji.vms.routing;

import com.anji.vms.routing.Path;
import java.util.List;

public class PathPlanNdk {

    public native List<Path> PathPlan(String srcLane,String srcBay, String dstBay);

}

 三、生成class文件

javac *java

四、生成头文件

javah -classpath D:\work\fms\core\Test\src com.anji.vms.routing.PathPlanNdk
javah -classpath ~/Documents/Test/Test/src com.anji.vms.routing.PathPlanNdk     ///Linux

五、C/C++添加结构体

struct Path {
	string Id; //节点编号
	double posX = 0; //节点gpsX坐标
	double posY = 0; //节点gpsY坐标
	PathType pathType = LANE; //节点类型
};

六、C/C++返回list<Path>

string jstr2str(JNIEnv *env, jstring javaString) {
	const char *nativeString = env->GetStringUTFChars(javaString, 0);

	// use your string
	string res = nativeString;
	env->ReleaseStringUTFChars(javaString, nativeString);
	return res;
}
/*
 * Class:     com_PathPlanNdk
 * Method:    PathPlan
 * Signature: (Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jobject JNICALL Java_com_anji_vms_routing_PathPlanNdk_PathPlan
(JNIEnv *env, jobject job, jstring jsrcLane, jstring jsrcBay, jstring jdstBay) {
	printf("%s\n", "PathPlan==========2===========");
	string srcLane = jstr2str(env, jsrcLane);
	string srcBay = jstr2str(env, jsrcBay);
	string dstBay = jstr2str(env, jdstBay);
	list<Path> path;
	bool res = routingAgentPathplan(srcLane, srcBay, dstBay, &path);
	if (res)
	{
		printf("%s\n", "Path plan=========res=true==========");
		//获取ArrayList类引用
		jclass list_jcs = env->FindClass("java/util/ArrayList");
		if (list_jcs == NULL) {
			printf("%s\n", "ArrayList no  find!");
			return NULL;
		}
		//获取ArrayList构造函数, 函数名为 <init> 返回类型必须为 void 即 V  
		jmethodID list_init = env->GetMethodID(list_jcs, "<init>", "()V");
		//创建一个ArrayList对象
		jobject list_obj = env->NewObject(list_jcs, list_init, "");
		//获取ArrayList对象的add()的methodID
		jmethodID list_add = env->GetMethodID(list_jcs, "add", "(Ljava/lang/Object;)Z");
		//获取Path类
		jclass Path_cls = env->FindClass("com/anji/vms/routing/Path");
		if (Path_cls == NULL) {
			printf("%s\n", "Path Class not find!");
			return NULL;
		}
		//获取Path的构造函数
		jmethodID Path_init = env->GetMethodID(Path_cls, "<init>", "(Ljava/lang/String;DDI)V");  //括号里为签名见下图
		if (Path_init == NULL) {
			printf("%s\n", "Path init not find!");
			return NULL;
		}

		printf("%s\n","Path plan==========3===========");
		for (list<Path>::iterator i_path = path.begin(); i_path != path.end(); i_path++)
		{
			const char* path_id = (*i_path).Id.c_str();
			double posX = (*i_path).posX;
			double posY = (*i_path).posY;
			int i_pathType = (*i_path).pathType;
			//通过Path的构造函数创建Path对象
			jobject path_obj = env->NewObject(Path_cls, Path_init, env->NewStringUTF(path_id), posX, posY, i_pathType);

			env->CallBooleanMethod(list_obj, list_add, path_obj);
		}
		
		return list_obj;
	}
	else
	{
		printf("%s\n", "Path plan=========res=false==========");
		return NULL;
	}

}

                                              

七、java调用测试

PathPlanNdk pat = new PathPlanNdk();
List<Path> res1 = pat.PathPlan(s1, s2, s3);

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值