|
// Written by Shai Raiten: http://blogs.microsoft.co.il/blogs/shair/
using System;
using KinectReception.Enums;
using Microsoft.Research.Kinect.Nui;
namespace KinectReception.Managers
{
public class KinectManager : IDisposable
{
private static KinectManager _instance = new KinectManager();
private DateTime _lastTime = DateTime.MaxValue;
private int _totalFrames = 0;
private int _lastFrames = 0;
//Allow the user to restart the kinect runtime from the control panel
public delegate void InitializeHandler(object sender);
public event InitializeHandler ReInitialize;
public bool IsInitialize { get; set; }
public string StatusMessage { get; set; }
public Runtime KinectNui;
public Camera Camera;
public static KinectManager GetInstance
{
get { return _instance; }
}
public KinectManager()
{
try
{
//• Declares _kinectNui as a Runtime object, which represents the Kinect sensor instance.
KinectNui = new Runtime();
//Open the video and depth streams, and sets up the event handlers that
//the runtime calls when a video, depth, or skeleton frame is ready
//An application must initialize the Kinect sensor by calling Runtime.
//Initialize before calling any other methods on the Runtime object.
KinectNui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking |
RuntimeOptions.UseColor);
//To stream color images:
// • The options must include UseColor.
// • Valid image resolutions are Resolution1280x1024 and Resolution640x480.
// • Valid image types are Color, ColorYUV, and ColorYUVRaw.
KinectNui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480,
ImageType.ColorYuv);
KinectNui.SkeletonEngine.TransformSmooth = true;
var parameters = new TransformSmoothParameters
{
Smoothing = 1.0f,
Correction = 0.1f,
Prediction = 0.1f,
JitterRadius = 0.05f,
MaxDeviationRadius = 0.05f
};
KinectNui.SkeletonEngine.SmoothParameters = parameters;
_lastTime = DateTime.Now;
Camera = KinectNui.NuiCamera;
IsInitialize = true;
StatusMessage = Properties.Resources.KinectReady;
}
catch (InvalidOperationException ex)
{
IsInitialize = false;
StatusMessage = ex.Message;
}
}
public void Start()
{
try
{
_instance = new KinectManager();
ReInitialize(this);
}
catch (Exception exception)
{
StatusMessage = exception.Message;
}
}
public void ChangeCameraAngle(ChangeDirection dir)
{
if (!IsInitialize) return;
try
{
if (dir == ChangeDirection.Up)
Camera.ElevationAngle = Camera.ElevationAngle +
Properties.Settings.Default.ElevationAngleInterval;
else
Camera.ElevationAngle = Camera.ElevationAngle -
Properties.Settings.Default.ElevationAngleInterval;
StatusMessage = Properties.Resources.KinectReady;
}
catch (InvalidOperationException ex)
{
StatusMessage = ex.Message;
}
catch (ArgumentOutOfRangeException outOfRangeException)
{
StatusMessage = outOfRangeException.Message;
}
}
public void Close()
{
Dispose();
}
public void Dispose()
{
if (KinectNui == null) return;
KinectNui.Uninitialize();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.
My Blog: http://blogs.microsoft.co.il/blogs/shair/