Serializing Tracking Data

The Frame class provides methods for serializing the data in a frame as a sequence of bytes. You can pass this sequence of bytes to the deserializer to create a new copy of the original frame.

Serializing a Frame

To serialize a Frame instance, use Frame.Serialize.

The following example serializes a frame and saves it to a file.

byte[] serializedFrame = frame.Serialize;
System.IO.File.WriteAllBytes ("frame.data", serializedFrame);

Deserializing a Frame

Before you can deserialize Frame data, you must create a Controller object. Accessing images stored in the frame data is not supported at this time. Deserializing data serialized by a different version of the Leap Motion SDK is not officially supported; it may work if there are no data model differences between the two versions.

To deserialize frame data:

  1. Create a Controller instance if one doesn’t already exist.
  2. Acquire the data and, if necessary, convert it into the proper data type.
  3. Create a Frame object.
  4. Call the new frame’s Deserialize() function.

Note that the reconstructed data replaces the existing data in the frame. If you use an existing, valid Frame instance, its existing data is replaced; however, any child objects, like hands or fingers, remain valid as long as you maintain a separate reference to them.

The following example loads data from a file and deserializes it to create a Frame object.

Controller controller = new Controller (); //An instance must exist
byte[] frameData = System.IO.File.ReadAllBytes ("frame.data"); 
Frame reconstructedFrame = new Frame ();
reconstructedFrame.Deserialize (frameData);

Saving and Loading Multiple Frames

If you wish to save several frames to the same file, you will have to devise a way to tell where one frame ends and the next begins. The size of the data can vary from frame-to-frame. The following example illustrates one way to do this, which is to store a 4 byte integer containing the size of the data immediately before the data itself.

using (System.IO.BinaryWriter writer = 
    new System.IO.BinaryWriter (System.IO.File.Open ("file.data", System.IO.FileMode.Create))) {
    for (int f = 9; f >= 0; f--) {
        Frame frameToSerialize = controller.Frame (f);
        byte[] serialized = frameToSerialize.Serialize;
        Int32 length = serialized.Length;
        writer.Write (length);
        writer.Write (serialized);
    }
}

To read the frame data from the saved file, you can then read the first 4 bytes of the file to determine how much data to read to get an entire frame. Simply repeat the process until you reach the end of the file.

Controller leapController = new Controller ();
using (System.IO.BinaryReader br = 
    new System.IO.BinaryReader (System.IO.File.Open ("file.data", System.IO.FileMode.Open))) {
    while (br.BaseStream.Position < br.BaseStream.Length) {
        Int32 nextBlock = br.ReadInt32 ();
        byte[] frameData = br.ReadBytes (nextBlock);
        Frame newFrame = new Frame ();
        newFrame.Deserialize (frameData);
    }
}

Serialization is not implemented internally in C# at this time. However, since it is now possible to create valid Frame, Hand, and other API objects, it is also feasible for API users to provide their own serialization and deserialization implementations.