// // Created by Frewen.Wang on 2022/11/19. // #include"FaceInfo_generated.h" #include"aura/baselib/utils/AuraLog.h"
usingnamespace aura::aura_vision;
constchar *TAG = "demo_flatbuffers";
/** * 参考文档:https://github.com/google/flatbuffers/blob/master/samples/sample_binary.cpp * @param argc * @param argv * @return */ intmain(int argc, constchar *argv[]){ // Build up a serialized buffer algorithmically: flatbuffers::FlatBufferBuilder builder; // 创建FaceInfo的String字段:name auto name = builder.CreateString("MyFace"); // 创建FaceInfo的short字段:name short index = 123; // 创建FaceIndo的struct字段:headLocation auto headLocation = VPoint3(1.0f, 2.0f, 3.0f); auto headDeflection = VAngle(1.0f, 2.0f, 3.0f); // 创建FaceInfo的Vector<float>字段:faceFeature float face_feature[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; auto faceFeature = builder.CreateVector(face_feature, 10);
auto point1 = builder.CreateStruct(VPoint3(1.0f, 2.0f, 3.0f)); auto point2 = builder.CreateStruct(VPoint3(4.0f, 5.0f, 6.0f)); auto point3 = builder.CreateStruct(VPoint3(7.0f, 8.0f, 9.0f)); std::vector<flatbuffers::Offset<const VPoint3 *>> eye3dLandmark28LeftVector; eye3dLandmark28LeftVector.push_back(point1); eye3dLandmark28LeftVector.push_back(point2); eye3dLandmark28LeftVector.push_back(point3); auto eye3dLandmark28Lefts = builder.CreateVector(eye3dLandmark28LeftVector); std::vector<flatbuffers::Offset<const VPoint3 *>> eye3dLandmark28RightVector; eye3dLandmark28RightVector.push_back(point1); eye3dLandmark28RightVector.push_back(point2); eye3dLandmark28RightVector.push_back(point3); auto eye3dLandmark28Rights = builder.CreateVector(eye3dLandmark28RightVector);
auto gestureName1 = builder.CreateString("gesture1"); short gestureIndex1 = 12345; auto gestureName2 = builder.CreateString("gesture2"); short gestureIndex2 = 6789; auto gesture1 = CreateGestureInfo(builder, gestureName1, gestureIndex1); auto gesture2 = CreateGestureInfo(builder, gestureName2, gestureIndex2);
// Create a FlatBuffer's `vector` from the `std::vector`. // 创建FaceInfo的Table型数组对象:gesture_info_list std::vector<flatbuffers::Offset<GestureInfo>> gestureVector; gestureVector.push_back(gesture1); gestureVector.push_back(gesture2); auto gestureList = builder.CreateVector(gestureVector);
// 我们现在有了一个可以存储在磁盘上或通过网络发送的 FlatBuffer。 // ** file/network code goes here :) ** // access builder.GetBufferPointer() for builder.GetSize() bytes
// 下面,我们立即访问序列化之后的buffer数据(就好像我们刚刚收到它一样)。 auto newFaceInfo = GetFaceInfo(buffer); // Get and test some scalar types from the FlatBuffer. assert(newFaceInfo->name()->str() == "MyFace"); ALOGD(TAG, "deserialization object name:%s", newFaceInfo->name()->str().c_str()); assert(newFaceInfo->index() == 123);