Created
April 9, 2013 09:42
-
-
Save maxkandler/5344437 to your computer and use it in GitHub Desktop.
Serializing Kinect-Skeletons and transmitting them via MemoryMappedFiles to another process.
These are just snippets and won't run without further Code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This process uses the data comming from the kinect processes. | |
// Read from the MemoryMappedFile | |
MemoryMappedViewAccessor accessor = memoryMappedFile.createViewAccessor(); | |
// Read to Array | |
accessor.ReadArray<byte>(0, mmf_result, 0, mmf_result.Length); | |
// Use a local Array for further instructions | |
byte[] temp = new byte[mmf_result.Length]; | |
Array.Copy(mmf_result, temp, mmf_result.Length); | |
// Check if the Byte Array contains only 00-Bytes | |
empty = temp.All(B => B == default(Byte)); | |
// if not: attempt to create a skelton | |
if(!empty) | |
{ | |
BinaryFormatter bf = new BinaryFormatter(); | |
MemoryStream ms = new MemoryStream(temp); | |
Skeleton skelNew = (Skeleton)bf.Deserialize(ms); | |
this.skeletons.Add(skelNew); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This process is connected to the kinect. | |
// MemoryMappedFile has already been setup. | |
// Skeleton has already been loaded from the sensor. | |
Skeleton skel = this.skeletons[0]; | |
// 1: Serialize Skeleton to Byte-Array | |
BinaryFormatter bf = new BinaryFormatter(); | |
MemoryStream ms = new MemoryStream(); | |
bf.Serialize(ms, skel); | |
byte[] serializedSkel = ms.ToArray(); | |
ms.Close(); | |
// 2: Transmit Skeleton to MemoryMappedFile | |
// Transmit Skeleton Data | |
MemoryMappedViewAccessor writer = memoryMappedFile.createViewAccessor(); | |
writer.WriteArray<byte>(0, serializedSkel, 0, serializedSkel.Length); | |
// If no Skeleton is available transmit an empty byte-array. | |
// Otherwise the last Skeleton will remain in the memory-mapped file forever. | |
byte[] empty = new byte[1] { 0 }; | |
writer.WriteArray<byte>(0, empty, 0, empty.Length); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment