Getting Started with Kinect

Thanks to Brother Hu for your support, Yushen and I can make a little progress.
I would also like to thank all the great gods on the Internet for their selfless dedication.
Although I have seen C# and played Kinect more or less before, it only stays at the level of playing.
Tonight, with the encouragement of Brother Hu, I finally started to learn Kinect step by step more formally and harder.

Since we didn't have comprehensive study materials, Yushen and I searched for various materials on the Internet to learn. I also successfully turned to the other side of the wall to see the wonderful world, but in the end, I couldn't find a more suitable resource. . Yushen found a blog with his broken computer, and posted it here first for everyone to learn and reference, thank the blogger for his selfless sharing
http://www.cnblogs.com/yangecnu/

Well, this is the case, because the blog post is a translation The article, we are based on this to learn, the first article is the basic environment configuration and so on, so I won't repeat it.
It should be noted that if you want to develop and learn, you should pay attention to the matching of the version of the device and the SDK package. 1.0 can be used with Kinect, and 2.0 should be used with the latest Kinect 2.0, because the author is shy, There is no experiment with the latest.

It took a lot of time tonight because when we were conducting the second set of experiments, because we had the source code, we just watched it and got started. No matter how much we understood, let’s try to run it. The

source code is sent first:
using System;
using System.Collections.Generic;
using System.Linq;
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 WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private KinectSensor kinect;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += (s, e) => DiscoverKinectSensor();
            this.Unloaded += (s, e) => this.kinect = null;
        }

        public KinectSensor Kinect
        {
            get { return this.kinect; }
            set
            {
                / /If the sensor with the assignment is different from the current one
                if (this.kinect != value)
                {
                    //If the current sensor object is not null
                    if (this.kinect != null)
                    {
                        UninitializeKinectSensor(this.kinect);
                        / /uninitailize the current object
                        this.kinect = null;
                    }
                    //If the incoming object is not empty and the state is connected
                    if (value != null && value.Status == KinectStatus.Connected)
                    {
                        this.kinect = value;
                        InitializeKinectSensor(this.kinect);
                    }
                }
            }
        }

        private void DiscoverKinectSensor()
        {
            KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;
            this.Kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
        }

        private void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case KinectStatus.Connected:
                    if (this.kinect == null)
                        this.kinect = e.Sensor;
                    break;
                case KinectStatus.Disconnected:
                    if (this.kinect == e.Sensor)
                    {
                        this.kinect = null;
                        this.kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
                        if (this.kinect == null)
                        {
                            //TODO: notify for Kinect to be unplugged
                        }
                    }
                    break;
                //TODO: handle state in other cases
            }
        }

        private void InitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                kinectSensor.ColorStream.Enable( );
                kinectSensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
                kinectSensor.Start();
            }
        }

        private void UninitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                kinectSensor.Stop();
                kinectSensor.ColorFrameReady -= new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
            }
        }

        void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame != null)
                {
                    byte[] pixelData = new byte[frame.PixelDataLength];
                    frame.CopyPixelDataTo(pixelData);
                    ColorImageElement.Source = BitmapImage.Create(frame.Width, frame.Height, 96, 96,
                                                                 PixelFormats.Bgr32, null, pixelData,
                                                                 frame.Width * frame.BytesPerPixel);

                }
            }
        }
    }
}


<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"Width="525">
    <Grid>
        <Image x:Name="ColorImageElement"></Image>
    </Grid>
</Window>


When debugging, there are several problems that may occur to everyone.
When you create a project, remember to choose a WPF application.
Secondly , pay attention to the namespace, and do not copy the file name
. Of course, you must remember to add a reference set. This is a mistake that beginners may make a lot of times, and there are many people. I don't know how to do this step, so I will teach as follows:
first click on the project -> add
reference , a dialog box will pop up, select the assembly on the left, and then search for the keyword "Kinect" in the search box in the upper right corner, It will appear that Microsoft Kinect has a version number on the right. Since the hardware device I used is kinect, I chose version 1.0, depending on the situation. Then click OK to reference kinect.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327010119&siteId=291194637