关于使用kinect播放PPT

来源:互联网 发布:幕墙易云计算手机 编辑:程序博客网 时间:2024/04/30 18:36

最近也找了不少资料,编写了一个使用kinect来播放ppt的程序,基于c#,不足之处大家一起交流

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 OFFICECORE = Microsoft.Office.Core;
using POWERPOINT = Microsoft.Office.Interop.PowerPoint;
using System.IO;
using System.Windows.Forms;
using Microsoft.Kinect;
using System.Diagnostics;
using System.ComponentModel;
using System.Globalization;


namespace WpfApplication3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>

public partial class MainWindow : Window, INotifyPropertyChanged
{
POWERPOINT.Application objApp = null;
POWERPOINT.Presentation objPresSet = null;
//POWERPOINT.SlideShowWindows objSSWs;
//POWERPOINT.SlideShowTransition objSST;
POWERPOINT.SlideShowSettings objSSS;
//POWERPOINT.SlideRange objSldRng;
KinectSensor kinectSensor = null;
private BodyFrameReader bodyFrameReader = null;
private Body[] bodies = null;
public MainWindow()
{
InitializeComponent();
this.kinectSensor = KinectSensor.GetDefault();
this.kinectSensor.Open();
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
if (bodyFrameReader != null)
{
bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived;
textbox1.Text = "读取骨架数据流1";
}
this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged;
}



private void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{

bool data = false;
using (BodyFrame bodyframe = e.FrameReference.AcquireFrame())
{
if (bodyframe != null)
{
if (this.bodies == null)
{
this.bodies = new Body[bodyframe.BodyCount];
}
bodyframe.GetAndRefreshBodyData(this.bodies);
data = true;
}
}
if (data)
{

foreach (Body body in bodies)
{
if (body.IsTracked)
{
IReadOnlyDictionary<JointType, Joint> joints = body.Joints;
CameraSpacePoint righthand = joints[JointType.HandRight].Position;
//右手的位置获取
CameraSpacePoint righthip = joints[JointType.HipRight].Position;
//右臀位置获获取
CameraSpacePoint lefthand = joints[JointType.HandLeft].Position;
//右手的位置获取
CameraSpacePoint leftthip = joints[JointType.HipLeft].Position;
//右臀位置获获取
textbox1.Text = "读取骨架数据流2";
if (righthand.X - righthip.X > 0.45)
{
System.Windows.Forms.SendKeys.SendWait("{PGDN}");
System.Threading.Thread.Sleep(1000);
textbox1.Text = "右手";
}
if (leftthip.X - lefthand.X > 0.45)
{
System.Windows.Forms.SendKeys.SendWait("{PGUP}");
System.Threading.Thread.Sleep(1000);
textbox1.Text = "左手";
}
/* if (lefthand.X - righthand.X <0.2)
{
System.Windows.Forms.SendKeys.SendWait("{END}");
System.Threading.Thread.Sleep(1000);
textbox1.Text = "结束";
}*/
}
}
}
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
objApp = new POWERPOINT.Application();
objPresSet = objApp.Presentations.Open(@"C:\Users\xiong\Desktop\text.ppt", OFFICECORE.MsoTriState.msoCTrue, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
objSSS = this.objPresSet.SlideShowSettings;
objSSS.Run();
textbox1.Text = "hellow";
openfile();//用于测试是否正常运行
}
private void openfile()
{
if (File.Exists(@"C:\Users\xiong\Desktop\新建文本文档.txt"))
{
FileStream TextFile = File.Open(@"C:\Users\xiong\Desktop\新建文本文档.txt", FileMode.Append);
byte[] Info = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o', (byte)'w' };
TextFile.Write(Info, 0, Info.Length);
TextFile.Close();
textbox2.Text = "打开了!";
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e)
{
textbox2.Text = "连接成功";
}
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
if (this.bodyFrameReader != null)
{
// BodyFrameReader is IDisposable
this.bodyFrameReader.Dispose();
this.bodyFrameReader = null;
}

if (this.kinectSensor != null)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
}
}
我把PPT放在了桌面上,并且命名为text,大家可以自己改一下名称就可以啦,另外的新建文件夹是将Hellow以不打开txt的形式写入

原创粉丝点击