JavaCpp Master 的使用

2019年11月10日 阅读数:393
这篇文章主要向大家介绍JavaCpp Master 的使用,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

  因为在项目中要用json来进行cs间数据的传输(不敢苟同),特意贴出构造json的代码。固然下载jsoncpp mater后用cmake编译,将jsoncpp中的inclue和生成的lib放到对应的javascript

项目就能够使用了。java

如下为c++构造json的代码,构造的是leapmotion的TrackingHand的数据:c++

std::string json;
Json::Value JRoot;
Json::FastWriter JWrite;git

std::string Value;
JRoot["DeviceID"] = std::to_string(msg.device_id);
JRoot["HandNums"] = std::to_string(msg.tracking_event->nHands);
JRoot["FrameRate"] = std::to_string(msg.tracking_event->framerate);
JRoot["FrameID"] = std::to_string(msg.tracking_event->tracking_frame_id);json

// fill LEAP_FRAME_HEADER
Json::Value JInfo;
JInfo["Reserved"] = ""; // msg.tracking_event->info.reserved
JInfo["FrameID"] = std::to_string(msg.tracking_event->info.frame_id);
JInfo["TimeStamp"] = std::to_string(msg.tracking_event->info.timestamp);
JRoot["Info"] =JInfo;app

// fill LEAP_HAND
Json::Value JHand;
for (i = 0; i < msg.tracking_event->nHands; i++)
{
JHand.clear();
LEAP_HAND hand = msg.tracking_event->pHands[i];
JHand["HandID"] = std::to_string(hand.id);
JHand["HandFlag"] = std::to_string(hand.flags);
JHand["HandType"] = std::to_string(hand.type);
JHand["HandCfd"] = std::to_string(hand.confidence);
JHand["VisibleTime"] = std::to_string(hand.visible_time);
JHand["PchDis"] = std::to_string(hand.pinch_distance);
JHand["GrabAngle"] = std::to_string(hand.grab_angle);
JHand["PchStrth"] = std::to_string(hand.pinch_strength);
JHand["GrabStrth"] = std::to_string(hand.grab_strength);
JHand["HandID"] = std::to_string(hand.id);ide

//////////////////////////////////////////////////////////////////////////
// --- fill _LEAP_PALM ---
Json::Value JPalm;
Json::Value JVector;
// postion
JVector["HandX"] = std::to_string(hand.palm.position.x);
JVector["HandY"] = std::to_string(hand.palm.position.y);
JVector["HandZ"] = std::to_string(hand.palm.position.z);
JPalm["HandPos"] = JVector;post

// stabilized_position
JVector.clear();
JVector["StabX"] = std::to_string(hand.palm.stabilized_position.x);
JVector["StabY"] = std::to_string(hand.palm.stabilized_position.y);
JVector["StabZ"] = std::to_string(hand.palm.stabilized_position.z);
JPalm["StabPos"] = JVector;this

// velocity
JVector.clear();
JVector["VelocX"] = std::to_string(hand.palm.velocity.x);
JVector["VelocY"] = std::to_string(hand.palm.velocity.y);
JVector["VelocZ"] = std::to_string(hand.palm.velocity.z);
JPalm["Velocity"] = JVector;spa

// normal
JVector.clear();
JVector["NormalX"] = std::to_string(hand.palm.normal.x);
JVector["NormalY"] = std::to_string(hand.palm.normal.y);
JVector["NormalZ"] = std::to_string(hand.palm.normal.z);
JPalm["Normal"] = JVector;

// direction
JVector.clear();
JVector["DirX"] = std::to_string(hand.palm.direction.x);
JVector["DirY"] = std::to_string(hand.palm.direction.y);
JVector["DirZ"] = std::to_string(hand.palm.direction.z);
JPalm["Direction"] = JVector;

// orientation
JVector.clear();
JVector["OrientX"] = std::to_string(hand.palm.orientation.x);
JVector["OrientY"] = std::to_string(hand.palm.orientation.y);
JVector["OrientZ"] = std::to_string(hand.palm.orientation.z);
JVector["OrientW"] = std::to_string(hand.palm.orientation.w);
JPalm["Orient"] = JVector;

// hand width
JPalm["PalmWidth"] = std::to_string(hand.palm.width);
JHand["Palm"] = JPalm;
// --- fill _LEAP_PALM ---
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
// fill LEAP_DIGIT digits[5];
Json::Value JDigit,JDigitValue;
std::string SDigitIndex = "Digit_";
for (int d = 0; d < 5; ++d)
{
JDigit.clear();
JDigitValue.clear();

// fill LEAP_BONE bones[4];
Json::Value JBone;
Json::Value JBoneValue;
std::string SBoneIndex = "Bone_";
for (int b = 0; b < 4; ++b)
{
// prev_joint
JBone.clear();
JVector.clear();
JVector["JPrevX"] = std::to_string(hand.digits[d].bones[b].prev_joint.x);
JVector["JPrevY"] = std::to_string(hand.digits[d].bones[b].prev_joint.y);
JVector["JPrevZ"] = std::to_string(hand.digits[d].bones[b].prev_joint.z);
JBoneValue["PrevJoint"] = JVector;

// next_joint
JVector.clear();
JVector["JNextX"] = std::to_string(hand.digits[d].bones[b].next_joint.x);
JVector["JNextY"] = std::to_string(hand.digits[d].bones[b].next_joint.y);
JVector["JNextZ"] = std::to_string(hand.digits[d].bones[b].next_joint.z);
JBoneValue["NextJoint"] = JVector;

// rotation
JVector.clear();
JVector["JRotX"] = std::to_string(hand.digits[d].bones[b].rotation.x);
JVector["JRotY"] = std::to_string(hand.digits[d].bones[b].rotation.y);
JVector["JRotZ"] = std::to_string(hand.digits[d].bones[b].rotation.z);
JVector["JRotW"] = std::to_string(hand.digits[d].bones[b].rotation.w);
JBoneValue["RotJonit"] = JVector;

// width
JBoneValue["BoneW"] = std::to_string(hand.digits[d].bones[b].width);

// append to array
std::string sIndex = SBoneIndex + std::to_string(b);
JBone[sIndex] = (JBoneValue);
JDigitValue["DigitBone"].append(JBone);
}
JDigitValue["DigitID"] = std::to_string(hand.digits[d].finger_id);
JDigitValue["DigitEx"] = std::to_string(hand.digits[d].is_extended);

std::string SDitIndex = SDigitIndex + std::to_string(d);
JDigit[SDitIndex] = JDigitValue;
JHand["Digits"].append(JDigit);
}
// fill LEAP_DIGIT digits[5];
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
// fill LEAP_BONE arm
Json::Value JArm;
// prev_joint
JVector.clear();
JVector["JPrevX"] = std::to_string(hand.arm.prev_joint.x);
JVector["JPrevY"] = std::to_string(hand.arm.prev_joint.y);
JVector["JPrevZ"] = std::to_string(hand.arm.prev_joint.z);
JArm["PrevJoint"] = JVector;

// next_joint
JVector.clear();
JVector["JNextX"] = std::to_string(hand.arm.next_joint.x);
JVector["JNextY"] = std::to_string(hand.arm.next_joint.y);
JVector["JNextZ"] = std::to_string(hand.arm.next_joint.z);
JArm["NextJoint"] = JVector;

// rotation
JVector.clear();
JVector["JRotX"] = std::to_string(hand.arm.rotation.x);
JVector["JRotY"] = std::to_string(hand.arm.rotation.y);
JVector["JRotZ"] = std::to_string(hand.arm.rotation.z);
JVector["JRotW"] = std::to_string(hand.arm.rotation.w);
JArm["RotJonit"] = JVector;

// width
JArm["BoneW"] = std::to_string(hand.arm.width);
JHand["Arm"] = JArm;
// fill LEAP_BONE arm
//////////////////////////////////////////////////////////////////////////

JRoot["Hands"].append(JHand);
}

json = JWrite.write(JRoot);

最后想要查看构造的正确与否能够将代码中"json"变量的值拿出去验证(https://www.json.cn/)就好了。

再贴个解析出来的json串(最后有彩蛋): 

{
    "DeviceID":"1",
    "FrameID":"538626",
    "FrameRate":"115.338913",
    "HandNums":"1",
    "Hands":[