pull/9/head
Johan LACHENAL 1 year ago
parent 993306b1c3
commit c643193dd7

@ -1,4 +1,5 @@
using Microsoft.Kinect; using Microsoft.Kinect;
using System.ComponentModel;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
@ -8,9 +9,20 @@ namespace Lib
public class ColorImageStream : KinectStream public class ColorImageStream : KinectStream
{ {
private ColorFrameReader reader; private ColorFrameReader reader;
public ColorImageStream(KinectSensor sensor) : base(sensor) public ColorFrameReader Reader {
get {
return reader;
}
set
{
reader = value;
}
}
public ColorImageStream() : base()
{ {
reader = sensor.ColorFrameSource.OpenReader(); 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_FrameArrived;
} }

@ -6,24 +6,18 @@ namespace Lib
{ {
public class KinectManager : INotifyPropertyChanged public class KinectManager : INotifyPropertyChanged
{ {
private KinectStream currentStream; private KinectSensor sensor;
public KinectStream CurrentStream public KinectSensor Sensor {
{ get {
get { return currentStream; } return sensor;
}
set set
{ {
if (currentStream != value) sensor = value;
{ OnPropertyChanged(nameof(Sensor));
if (currentStream != null)
{
currentStream.Stop();
}
currentStream = value;
currentStream.Start();
OnPropertyChanged(nameof(CurrentStream));
}
} }
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) protected virtual void OnPropertyChanged(string propertyName)
{ {
@ -32,48 +26,38 @@ namespace Lib
public KinectManager() public KinectManager()
{ {
CurrentStream = new ColorImageStream(KinectSensor.GetDefault()); sensor = KinectSensor.GetDefault();
} }
public void ChangeToColorStream()
{
CurrentStream = new ColorImageStream(KinectSensor.GetDefault());
}
//public void ChangeToDepthStream()
//{
// KinectSensor sensor = KinectSensor.GetDefault();
// CurrentStream = new DepthImageStream(sensor);
//}
//public void ChangeToInfraredStream()
//{
// KinectSensor sensor = KinectSensor.GetDefault();
// CurrentStream = new InfraredImageStream(sensor);
//}
public void StartSensor() public void StartSensor()
{ {
currentStream.Start(); if (sensor != null) {
sensor.Open();
}
} }
public void StopSensor() public void StopSensor()
{ {
currentStream.Stop(); if (sensor != null)
{
sensor.Close();
}
} }
public bool Status public bool Status
{ {
get { return currentStream.Sensor != null && currentStream.Sensor.IsAvailable; } get { return Sensor != null && Sensor.IsAvailable; }
} }
public string StatusText public string StatusText
{ {
get get
{ {
if (currentStream.Sensor == null) if (Sensor == null)
{ {
return "Kinect n'est pas connecté"; return "Kinect n'est pas connecté";
} }
else if (!currentStream.Sensor.IsOpen) else if (!Sensor.IsOpen)
{ {
return "Kinect n'est pas ouvert"; return "Kinect n'est pas ouvert";
} }
else if (currentStream.Sensor.IsAvailable) else if (Sensor.IsAvailable)
{ {
return "Kinect est disponible"; return "Kinect est disponible";
} }

@ -12,7 +12,7 @@ namespace Lib
{ {
public abstract class KinectStream : INotifyPropertyChanged public abstract class KinectStream : INotifyPropertyChanged
{ {
public KinectSensor Sensor { get; protected set; } public KinectManager KinectManager { get; private set; }
private WriteableBitmap bitmap; private WriteableBitmap bitmap;
public WriteableBitmap Bitmap public WriteableBitmap Bitmap
{ {
@ -33,26 +33,19 @@ namespace Lib
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }
public KinectStream()
public KinectStream(KinectSensor sensor)
{ {
Sensor = sensor; KinectManager = new KinectManager();
var frameDescription = sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
} }
public virtual void Start() public virtual void Start()
{ {
if (Sensor != null) { KinectManager.StartSensor();
Sensor.Open();
}
} }
public virtual void Stop() public virtual void Stop()
{ {
if (Sensor != null) { KinectManager.StopSensor();
Sensor.Close();
}
} }
} }
} }

@ -26,6 +26,6 @@
<Button Content="Filtre 5" Margin="5"/> <Button Content="Filtre 5" Margin="5"/>
</StackPanel> </StackPanel>
<Image Grid.Row="2" Source="{Binding kinectManager.CurrentStream.BitMap}" /> <Image Grid.Row="2" Source="{Binding KinectStream.BitMap}" />
</Grid> </Grid>
</Window> </Window>

@ -23,29 +23,26 @@ namespace WpfApp
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private KinectManager _kinectManager; private KinectStream _kinectStream;
public KinectManager kinectManager public KinectStream KinectStream
{ {
get { return _kinectManager; } get { return _kinectStream; }
set { _kinectManager = value; } set { _kinectStream = value; }
} }
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
kinectManager = new KinectManager(); this.DataContext = this;
Debug.WriteLine(kinectManager.StatusText); KinectStream = new ColorImageStream();
kinectManager.ChangeToColorStream(); Debug.WriteLine(KinectStream.KinectManager.StatusText);
Debug.WriteLine(kinectManager.StatusText); KinectStream.Start();
kinectManager.StartSensor(); Debug.WriteLine(KinectStream.KinectManager.StatusText);
Debug.WriteLine(kinectManager.CurrentStream.Bitmap);
Debug.WriteLine(kinectManager.StatusText);
DataContext = this;
} }
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ {
Debug.WriteLine(kinectManager.StatusText); Debug.WriteLine(KinectStream.KinectManager.StatusText);
kinectManager.StopSensor(); KinectStream.Stop();
} }
} }
} }

Loading…
Cancel
Save