2012年2月28日 星期二

Kinect SDK V1.0 (1)

一時間看不來正式版SDK,測試入門語法。
ref: http://channel9.msdn.com/Series/KinectQuickstart/Camera-Fundamentals
1. 環境: SDK installed.
2. Open new WPF project.
3. Add Reference
     Microsoft.Kinect
     Microsoft.Samples.Kinect.WpfViewers"

4. Add using namespace Microsoft.Kinect
5. Add (@ Xaml file)
    xmlns:my="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers"
6. Add 2 xaml tags

 <my:KinectSensorChooser  Margin="234,104,0,0" Name="kinectSensorChooser1"  />
        <my:KinectColorViewer Width="640" Height="480" Kinect="{Binding ElementName=kinectSensorChooser1, Path=Kinect}"></my:KinectColorViewer>
7. Add Window_Loaded event method
    kinectSensorChooser1.KinectSensorChanged += new DependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged);
8. Copy  private void StopKinect(KinectSensor sensor) from refernce
     private void StopKinect(KinectSensor sensor)
        {
            if (sensor != null)
            {
                if (sensor.IsRunning)
                {
                    //stop sensor
                    sensor.Stop();

                    //stop audio if not null
                    if (sensor.AudioSource != null)
                    {
                        sensor.AudioSource.Stop();
                    }


                }
            }
        }
9. Add event method

void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var oldSensor = (KinectSensor)e.OldValue;
            //stop the old sensor
            if (oldSensor != null)
            {
                StopKinect(oldSensor);
            }
            //get the new sensor
            var newSensor = (KinectSensor)e.NewValue;
            if (newSensor == null)
            {
                return;
            }
            //newSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(newSensor_AllFramesReady);
            //turn on features that you need
            newSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
            //newSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
            //newSensor.SkeletonStream.Enable();
            try
            {
                newSensor.Start();
            }
            catch (System.IO.IOException)
            {
                //this happens if another app is using the Kinect
                kinectSensorChooser1.AppConflictOccurred();
            }
        }


10. Add Window_Closing event method to close the kinect.


沒有留言:

張貼留言