kinect sdk开发入门WPFdemo笔记

    随着2012年2月1号Kinect For Windows SDK v1版本的发布,将会有更多的人投身于Kinect for Windows的开发,本人利用官方提供的开发视频进行编写的第一个WPF demo。步骤如下:

    1.

    2.

     3.

     4.

     5.

    6.

     7.

     8.

      9.

      10.

 

    11.

 

     12.

       13.MainWindow.xaml内容:

<Window x:Class="KinectSdkDemo.MainWindow"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="MainWindow" Height="350" Width="769" Loaded="Window_Loaded" Closing="Window_Closing" xmlns:my="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers">
<Grid Height="381">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="354*" />
<ColumnDefinition Width="393*" />
</Grid.ColumnDefinitions>
<my:KinectSensorChooser HorizontalAlignment="Left" Margin="330,123,0,0" Name="kinectSensorChooser1" VerticalAlignment="Top" Width="328" Grid.ColumnSpan="2" />
<Image Height="240" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="320" />
<my:KinectColorViewer HorizontalAlignment="Right" Name="kinectColorViewer1" VerticalAlignment="Top" Height="240" Width="320" Kinect="{Binding ElementName=kinectSensorChooser1, Path=Kinect}" Grid.Column="1" />
</Grid>
</Window>

      14.后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace KinectSdkDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

// KinectSensor _sensor;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
kinectSensorChooser1.KinectSensorChanged
+= new DependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged);
//if (KinectSensor.KinectSensors.Count > 0)
//{
// _sensor = KinectSensor.KinectSensors[0];
//}
//if (_sensor.Status == KinectStatus.Connected)
//{
// _sensor.ColorStream.Enable();
// _sensor.DepthStream.Enable();
// _sensor.SkeletonStream.Enable();
// _sensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(_sensor_AllFramesReady);
// _sensor.Start();
//}

}

void _sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
/*throw new NotImplementedException();*/
//using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
//{
// if (colorFrame == null)
// {
// return;
// }

// byte[] pixels = new byte[colorFrame.PixelDataLength];
// colorFrame.CopyPixelDataTo(pixels);

// int stride = colorFrame.Width * 4;
// image1.Source =
// BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96
// , PixelFormats.Bgr32, null, pixels, stride);

//}
}

void StopKinect(KinectSensor sensor)
{
if (sensor != null)
{
sensor.Stop();
sensor.AudioSource.Stop();
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
StopKinect(kinectSensorChooser1.Kinect);
}

void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
{
KinectSensor oldSensor = (KinectSensor)e.OldValue;
StopKinect(oldSensor);

KinectSensor newSensor = (KinectSensor)e.NewValue;
newSensor.ColorStream.Enable();
newSensor.DepthStream.Enable();
newSensor.SkeletonStream.Enable();
// newSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(_sensor_AllFramesReady);
try
{
newSensor.Start();
}
catch (System.IO.IOException)
{
kinectSensorChooser1.AppConflictOccurred();
}

}
}
}

     15.运行结果忽略.


转载于:https://www.cnblogs.com/yemeishu/archive/2012/02/02/2336171.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值