|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.Kinect;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
@ -20,9 +21,76 @@ namespace WpfApp
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
{
|
|
|
|
|
private KinectSensor kinectSensor = null;
|
|
|
|
|
private ColorFrameReader colorFrameReader = null;
|
|
|
|
|
private WriteableBitmap colorBitmap = null;
|
|
|
|
|
|
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
// Initialiser la Kinect
|
|
|
|
|
this.kinectSensor = KinectSensor.GetDefault();
|
|
|
|
|
|
|
|
|
|
// Ouvrir le lecteur de flux de couleur
|
|
|
|
|
this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader();
|
|
|
|
|
|
|
|
|
|
// Frame description pour les images de couleur
|
|
|
|
|
FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
|
|
|
|
|
|
|
|
|
|
// Créer le bitmap pour afficher l'image
|
|
|
|
|
this.colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
|
|
|
|
|
|
|
|
|
|
// Gérer l'événement FrameArrived pour le flux de couleur
|
|
|
|
|
this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived;
|
|
|
|
|
|
|
|
|
|
// Ouvrir la Kinect
|
|
|
|
|
this.kinectSensor.Open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
|
|
|
|
|
{
|
|
|
|
|
if (colorFrame != null)
|
|
|
|
|
{
|
|
|
|
|
FrameDescription colorFrameDescription = colorFrame.FrameDescription;
|
|
|
|
|
|
|
|
|
|
using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
|
|
|
|
|
{
|
|
|
|
|
this.colorBitmap.Lock();
|
|
|
|
|
|
|
|
|
|
// Vérifier si la taille de l'image a changé
|
|
|
|
|
if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight))
|
|
|
|
|
{
|
|
|
|
|
colorFrame.CopyConvertedFrameDataToIntPtr(
|
|
|
|
|
this.colorBitmap.BackBuffer,
|
|
|
|
|
(uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4),
|
|
|
|
|
ColorImageFormat.Bgra);
|
|
|
|
|
|
|
|
|
|
this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.colorBitmap.Unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assurez-vous de fermer correctement le lecteur et le capteur Kinect lors de la fermeture de la fenêtre
|
|
|
|
|
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (this.colorFrameReader != null)
|
|
|
|
|
{
|
|
|
|
|
this.colorFrameReader.Dispose();
|
|
|
|
|
this.colorFrameReader = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.kinectSensor != null)
|
|
|
|
|
{
|
|
|
|
|
this.kinectSensor.Close();
|
|
|
|
|
this.kinectSensor = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|