using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Coding4Fun.Kinect.Wpf;
using Coding4Fun.Kinect.Wpf.Controls;
using Microsoft.Kinect;
namespace KinectHubDemo
{
public partial class MainWindow : Window
{
KinectSensor kinect;
private List
s).OrderBy(s => s.Joints[JointType.Head].Position.Z)
.FirstOrDefault();
if (closestSkeleton == null)
return;
if (closestSkeleton.TrackingState != SkeletonTrackingState.Tracked)
return;
var joints = closestSkeleton.Joints;
Joint rightHand = joints[JointType.HandRight];
Joint leftHand = joints[JointType.HandLeft];
//通过Y轴坐标判断是左手习惯还是右手习惯:举起的那支手的Y轴坐标值更大
var hand = (rightHand.Position.Y > leftHand.Position.Y)
? rightHand
: leftHand;
if (hand.TrackingState != JointTrackingState.Tracked)
return;
//获得屏幕的宽度和高度
int screenWidth = (int)SystemParameters.PrimaryScreenWidth;
int screenHeight = (int)SystemParameters.PrimaryScreenHeight;
//将部位“手”的骨骼坐标映射为屏幕坐标;手只需要在有限范围内移动即可覆盖整个屏幕区域
float posX = hand.ScaleTo(screenWidth, screenHeight, 0.2f, 0.2f).Position.X;
float posY = hand.ScaleTo(screenWidth, screenHeight, 0.2f, 0.2f).Position.Y;
//判断是否悬浮在图片按钮上,有则触发Click事件
OnButtonLocationChanged(kinectButton, buttons, (int)posX, (int)posY);
}
}
void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame == null)
{
return;
}
//屏幕右下角显示彩色摄像,使用Coding4Fun.Kinect.Wpf的扩展方法
videoImage.Source = colorFrame.ToBitmapSource();
}
}
public MainWindow()
{
InitializeComponent();
kinectButton.Click += new RoutedEventHandler(kinectButton_Clicked);
}
private void InitializeButtons()
{
buttons = new List
hand.Release();
//移动手型光标
Canvas.SetLeft(hand, X - (hand.ActualWidth / 2));
Canvas.SetTop(hand, Y - (hand.ActualHeight / 2));
}
private void kinectButton_Clicked(object sender, RoutedEventArgs e)
{
hoveredButton.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, hoveredButton));
}
public bool IsButtonOverObject(FrameworkElement hand, List
{
if (isWindowsClosing || !Window.GetWindow(hand).IsActive)
return false;
// 找到悬浮手型控件的中心点位置
var handTopLeft = new Point(Canvas.GetTop(hand), Canvas.GetLeft(hand));
double handLeft = handTopLeft.X + (hand.ActualWidth / 2);
double handTop = handTopLeft.Y + (hand.ActualHeight / 2);
//遍历图片按钮,判断Hand图标是否悬浮在其中之一
foreach (Button target in buttons)
{
Point targetTopLeft = target.PointToScreen(new Point());
if (handTop > targetTopLeft.X
&& handTop < targetTopLeft.X + target.ActualWidth
&& handLeft > targetTopLeft.Y
&& handLeft < targetTopLeft.Y + target.ActualHeight)
{
hoveredButton = target;
return true;
}
}
return false;
}
private void promoteButtonClickEvent(string info)
{
listBoxHoverEvent.Items.Add(string.Format("{0} : {1}", info, DateTime.Now.ToString("t")));
}
private void button1_Click(object sender, RoutedEventArgs e)
{
promoteButtonClickEvent("Button 1 Clicked");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
promoteButtonClickEvent("Button 2 Clicked");
}
private void button3_Click(object sender, RoutedEventArgs e)
{
promoteButtonClickEvent("Button 3 Clicked");
}
private void button4_Click(object sender, RoutedEventArgs e)
{
promoteButtonClickEvent("Button 4 Clicked");
}
private void button5_Click(object sender, RoutedEventArgs e)
{
promoteButtonClickEvent("Button 5 Clicked");
}
private void button6_Click(object sender, RoutedEventArgs e)
{
promoteButtonClickEvent("Button 6 Clicked");
}
private void Canvas_Loaded(object sender, RoutedEventArgs e)
{
}
private void stopKinect()
{
if (kinect != null)
{
if (kinect.Status == KinectStatus.Connected)
{
//关闭Kinect设备
kinect.Stop();
}
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitializeButtons();
startKinect();
}
private void Window_Closing(object sender, https://www.doczj.com/doc/0117647527.html,ponentModel.CancelEventArgs e)
{
isWindowsClosing = true;
stopKinect();
}
}
}
//界面::::
WindowStyle="SingleBorderWindow"
xmlns="https://www.doczj.com/doc/0117647527.html,/winfx/2006/xaml/presentation"
xmlns:x="https://www.doczj.com/doc/0117647527.html,/winfx/2006/xaml"
Background="#1E1C37"
xmlns:Controls="clr-namespace:Coding4Fun.Kinect.Wpf.Controls;assembly=Coding4Fun.Kinect.Wpf"
Title="Kinect Hub Demo" Height="800" Width="1280" Loaded="Window_Loaded" Closing="Window_Closing">