How to get the coordinates of the body in a file.txt in Visual Studio, C #

0

Very good, I am trying to save in a file.txt the coordinates that Kinect gives me of the different Joints of the body in Visual Studio. But as it is an array of type object it does not let me pass to string by the following form: This would be the new part implemented

                        //Mano Derecha
                        VariablesGlobales.HandRightX = body.Joints[JointType.HandRight].Position.X;
                        VariablesGlobales.HandRightY = body.Joints[JointType.HandRight].Position.Y;
                        VariablesGlobales.HandRightZ = body.Joints[JointType.HandRight].Position.Z;


                        //ManoIzquierda
                        VariablesGlobales.HandLeftX = body.Joints[JointType.HandLeft].Position.X;
                        VariablesGlobales.HandLeftY = body.Joints[JointType.HandLeft].Position.Y;
                        VariablesGlobales.HandLeftZ = body.Joints[JointType.HandLeft].Position.Z;


                        //Cabeza 
                        VariablesGlobales.HeadX = body.Joints[JointType.Head].Position.X;
                        VariablesGlobales.HeadY = body.Joints[JointType.Head].Position.Y;
                        VariablesGlobales.HeadZ = body.Joints[JointType.Head].Position.Z;


                        // Parte central Torso
                        VariablesGlobales.SpineMidX = body.Joints[JointType.SpineMid].Position.X;
                        VariablesGlobales.SpineMidY = body.Joints[JointType.SpineMid].Position.Y;
                        VariablesGlobales.SpineMidZ = body.Joints[JointType.SpineMid].Position.Z;


                        // Una de las soluciones es hacer un array para cada joint, como ultima opcion

                        DispatcherTimer dispatcher = new DispatcherTimer();
                        // Intervalo de tiempo de 1 segundo

                        dispatcher.Interval = new TimeSpan(0, 0, 1);
                        dispatcher.Tick += (s, a) =>
                         {
                             //MiArray[n] = body.Joints[JointType.HandLeft];
                             // Estoy guardando toda la informacion necesaria de los 25 joints del body, es decir TODO EL BODY
                             MiArray[n] = body;
                             m = 0;
                             using (StreamWriter sw = new StreamWriter(@"prueba.txt",true))
                             {

                                 sw.WriteLine(VariablesGlobales.HandLeftX);
                                 sw.WriteLine(VariablesGlobales.HandLeftY);


                             }
    
asked by Daniel Potrimba 22.05.2017 в 10:56
source

1 answer

3

First of all you must have the real type of your object so that you can access it. It would be very useful if you could give us more information about the problem with sample code and / or some error or problem capture.

Currently I do not have VS available, but I can tell you that there is a better way to do things to write these values. First, you must make sure that VariablesGlobales.HandLeftX is a float , or another type of base value that you can print raw without it being a class that when you do .ToString() does not return but the type of object ( [System.Object] or something similar) but a value, for example 100.45 .

To write you should use a Stream, which can help you solve the problem:

using(StreamWriter sw = new StreamWriter("prueba.txt", true)) {
    sw.WriteLine(VariablesGlobales.HandLeftX);
    sw.WriteLine(VariablesGlobales.HandLeftY);
    sw.WriteLine(VariablesGlobales.HandRightX);
    sw.WriteLine(VariablesGlobales.HandRightY);
}

You must know all the properties of your object to write the values line by line. If you do not know them you should use Reflection. As for the loop I do not have much idea, I assume that in your program you should be constantly getting an event or looping with Timer or similar to see the new values. In that loop or routine, you should write to your file.

This should help you. Greetings!

    
answered by 22.05.2017 / 11:07
source