问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingMicrosoft.Kinect;usingSystem.Diagnostics;usingSystem.IO;namespaceDepthTextStream{///<summary>///InteractionlogicforMainWindow.xaml///</summary>publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();}constfloatMaxDepthDistance=4095;//maxvaluereturnedconstfloatMinDepthDistance=850;//minvaluereturnedconstfloatMaxDepthDistanceOffset=MaxDepthDistance-MinDepthDistance;privatevoidWindow_Loaded(objectsender,RoutedEventArgse){kinectSensorChooser1.KinectSensorChanged+=newDependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged);}voidkinectSensorChooser1_KinectSensorChanged(objectsender,DependencyPropertyChangedEventArgse){varoldSensor=(KinectSensor)e.OldValue;//stoptheoldsensorif(oldSensor!=null){oldSensor.Stop();oldSensor.AudioSource.Stop();}//getthenewsensorvarnewSensor=(KinectSensor)e.NewValue;if(newSensor==null){return;}//turnonfeaturesthatyouneednewSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);newSensor.SkeletonStream.Enable();//signupforeventsifyouwanttogetatAPIdirectlynewSensor.AllFramesReady+=newEventHandler(newSensor_AllFramesReady);try{newSensor.Start();}catch(System.IO.IOException){//thishappensifanotherappisusingtheKinectkinectSensorChooser1.AppConflictOccurred();}}voidnewSensor_AllFramesReady(objectsender,AllFramesReadyEventArgse){short[]depthData;using(DepthImageFramedepthFrame=e.OpenDepthImageFrame())//createanewframeeverytimeoneisready{//assignavaluetodepthDatadepthData=newshort[depthFrame.PixelDataLength];}}privatevoidSaveDepthData(short[]depthData){//initializeaStreamWriterStreamWritersw=newStreamWriter(@"C:/Example.txt");//searchthedepthdataandaddittothefilefor(inti=0;i<depthData.Length;i++){sw.WriteLine(depthData[i]+"n");//nforanewline}//disposeofswsw.Close();SaveDepthData(depthData);}privatevoidWindow_Closing(objectsender,System.ComponentModel.CancelEventArgse){StopKinect(kinectSensorChooser1.Kinect);}privatevoidStopKinect(KinectSensorsensor){if(sensor!=null){if(sensor.IsRunning){//stopsensorsensor.Stop();//stopaudioifnotnullif(sensor.AudioSource!=null){sensor.AudioSource.Stop();}}}}}}