How to make a copy of an Kinect Skeleton object to

2019-08-06 11:23发布

问题:

I am using the Kinect Toolbox, so I have a list of ReplaySkeletonFrames in my hand. I am iterating over this list, getting the first tracked skeleton and modifying some properties.

As we know, when we change an object we also change the original object.

I need to make a copy of an skeleton.

Note: I can't use CopySkeletonDataTo() because my frame is a ReplaySkeletonFrame and not the ReplayFrame of the "normal" Kinect.

I tried to make my own method that copies property by property, but some properties could not be copied. look...

 public static Skeleton Clone(this Skeleton actualSkeleton)
    {
        if (actualSkeleton != null)
        {
            Skeleton newOne = new Skeleton();

 // doesn't work - The property or indexer 'Microsoft.Kinect.SkeletonJoints'
 // cannot be used in this context because the set accessor is inaccessible
            newOne.Joints = actualSkeleton.Joints;

 // doesn't work - The property or indexer 'Microsoft.Kinect.SkeletonJoints' 
 // cannot be used in this context because the set accessor is inaccessible
            JointCollection jc = new JointCollection();
            jc = actualSkeleton.Joints;
            newOne.Joints = jc;

            //...

        }

        return newOne;

    }

How to solve it?

回答1:

with more search i ended up whit the following solution: Serialize the skeleton to the memory, deserialize to a new object

Here is the code

 public static Skeleton Clone(this Skeleton skOrigin)
    {
        // isso serializa o skeleton para a memoria e recupera novamente, fazendo uma cópia do objeto
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter();

        bf.Serialize(ms, skOrigin);

        ms.Position = 0;
        object obj = bf.Deserialize(ms);
        ms.Close();

        return obj as Skeleton;
    }