|
@@ -1,4 +1,8 @@
|
|
|
-namespace KinectCar
|
|
|
+using System.Windows.Input;
|
|
|
+using System.Windows.Media;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
+
|
|
|
+namespace KinectCar
|
|
|
{
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
@@ -21,6 +25,8 @@
|
|
|
private CoordinateMapper coordinateMapper = null;
|
|
|
// 身体框架渲染器
|
|
|
private BodyFrameReader bodyFrameReader = null;
|
|
|
+ // 颜色框架渲染器
|
|
|
+ private ColorFrameReader colorFrameReader = null;
|
|
|
// 身体的数组
|
|
|
private Body[] _bodies = null;
|
|
|
// 骨骼的定义
|
|
@@ -33,6 +39,8 @@
|
|
|
private const float Threshold = 0.15f;
|
|
|
// 当前状态
|
|
|
private int _gesture = -1;
|
|
|
+ // Bitmap to display
|
|
|
+ private WriteableBitmap _colorBitmap = null;
|
|
|
|
|
|
private static string MQTT_BROKER_ADDRESS = "123.207.151.92";
|
|
|
// 创建客户端实例
|
|
@@ -70,43 +78,48 @@
|
|
|
// 获取坐标映射器
|
|
|
coordinateMapper = _kinectSensor.CoordinateMapper;
|
|
|
// 获取深度(显示)范围
|
|
|
- FrameDescription frameDescription = _kinectSensor.DepthFrameSource.FrameDescription;
|
|
|
+ var frameDescription = _kinectSensor.DepthFrameSource.FrameDescription;
|
|
|
// 得到关节空间的大小
|
|
|
displayWidth = frameDescription.Width;
|
|
|
displayHeight = frameDescription.Height;
|
|
|
- // 打开渲染器的身体框架
|
|
|
+ // 打开身体框架的渲染器
|
|
|
bodyFrameReader = _kinectSensor.BodyFrameSource.OpenReader();
|
|
|
+ // 打开颜色框架的渲染器
|
|
|
+ colorFrameReader = _kinectSensor.ColorFrameSource.OpenReader();
|
|
|
// 骨骼定义为两个关节之间的线
|
|
|
- _bones = new List<Tuple<JointType, JointType>>();
|
|
|
- // 躯干
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.Head, JointType.Neck));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.Neck, JointType.SpineShoulder));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.SpineMid));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.SpineMid, JointType.SpineBase));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderRight));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderLeft));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipRight));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipLeft));
|
|
|
- // 右臂
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderRight, JointType.ElbowRight));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.ElbowRight, JointType.WristRight));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.HandRight));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.HandRight, JointType.HandTipRight));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.ThumbRight));
|
|
|
- // 左臂
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderLeft, JointType.ElbowLeft));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.ElbowLeft, JointType.WristLeft));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.HandLeft));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.HandLeft, JointType.HandTipLeft));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.ThumbLeft));
|
|
|
- // 右腿
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.HipRight, JointType.KneeRight));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.KneeRight, JointType.AnkleRight));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.AnkleRight, JointType.FootRight));
|
|
|
- // 左腿
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.HipLeft, JointType.KneeLeft));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.KneeLeft, JointType.AnkleLeft));
|
|
|
- _bones.Add(new Tuple<JointType, JointType>(JointType.AnkleLeft, JointType.FootLeft));
|
|
|
+ _bones = new List<Tuple<JointType, JointType>>
|
|
|
+ {
|
|
|
+ // 躯干
|
|
|
+ new Tuple<JointType, JointType>(JointType.Head, JointType.Neck),
|
|
|
+ new Tuple<JointType, JointType>(JointType.Neck, JointType.SpineShoulder),
|
|
|
+ new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.SpineMid),
|
|
|
+ new Tuple<JointType, JointType>(JointType.SpineMid, JointType.SpineBase),
|
|
|
+ new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderRight),
|
|
|
+ new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderLeft),
|
|
|
+ new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipRight),
|
|
|
+ new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipLeft),
|
|
|
+ // 右臂
|
|
|
+ new Tuple<JointType, JointType>(JointType.ShoulderRight, JointType.ElbowRight),
|
|
|
+ new Tuple<JointType, JointType>(JointType.ElbowRight, JointType.WristRight),
|
|
|
+ new Tuple<JointType, JointType>(JointType.WristRight, JointType.HandRight),
|
|
|
+ new Tuple<JointType, JointType>(JointType.HandRight, JointType.HandTipRight),
|
|
|
+ new Tuple<JointType, JointType>(JointType.WristRight, JointType.ThumbRight),
|
|
|
+ // 左臂
|
|
|
+ new Tuple<JointType, JointType>(JointType.ShoulderLeft, JointType.ElbowLeft),
|
|
|
+ new Tuple<JointType, JointType>(JointType.ElbowLeft, JointType.WristLeft),
|
|
|
+ new Tuple<JointType, JointType>(JointType.WristLeft, JointType.HandLeft),
|
|
|
+ new Tuple<JointType, JointType>(JointType.HandLeft, JointType.HandTipLeft),
|
|
|
+ new Tuple<JointType, JointType>(JointType.WristLeft, JointType.ThumbLeft),
|
|
|
+ // 右腿
|
|
|
+ new Tuple<JointType, JointType>(JointType.HipRight, JointType.KneeRight),
|
|
|
+ new Tuple<JointType, JointType>(JointType.KneeRight, JointType.AnkleRight),
|
|
|
+ new Tuple<JointType, JointType>(JointType.AnkleRight, JointType.FootRight),
|
|
|
+ // 左腿
|
|
|
+ new Tuple<JointType, JointType>(JointType.HipLeft, JointType.KneeLeft),
|
|
|
+ new Tuple<JointType, JointType>(JointType.KneeLeft, JointType.AnkleLeft),
|
|
|
+ new Tuple<JointType, JointType>(JointType.AnkleLeft, JointType.FootLeft)
|
|
|
+ };
|
|
|
+
|
|
|
// 设置 IsAvailableChanged 事件通知程序
|
|
|
_kinectSensor.IsAvailableChanged += Sensor_IsAvailableChanged;
|
|
|
// 打开传感器
|
|
@@ -125,8 +138,17 @@
|
|
|
{
|
|
|
bodyFrameReader.FrameArrived += Reader_FrameArrived;
|
|
|
}
|
|
|
+ if (colorFrameReader != null)
|
|
|
+ {
|
|
|
+ colorFrameReader.FrameArrived += Reader_ColorFrameArrived;
|
|
|
+ }
|
|
|
+ // create the colorFrameDescription from the ColorFrameSource using Bgra format
|
|
|
+ var colorFrameDescription = _kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
|
|
|
+ // create the bitmap to display
|
|
|
+ _colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
|
|
|
+
|
|
|
// 注册消息接收处理事件,还可以注册消息订阅成功、取消订阅成功、与服务器断开等事件处理函数
|
|
|
- _client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
|
|
|
+ _client.MqttMsgPublishReceived += ClientMqttMsgPublishReceived;
|
|
|
//生成客户端 ID 并连接服务器
|
|
|
string clientId = Guid.NewGuid().ToString();
|
|
|
_client.Connect(clientId, "yumin", "minbb.cn");
|
|
@@ -144,6 +166,12 @@
|
|
|
bodyFrameReader = null;
|
|
|
}
|
|
|
|
|
|
+ if (colorFrameReader != null)
|
|
|
+ {
|
|
|
+ colorFrameReader.Dispose();
|
|
|
+ colorFrameReader = null;
|
|
|
+ }
|
|
|
+
|
|
|
if (_kinectSensor != null)
|
|
|
{
|
|
|
_kinectSensor.Close();
|
|
@@ -153,10 +181,10 @@
|
|
|
_client?.Disconnect();
|
|
|
}
|
|
|
|
|
|
- void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
|
|
|
+ private void ClientMqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
|
|
|
{
|
|
|
//处理接收到的消息
|
|
|
- string msg = System.Text.Encoding.Default.GetString(e.Message);
|
|
|
+ string msg = Encoding.Default.GetString(e.Message);
|
|
|
Console.WriteLine("收到消息 = " + msg + "\r\n");
|
|
|
}
|
|
|
|
|
@@ -296,10 +324,65 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
|
|
|
+ {
|
|
|
+ // ColorFrame is IDisposable
|
|
|
+ using (var colorFrame = e.FrameReference.AcquireFrame())
|
|
|
+ {
|
|
|
+ if (colorFrame != null)
|
|
|
+ {
|
|
|
+ var colorFrameDescription = colorFrame.FrameDescription;
|
|
|
+ using (var 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));
|
|
|
+ Console.WriteLine(Image.Source == null);
|
|
|
+ }
|
|
|
+ _colorBitmap.Unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e)
|
|
|
{
|
|
|
// 如果失败,设置状态文本
|
|
|
StatusText = _kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText : Properties.Resources.SensorNotAvailableStatusText;
|
|
|
}
|
|
|
+
|
|
|
+ private void Window_KeyDown(object sender, KeyEventArgs e)
|
|
|
+ {
|
|
|
+ switch (e.Key)
|
|
|
+ {
|
|
|
+ case Key.Escape:
|
|
|
+ ToolBar.Visibility = Visibility.Visible;
|
|
|
+ ViewBox.Margin = new Thickness { Left = 0, Top = 32, Right = 0, Bottom = 0 };
|
|
|
+ WindowState = WindowState.Normal;
|
|
|
+ WindowStyle = WindowStyle.SingleBorderWindow;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Settings_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ new SettingsWindow().ShowDialog();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void FullScreen_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ ToolBar.Visibility = Visibility.Hidden;
|
|
|
+ ViewBox.Margin = new Thickness { Left = 0, Top = 0, Right = 0, Bottom = 0 };
|
|
|
+ this.Topmost = false;
|
|
|
+ this.WindowStyle = WindowStyle.None;
|
|
|
+ this.WindowState = WindowState.Maximized;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ImageSource ImageSource => _colorBitmap;
|
|
|
}
|
|
|
}
|