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.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.Windows.Forms;
namespace WpfApplication1
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
Cl c = new Cl();
public MainWindow()
{
InitializeComponent();
}
//传感器设备
KinectSensor _sensor;
//资源读取
MultiSourceFrameReader _reader;
//骨骼点集合
IList _bodies;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//获取默认设备
_sensor = KinectSensor.GetDefault();
if (_sensor != null)
{
_sensor.Open();
//打开一个新的流读取器
_reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);
//帧变化事件
_reader.MultiSourceFrameArrived += _reader_MultiSourceFrameArrived;
}
}
void _reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
//throw new NotImplementedException();
//从框架参考集中获取框架
var reference = e.FrameReference.AcquireFrame();
//从该资源的颜色框架中获取资源
using (var frame = reference.ColorFrameReference.AcquireFrame())
{
if (frame != null)
{
img.Source = frame.ToBitmap();
}
}
//从该资源的骨骼框架中获取资源
using (var frame = reference.BodyFrameReference.AcquireFrame())
{
if (frame != null)
{
//创建一个骨骼点集合
_bodies = new Body[frame.BodyFrameSource.BodyCount];
//获取更新的主体数据的列表。***********************************
frame.GetAndRefreshBodyData(_bodies);
foreach (var body in _bodies)
{
//判断是否被跟踪
if (body.IsTracked == true)
{
// 获取左手骨骼点 point信息
Joint leftHand = body.Joints[JointType.HandLeft];
//获取左肩骨骼点 point信息
Joint leftShoulder = body.Joints[JointType.ShoulderLeft];
//判断检测状态
if (leftHand.TrackingState == TrackingState.Tracked)
{
//算夹角 以中心法线为标准
double a = c.deg(leftHand, leftShoulder);
leftPx.Text = a.ToString();
if (a < 90)
{
//模拟按键A
System.Windows.Forms.SendKeys.SendWait("{A}");
}
}
}
}
}
}
}
}
}