2013-11-18 2 views
0

Я хотел бы хранить данные скелета, снятые камерой kinect для будущего использования на моем жестком диске или в любой папке. Не могли ли вы помочь мне, пожалуйста??? Я буду получать данные скелета в будущем и использовать его для сравнения в будущем. Вот мой код ....сохранение данных скелета в файле

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.IO; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using Microsoft.Kinect; 

namespace KinectUserHeight 
{ 
    /// Interaction logic for MainWindow.xaml 

    public partial class MainWindow : Window 
    { 
     KinectSensor _sensor; 
     Byte[] pixelData; 
     string image_name; 
     string[] joint_intdex; 

     public MainWindow() 
     { 
      InitializeComponent(); 

     } 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 

      _sensor = KinectSensor.KinectSensors.Where(x => x.Status == KinectStatus.Connected).FirstOrDefault(); 

      if (_sensor != null) 
      { 
       _sensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(Sensor_SkeletonFrameReady); 
       _sensor.SkeletonStream.Enable(); 
       if (!this._sensor.ColorStream.IsEnabled) 
       { 
        this._sensor.ColorStream.Enable(); 
       } 
       this._sensor.ColorFrameReady += sensor_ColorFrameReady; 
      } 
     } 

     // new 1 
     void sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e){ 
      using (ColorImageFrame imageFrame = e.OpenColorImageFrame()) 
      { 
      // Check if the incoming frame is not null 
      if (imageFrame == null) 
      { 
      return; 
      } 
      else 
      { 
      // Get the pixel data in byte array 
      this.pixelData = new byte[imageFrame.PixelDataLength]; 
      // Copy the pixel data 
      imageFrame.CopyPixelDataTo(this.pixelData); 
      // Calculate the stride 
      int stride = imageFrame.Width * imageFrame.BytesPerPixel; 
      // assign the bitmap image source into image control 
      this.VideoControl.Source = BitmapSource.Create(
      imageFrame.Width, 
      imageFrame.Height, 
      96, 
      96, 
      PixelFormats.Bgr32, 
      null, 
      pixelData, 
      stride); 
      } 
      } 

     } 

     void Sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) 
     { 
      using (var frame = e.OpenSkeletonFrame()) 
      { 
       if (frame != null) 
       { 
        canvas.Children.Clear(); 

        Skeleton[] skeletons = new Skeleton[frame.SkeletonArrayLength]; 

        frame.CopySkeletonDataTo(skeletons); 

        var skeleton = skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault(); 

        if (skeleton != null) 
        { 
         // Calculate height. 
         double height = Math.Round(skeleton.Height(), 2); 

         // Draw skeleton joints. 
          foreach (JointType joint in Enum.GetValues(typeof(JointType))) 
          { 
           DrawJoint(skeleton.Joints[joint].ScaleTo(640, 480)); 
           string x = skeleton.Joints[joint].Position.X.ToString(); 
           x_pos.Content = x; 
           joint_intdex[(int)joint] = x; 
          } 
         // System.IO.File.WriteAllText(Environment.SpecialFolder.MyComputer); 
         // Display height. 
         tblHeight.Text = "Height: " + height.ToString() + "m"; 
        } 
       } 
      } 
     } 

     public string arrayToString(System.Collections.ArrayList ar) 
     { 
      StringBuilder sb = new StringBuilder(); 
      System.Xml.XmlWriterSettings st = new System.Xml.XmlWriterSettings(); 
      st.OmitXmlDeclaration = true; 
      st.Indent = false; 
      System.Xml.XmlWriter w = System.Xml.XmlWriter.Create(sb, st); 
      System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(ar.GetType()); 
      s.Serialize(w, ar); 
      w.Close(); 
      return sb.ToString(); 
     } 

     private void DrawJoint(Joint joint) 
     { 
      Ellipse ellipse = new Ellipse 
      { 
       Width = 10, 
       Height = 10, 
       Fill = new SolidColorBrush(Colors.LightCoral) 
      }; 

      Canvas.SetLeft(ellipse, joint.Position.X); 
      Canvas.SetTop(ellipse, joint.Position.Y); 

      canvas.Children.Add(ellipse); 
     } 

     private void btnConnect_Click(object sender, RoutedEventArgs e) 
     { 
      _sensor.Start(); 
     } 

     private void btnDisconnect_Click(object sender, RoutedEventArgs e) 
     { 
      _sensor.Stop(); 
     } 

     private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) 
     { 
      lblValue.Content = (int)slider1.Value; 
     } 

     private void btn_angleAdjust_Click(object sender, RoutedEventArgs e) 
     {   
      if (this._sensor.ElevationAngle < this._sensor.MaxElevationAngle || this._sensor.ElevationAngle > this._sensor.MinElevationAngle) 
      this._sensor.ElevationAngle = (int)slider1.Value; 
     } 

     private void btn_save_Click(object sender, RoutedEventArgs e) 
     { 
      image_name = image_handler.Text; 
      image_name += ".png"; 

      string filePath = nvironment.GetFolderPath(Environment.SpecialFolder.MyComputer); 
      filePath = filePath + @"G:\thesis_picture\"; 
      if (!Directory.Exists(filePath)) 
      { 
       Directory.CreateDirectory(filePath); 
      } 

      //string path =  System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), image_name); 
      string path = System.IO.Path.Combine(filePath, image_name); 
       using (FileStream fs = new FileStream(path, FileMode.Create)) 
       { 
        BitmapSource imageSource = (BitmapSource)VideoControl.Source; 
        JpegBitmapEncoder jpegEncoder = new JpegBitmapEncoder(); 
        jpegEncoder.Frames.Add(BitmapFrame.Create(imageSource)); 
        jpegEncoder.Save(fs); 
        fs.Close(); 
       } 
     } 
    } 
} 
+1

Пытаться ограничить ваш пример кода только для соответствующих частей. –

ответ

Смежные вопросы