2014/01/03

XNA 使用Kinect並顯示影像

寫過Kinect會知道顯示影像是透過byte的array去取得,在將值讓UI去呈現
XNA則是透過座標以及一些方框去接收,透過Draw方法將影像描繪出來


程式碼:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Kinect;

namespace WindowsGame3
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {

        /// <summary>
        /// 圖形管理
        /// </summary>
        GraphicsDeviceManager graphics;


        /// <summary>
        /// 繪圖
        /// </summary>
        SpriteBatch spriteBatch;


        /// <summary>
        /// Kinect Sensor
        /// </summary>
        KinectSensor kinect;


        /// <summary>
        /// Kinect框架
        /// </summary>
        Texture2D texture2D;

        /// <summary>
        /// 矩形
        /// </summary>
        readonly Rectangle rect = new Rectangle(0, 0, 640, 480);

        /// <summary>
        /// 影像資料
        /// </summary>
        byte[] dataPixels;

        /// <summary>
        /// Kinect初始化
        /// </summary>
        bool isKinectInit = false;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }


        /// <summary>
        /// Kinect初始化
        /// </summary>
        private void Init()
        {
            var sensor = ((from s in KinectSensor.KinectSensors where (s.IsRunning != true && s.Status == KinectStatus.Connected) select s)).FirstOrDefault();

            if (sensor == null)
                isKinectInit = false;
            else
            {
                this.kinect = sensor;
                this.kinect.Start();
                this.kinect.ColorStream.Enable();
                this.kinect.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinect_ColorFrameReady);
            }
        }

        void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame == null)
                    return;

                dataPixels = new byte[frame.PixelDataLength];
                frame.CopyPixelDataTo(dataPixels);

                texture2D = new Texture2D(GraphicsDevice, 640, 480);
                texture2D.SetData(dataPixels);

            }
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Init();
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            if (texture2D != null)
            {

                spriteBatch.Begin();
                spriteBatch.Draw(texture2D, rect, Color.White);
                spriteBatch.End();
            }
            base.Draw(gameTime);
        }
    }
}

在Kinect影像事件觸發時,將資料塞入Texture2D物件,透過Rectangle指定初始位址以及寬高
void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame == null)
                    return;

                dataPixels = new byte[frame.PixelDataLength];
                frame.CopyPixelDataTo(dataPixels);

                texture2D = new Texture2D(GraphicsDevice, 640, 480);
                texture2D.SetData(dataPixels);

            }
        }

執行結果:

影像色澤偏藍可以參閱『Kinect+XNA彩色資訊』文章,裡面提到Kinect採用『BGRA』格式,而XNA採用『RGBA』格式
因此將BGRA轉RGBA就不會在出現藍藍的影像

改一下『kinect_ColorFrameReady』方法
void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame == null)
                    return;

                dataPixels = new byte[frame.PixelDataLength];
                frame.CopyPixelDataTo(dataPixels);

                Color[] colors = new Color[frame.Width * frame.Height];
                int sourceOffset = 0;
                int colorsLength = colors.Length;

                for (int index = 0; index < colorsLength; index++)
                {
                    colors[index] = new Color(dataPixels[sourceOffset + 2],
                        dataPixels[sourceOffset + 1], dataPixels[sourceOffset], 255);
                    sourceOffset += 4;
                }


                texture2D = new Texture2D(GraphicsDevice, 640, 480);
                texture2D.SetData(colors);

            }
        }

修改後成果:


參考資料:
http://gofly07.blogspot.tw/2013/02/kinectxna_20.html