JavaCpp Master 的使用
由于在项目中要用json来进行cs间数据的传输(不敢苟同),特地贴出构造json的代码。当然下载jsoncpp mater后用cmake编译,将jsoncpp中的inclue和生成的lib放到对应的
项目就可以使用了。
以下为c++构造json的代码,构造的是leapmotion的TrackingHand的数据:
std::string json;
Json::Value JRoot;
Json::FastWriter JWrite;
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);
// 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;
// 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);
//////////////////////////////////////////////////////////////////////////
// --- 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;
// 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;
// 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;
// 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":[
Object{...}
]">[
{
"Arm":Object{...},
"Digits":Array[5],
"GrabAngle":"0.000000",
"GrabStrth":"0.000000",
"HandCfd":"1.000000",
"HandFlag":"0",
"HandID":"31",
"HandType":"0",
"Palm":Object{...},
"PchDis":"86.311844",
"PchStrth":"0.000000",
"VisibleTime":"0"
}">{
"Arm":{
"BoneW":"62.661057",
"NextJoint":{
"JNextX":"118.585686",
"JNextY":"194.915756",
"JNextZ":"53.336639"
},
"PrevJoint":{
"JPrevX":"222.295624",
"JPrevY":"98.940933",
"JPrevZ":"278.125885"
},
"RotJonit":{
"JRotW":"0.947595",
"JRotX":"0.151793",
"JRotY":"0.231619",
"JRotZ":"-0.159297"
}
}">{
"BoneW":"62.661057",
"NextJoint":{
"JNextX":"118.585686",
"JNextY":"194.915756",
"JNextZ":"53.336639"
},
"PrevJoint":{
"JPrevX":"222.295624",
"JPrevY":"98.940933",
"JPrevZ":"278.125885"
},
"RotJonit":{
"JRotW":"0.947595",
"JRotX":"0.151793",
"JRotY":"0.231619",
"JRotZ":"-0.159297"
}
},
"Digits":[
Object{...},
Object{...},
Object{...},
Object{...},
Object{...}
]">[
{
"Digit_0":Object{...}
}">{
"Digit_0":{
"DigitBone":Array[4],
"DigitEx":"1",
"DigitID":"0"
}">{
"DigitBone":[
Object{...},
Object{...},
Object{...},
Object{...}
]">[
{
"Bone_0":Object{...}
}">{
"Bone_0":{
"BoneW":"19.913471",
"NextJoint":{
"JNextX":"137.607010",
"JNextY":"202.271103",
"JNextZ":"23.301464"
},
"PrevJoint":{
"JPrevX":"137.607010",
"JPrevY":"202.271103",
"JPrevZ":"23.301464"
},
"RotJonit":{
"JRotW":"0.694175",
"JRotX":"0.134262",
"JRotY":"0.150038",
"JRotZ":"-0.691074"
}
}">{
"BoneW":"19.913471",
"NextJoint":{
"JNextX":"137.607010",
"JNextY":"202.271103",
"JNextZ":"23.301464"
},
"PrevJoint":{
"JPrevX":"137.607010",
"JPrevY":"202.271103",
"JPrevZ":"23.301464"
},
"RotJonit":{
"JRotW":"0.694175",
"JRotX":"0.134262",
"JRotY":"0.150038",
"JRotZ":"-0.691074"
}
}
},
{
"Bone_1":Object{...}
}">{
"Bone_1":{
"BoneW":"19.913471",
"NextJoint":Object{...},
"PrevJoint":Object{...},
"RotJonit":Object{...}
}">{
"BoneW":"19.913471",
"NextJoint":{
"JNextX":"146.965485",
"JNextY":"201.532410",
"JNextZ":"-25.010183"
}">{
"JNextX":"146.965485",
"JNextY":"201.532410",
"JNextZ":"-25.010183"
},
"PrevJoint":{
"JPrevX":"137.607010",
"JPrevY":"202.271103",
"JPrevZ":"23.301464"
}">{
"JPrevX":"137.607010",
"JPrevY":"202.271103",
"JPrevZ":"23.301464"
},
"RotJonit":{
"JRotW":"0.691491",
"JRotX":