Browse Source

1.添加若干资源文件;
2.添加主窗口颜色识别逻辑。

Yumin 6 years ago
parent
commit
0e52f09acb

BIN
.vs/KinectCar/v14/.suo


+ 2 - 0
KinectCar.sln.DotSettings.user

@@ -0,0 +1,2 @@
+<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
+	<s:String x:Key="/Default/Environment/Hierarchy/Build/SolBuilderDuo/UseMsbuildSolutionBuilder/@EntryValue">NewVersion</s:String></wpf:ResourceDictionary>

+ 5 - 1
KinectCar/App.xaml

@@ -4,6 +4,10 @@
              xmlns:local="clr-namespace:KinectCar"
              StartupUri="MainWindow.xaml">
     <Application.Resources>
-         
+        <ResourceDictionary>
+            <ResourceDictionary.MergedDictionaries>
+                <ResourceDictionary Source="FontAwesome.xaml"/>
+            </ResourceDictionary.MergedDictionaries>
+        </ResourceDictionary>
     </Application.Resources>
 </Application>

+ 8 - 0
KinectCar/FontAwesome.xaml

@@ -0,0 +1,8 @@
+<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+                    xmlns:local="clr-namespace:KinectCar">
+    <Style x:Key="FontAwesome">
+        <Setter Property="TextElement.FontFamily" Value="/KinectCar;component/Resources/#Font Awesome 5 Free Solid"/>
+        <Setter Property="TextElement.FontSize" Value="12"/>
+    </Style>
+</ResourceDictionary>

BIN
KinectCar/Images/Kinect.ico


BIN
KinectCar/Images/Status.png


+ 17 - 0
KinectCar/KinectCar.csproj

@@ -60,6 +60,13 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
+    <Compile Include="SettingsWindow.xaml.cs">
+      <DependentUpon>SettingsWindow.xaml</DependentUpon>
+    </Compile>
+    <Page Include="FontAwesome.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
     <Page Include="MainWindow.xaml">
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
@@ -72,6 +79,10 @@
       <DependentUpon>MainWindow.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
+    <Page Include="SettingsWindow.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Properties\AssemblyInfo.cs">
@@ -97,10 +108,16 @@
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
     </None>
     <AppDesigner Include="Properties\" />
+    <Resource Include="Resources\fa-solid-900.ttf" />
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />
   </ItemGroup>
+  <ItemGroup />
+  <ItemGroup>
+    <Resource Include="Images\Kinect.ico" />
+    <Resource Include="Images\Status.png" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 14 - 3
KinectCar/MainWindow.xaml

@@ -4,10 +4,21 @@
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
-        Title="MainWindow" Height="400" Width="400" Loaded="Window_Loaded" Closing="Window_Closing">
+        Title="MainWindow" Height="400" Width="400" KeyDown="Window_KeyDown" ResizeMode="CanMinimize" Loaded="Window_Loaded" Closing="Window_Closing">
     <Grid>
-        <Label Name="Status" Content="准备" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
-        <Label Content="{Binding StatusText}" HorizontalAlignment="Left" Margin="10,40,0,0" VerticalAlignment="Top"/>
+        <ToolBar x:Name="ToolBar" VerticalAlignment="Top" Padding="0,0" Margin="0,0" Height="32">
+            <Button x:Name="Settings" Content="&#xF013; 设置" ToolTip="设置" Style="{StaticResource FontAwesome}" Click="Settings_Click" VerticalAlignment="Stretch"/>
+            <Separator/>
+            <Button x:Name="FullScreen" Content="&#xF31E; 全屏" ToolTip="全屏" Style="{StaticResource FontAwesome}" Click="FullScreen_Click" VerticalAlignment="Stretch"/>
+            <Separator/>
+            <Label x:Name="Status" Content="准备" ToolTip="状态" Foreground="Gray"/>
+            <Separator/>
+            <Label x:Name="FileName" Content="" Foreground="Gray"/>
+        </ToolBar>
+        <Label Content="{Binding StatusText}" HorizontalAlignment="Left" Margin="10,67,0,0" VerticalAlignment="Top"/>
 
+        <Viewbox x:Name="ViewBox" Margin="0,32,0,0" HorizontalAlignment="Center">
+            <Image Name="Image" Source="{Binding ImageSource}" Stretch="UniformToFill" />
+        </Viewbox>
     </Grid>
 </Window>

+ 119 - 36
KinectCar/MainWindow.xaml.cs

@@ -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;
     }
 }

BIN
KinectCar/Resources/fa-solid-900.ttf


+ 12 - 0
KinectCar/SettingsWindow.xaml

@@ -0,0 +1,12 @@
+<Window x:Class="KinectCar.SettingsWindow"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:KinectCar"
+        mc:Ignorable="d"
+        Title="设置" Height="300" Width="300" ResizeMode="CanMinimize">
+    <Grid>
+        
+    </Grid>
+</Window>

+ 27 - 0
KinectCar/SettingsWindow.xaml.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+
+namespace KinectCar
+{
+    /// <summary>
+    /// SettingsWindow.xaml 的交互逻辑
+    /// </summary>
+    public partial class SettingsWindow : Window
+    {
+        public SettingsWindow()
+        {
+            InitializeComponent();
+        }
+    }
+}