diff --git a/Sources/Lib/ColorAndBodyImageStream.cs b/Sources/Lib/ColorAndBodyImageStream.cs new file mode 100644 index 0000000..5b753e4 --- /dev/null +++ b/Sources/Lib/ColorAndBodyImageStream.cs @@ -0,0 +1,97 @@ +//using Microsoft.Kinect; +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; +//using System.Threading.Tasks; +//using System.Windows.Media.Imaging; +//using System.Windows.Media; +//using System.Windows; +//using System.Windows.Shapes; + +//namespace Lib +//{ +// class ColorAndBodyImageStream : KinectStream +// { +// private BodyFrameReader reader; +// public BodyFrameReader Reader +// { +// get { return reader; } +// set { reader = value; } +// } + +// private Body[] bodies; +// public Body[] Bodies +// { +// get { return bodies; } +// private set { bodies = value; } +// } +// public ColorAndBodyImageStream(KinectManager kinectManager) : base(kinectManager) +// { +// var frameDescription = KinectManager.Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); +// this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); +// reader = KinectManager.Sensor.BodyFrameSource.OpenReader(); +// reader.FrameArrived += Reader_BodyFrameArrived; +// // Initialiser le tableau des corps +// this.bodies = new Body[KinectManager.Sensor.BodyFrameSource.BodyCount]; +// } + +// private void DrawSkeleton(Body body) +// { +// foreach (JointType jointType in body.Joints.Keys) +// { +// Joint joint = body.Joints[jointType]; +// if (joint.TrackingState == TrackingState.Tracked) +// { +// // Convertir les coordonnées du joint en coordonnées de l'écran +// Point point = new Point(); +// ColorSpacePoint colorPoint = KinectManager.Sensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position); +// point.X = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X; +// point.Y = float.IsInfinity(colorPoint.Y) ? 0 : colorPoint.Y; + +// // Dessiner le joint +// DrawJoint(point); +// } +// } + +// // Dessinez les os ici si nécessaire +// } + +// private void DrawJoint(Point point) +// { +// Ellipse ellipse = new Ellipse +// { +// Width = 10, +// Height = 10, +// Fill = new SolidColorBrush(Colors.Red) +// }; + +// Canvas.SetLeft(ellipse, point.X - ellipse.Width / 2); +// Canvas.SetTop(ellipse, point.Y - ellipse.Height / 2); + +// skeletonCanvas.Children.Add(ellipse); +// } +// private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) +// { +// using (var bodyFrame = e.FrameReference.AcquireFrame()) +// { +// if (bodyFrame != null) +// { +// bodyFrame.GetAndRefreshBodyData(this.bodies); + +// skeletonCanvas.Children.Clear(); // Nettoyer le canvas avant de dessiner + +// foreach (var body in this.bodies) +// { +// if (body.IsTracked) +// { +// // Dessiner le squelette +// DrawSkeleton(body); +// } +// } +// } +// } +// } +// } +//} + diff --git a/Sources/Lib/ColorImageStream.cs b/Sources/Lib/ColorImageStream.cs index 8bdd2a2..2580220 100644 --- a/Sources/Lib/ColorImageStream.cs +++ b/Sources/Lib/ColorImageStream.cs @@ -18,17 +18,26 @@ namespace Lib reader = value; } } - public ColorImageStream() : base() + public ColorImageStream(KinectManager kinectManager) : base(kinectManager) { var frameDescription = KinectManager.Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); reader = KinectManager.Sensor.ColorFrameSource.OpenReader(); - reader.FrameArrived += Reader_FrameArrived; + reader.FrameArrived += Reader_ColorFrameArrived; } - private void Reader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) + public override void Stop() { - using (var colorFrame = e.FrameReference.AcquireFrame()) + if (reader != null) + { + reader.Dispose(); + reader = null; + } + } + + private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) + { + using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { diff --git a/Sources/Lib/DepthImageStream.cs b/Sources/Lib/DepthImageStream.cs new file mode 100644 index 0000000..1cdafd9 --- /dev/null +++ b/Sources/Lib/DepthImageStream.cs @@ -0,0 +1,90 @@ +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media.Imaging; +using System.Windows.Media; +using System.Windows; + +namespace Lib +{ + class DepthImageStream : KinectStream + { + private DepthFrameReader reader; + public DepthFrameReader Reader + { + get + { + return reader; + } + set + { + reader = value; + } + } + private byte[] depthPixels; + public byte[] DepthPixels { + get { + return depthPixels; + } + set + { + depthPixels = value; + } + } + public DepthImageStream(KinectManager kinectManager) : base(kinectManager) + { + var frameDescription = KinectManager.Sensor.DepthFrameSource.FrameDescription; + this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null); + reader = KinectManager.Sensor.DepthFrameSource.OpenReader(); + reader.FrameArrived += Reader_DepthFrameArrived; + DepthPixels = new byte[frameDescription.Width * frameDescription.Height]; + } + private void Reader_DepthFrameArrived(object sender, DepthFrameArrivedEventArgs e) + { + using (DepthFrame depthFrame = e.FrameReference.AcquireFrame()) + { + if (depthFrame != null) + { + FrameDescription depthFrameDescription = depthFrame.FrameDescription; + + // Créez un tableau pour stocker les données de profondeur + ushort[] depthData = new ushort[depthFrameDescription.LengthInPixels]; + depthFrame.CopyFrameDataToArray(depthData); + + // Traitez les données de profondeur + ProcessDepthFrameData(depthData, depthFrameDescription.LengthInPixels, depthFrame.DepthMinReliableDistance, depthFrame.DepthMaxReliableDistance); + + // Mettez à jour le bitmap de profondeur + this.Bitmap.WritePixels( + new Int32Rect(0, 0, depthFrameDescription.Width, depthFrameDescription.Height), + this.depthPixels, + depthFrameDescription.Width, + 0); + } + } + } + + public override void Stop() + { + if (reader != null) + { + reader.Dispose(); + reader = null; + } + } + + private void ProcessDepthFrameData(ushort[] depthData, uint depthFrameDataSize, ushort minDepth, ushort maxDepth) + { + // Convertir les données de profondeur en niveaux de gris + for (int i = 0; i < depthFrameDataSize; ++i) + { + ushort depth = depthData[i]; + DepthPixels[i] = (byte)(depth >= minDepth && depth <= maxDepth ? (depth % 256) : 0); + } + } + + } +} diff --git a/Sources/Lib/InfraredImageStream.cs b/Sources/Lib/InfraredImageStream.cs new file mode 100644 index 0000000..fd0fef8 --- /dev/null +++ b/Sources/Lib/InfraredImageStream.cs @@ -0,0 +1,92 @@ +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media.Imaging; +using System.Windows.Media; +using System.Windows; + +namespace Lib +{ + class InfraredImageStream : KinectStream + { + private InfraredFrameReader reader; + private byte[] infraredPixels; + public byte[] InfraredPixels + { + get + { + return infraredPixels; + } + private set + { + infraredPixels = value; + } + } + public InfraredFrameReader Reader + { + get + { + return reader; + } + private set + { + reader = value; + } + } + public InfraredImageStream(KinectManager kinectManager) : base(kinectManager) + { + var frameDescription = KinectManager.Sensor.InfraredFrameSource.FrameDescription; + this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null); + reader = KinectManager.Sensor.InfraredFrameSource.OpenReader(); + reader.FrameArrived += Reader_InfraredFrameArrived; + infraredPixels = new byte[frameDescription.Width * frameDescription.Height]; + } + + public override void Stop() + { + if (reader != null) + { + reader.Dispose(); + reader = null; + } + } + private void ProcessInfraredFrameData(ushort[] frameData, uint frameDataSize) + { + // Convertir les données infrarouges en niveaux de gris + for (int i = 0; i < frameDataSize; ++i) + { + // Convertir la valeur infrarouge en une intensité lumineuse + byte intensity = (byte)(frameData[i] >> 8); + InfraredPixels[i] = intensity; + } + } + + private void Reader_InfraredFrameArrived(object sender, InfraredFrameArrivedEventArgs e) + { + using (InfraredFrame infraredFrame = e.FrameReference.AcquireFrame()) + { + if (infraredFrame != null) + { + FrameDescription infraredFrameDescription = infraredFrame.FrameDescription; + + // Créez un tableau pour stocker les données infrarouges + ushort[] infraredData = new ushort[infraredFrameDescription.LengthInPixels]; + infraredFrame.CopyFrameDataToArray(infraredData); + + // Traitez les données infrarouges + ProcessInfraredFrameData(infraredData, infraredFrameDescription.LengthInPixels); + + // Mettez à jour le bitmap infrarouge + this.Bitmap.WritePixels( + new Int32Rect(0, 0, infraredFrameDescription.Width, infraredFrameDescription.Height), + this.infraredPixels, + infraredFrameDescription.Width, + 0); + } + } + } + } +} diff --git a/Sources/Lib/KinectManager.cs b/Sources/Lib/KinectManager.cs index 9977b36..ac50260 100644 --- a/Sources/Lib/KinectManager.cs +++ b/Sources/Lib/KinectManager.cs @@ -7,10 +7,6 @@ namespace Lib public class KinectManager : INotifyPropertyChanged { private KinectSensor sensor; - - private BodyFrameReader bodyFrameReader; - private Body[] bodies = null; - public KinectSensor Sensor { get { return sensor; @@ -31,96 +27,77 @@ namespace Lib public KinectManager() { sensor = KinectSensor.GetDefault(); - if (sensor != null) + if (Sensor == null) { - bodyFrameReader = sensor.BodyFrameSource.OpenReader(); - bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived; + StatusText = "Kinect n'est pas connecté"; } - } - - private void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) - { - using (var frame = e.FrameReference.AcquireFrame()) + else if (!Sensor.IsOpen) { - if (frame != null) - { - if (bodies == null) - { - bodies = new Body[frame.BodyCount]; - } - - frame.GetAndRefreshBodyData(bodies); - } + StatusText = "Kinect n'est pas ouvert"; } - } - - public Body GetNextBody() - { - if (bodies == null) return null; - - foreach (var body in bodies) + else if (Sensor.IsAvailable) { - if (body.IsTracked) - { - return body; - } + StatusText = "Kinect est disponible"; + } + else + { + StatusText = "Kinect n'est pas disponible"; } - - return null; } - - public bool StartSensor() + public void StartSensor() { - if (sensor != null) { + if (sensor != null && !sensor.IsOpen) { sensor.Open(); - return true; } - else - { - return false; + else { + sensor = KinectSensor.GetDefault(); + sensor.Open(); } } - public bool StopSensor() + public void StopSensor() { if (sensor != null) { sensor.Close(); - return true; - } - else - { - return false; + sensor = null; } } public bool Status { get { return Sensor != null && Sensor.IsAvailable; } } + private string _statusText; public string StatusText { - get - { - if (Sensor == null) + get { return _statusText; } + set { + _statusText = value; + if(this.PropertyChanged != null) { - return "Kinect n'est pas connecté"; - } - else if (!Sensor.IsOpen) - { - return "Kinect n'est pas ouvert"; - } - else if (Sensor.IsAvailable) - { - return "Kinect est disponible"; - } - else - { - return "Kinect n'est pas disponible"; + this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText")); } } } + private void KinectSensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs args) - { - // Vous pouvez ajouter ici une logique supplémentaire si nécessaire - } - } -} + { + if (Sensor == null) + { + StatusText = "Kinect n'est pas connecté"; + } + else if (!Sensor.IsOpen) + { + StatusText = "Kinect n'est pas ouvert"; + } + else if (Sensor.IsAvailable) + { + StatusText = "Kinect est disponible"; + } + else + { + StatusText = "Kinect n'est pas disponible"; + } + + } + } + } \ No newline at end of file diff --git a/Sources/Lib/KinectStream.cs b/Sources/Lib/KinectStream.cs index 61d2fdf..29e5f66 100644 --- a/Sources/Lib/KinectStream.cs +++ b/Sources/Lib/KinectStream.cs @@ -22,7 +22,7 @@ namespace Lib if (bitmap != value) { bitmap = value; - OnPropertyChanged(nameof(bitmap)); + OnPropertyChanged(nameof(Bitmap)); } } } @@ -33,9 +33,13 @@ namespace Lib PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - public KinectStream() + public KinectStream(KinectManager kinectManager) { - KinectManager = new KinectManager(); + KinectManager = kinectManager; + if (kinectManager.Sensor == null) + { + KinectManager.StartSensor(); + } } public virtual void Start() @@ -43,9 +47,7 @@ namespace Lib KinectManager.StartSensor(); } - public virtual void Stop() - { - KinectManager.StopSensor(); - } + public abstract void Stop(); + } } diff --git a/Sources/Lib/KinectStreams.cs b/Sources/Lib/KinectStreams.cs new file mode 100644 index 0000000..43c3b97 --- /dev/null +++ b/Sources/Lib/KinectStreams.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lib +{ + public enum KinectStreams + { + None, + Color, + Depth, + IR + } +} diff --git a/Sources/Lib/KinectStreamsFactory.cs b/Sources/Lib/KinectStreamsFactory.cs index 63c55d7..7ac4345 100644 --- a/Sources/Lib/KinectStreamsFactory.cs +++ b/Sources/Lib/KinectStreamsFactory.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; @@ -9,9 +8,44 @@ namespace Lib { public class KinectStreamsFactory { - private Dictionary> streamFactory; - public KinectStreamsFactory() { - + private KinectManager _kinectManager; + public KinectManager KinectManager { + get { return _kinectManager; } + private set { _kinectManager = value; } + } + + private Dictionary> _streamFactory; + public Dictionary> StreamFactory + { + get { return _streamFactory; } + private set { _streamFactory = value; } + } + + public KinectStreamsFactory(KinectManager kinect) + { + _kinectManager = kinect; + // Initialisation de la fabrique avec les fonctions de création pour chaque type de flux. + StreamFactory = new Dictionary> + { + { KinectStreams.Color, () => new ColorImageStream(KinectManager) }, + { KinectStreams.Depth, () => new DepthImageStream(KinectManager) }, + { KinectStreams.IR, () => new InfraredImageStream(KinectManager) } + }; + } + + public KinectStream this[KinectStreams stream] + { + get + { + if (StreamFactory.ContainsKey(stream)) + { + return StreamFactory[stream](); + } + else + { + throw new ArgumentException("Invalid stream type."); + } + } } } } diff --git a/Sources/Lib/Lib.csproj b/Sources/Lib/Lib.csproj index dd258d7..5be8933 100644 --- a/Sources/Lib/Lib.csproj +++ b/Sources/Lib/Lib.csproj @@ -46,9 +46,14 @@ + + + + + diff --git a/Sources/WpfApp/MainWindow.xaml b/Sources/WpfApp/MainWindow.xaml index 5d50914..88530fb 100644 --- a/Sources/WpfApp/MainWindow.xaml +++ b/Sources/WpfApp/MainWindow.xaml @@ -15,20 +15,20 @@ - + -