-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorDriver.cs
102 lines (85 loc) · 3.55 KB
/
ColorDriver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
namespace Ostsoft.Games.SuperKinectroid
{
public class ColorDriver : IDisposable
{
/// <summary>
/// Active Kinect sensor
/// </summary>
private KinectSensor kinectSensor = null;
/// <summary>
/// Width of display (depth space)
/// </summary>
private int displayWidth;
/// <summary>
/// Height of display (depth space)
/// </summary>
private int displayHeight;
/// <summary>
/// Reader for color frames
/// </summary>
private ColorFrameReader colorFrameReader = null;
/// <summary>
/// Bitmap to display
/// </summary>
private WriteableBitmap colorBitmap = null;
public ColorDriver(KinectSensor kinectSensor, WriteableBitmap colorBitmap)
{
this.kinectSensor = kinectSensor;
var colorFrameDescription = kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
this.colorBitmap = colorBitmap;
// get size of joint space
displayWidth = colorFrameDescription.Width;
displayHeight = colorFrameDescription.Height;
colorFrameReader = kinectSensor.ColorFrameSource.OpenReader();
colorFrameReader.FrameArrived += Reader_ColorFrameArrived;
}
public ImageSource ColorBitmap => colorBitmap;
/// <summary>
/// Handles the color frame data arriving from the sensor
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
// ColorFrame is IDisposable
using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
{
if (colorFrame != null)
{
FrameDescription colorFrameDescription = colorFrame.FrameDescription;
using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
{
colorBitmap.Lock();
// verify data and write the new color frame data to the display bitmap
if ((colorFrameDescription.Width == colorBitmap.PixelWidth) &&
(colorFrameDescription.Height == colorBitmap.PixelHeight))
{
colorFrame.CopyConvertedFrameDataToIntPtr(
colorBitmap.BackBuffer,
(uint) (colorFrameDescription.Width * colorFrameDescription.Height * 4),
ColorImageFormat.Bgra);
colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth,
colorBitmap.PixelHeight));
}
colorBitmap.Unlock();
}
}
}
}
public void Dispose()
{
colorFrameReader?.Dispose();
}
public static WriteableBitmap GetBitmap(KinectSensor kinectSensor)
{
var colorFrameDescription = kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
return new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0,
PixelFormats.Bgr32, null);
}
}
}