0

我正在尝试使 KinectColorViewer 与 SDK 1.7 一起使用,但没有成功。只有当我手动将像素从相机复制到图像元素时,我才能显示视频图片。

我有以下 XAML:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:k="http://schemas.microsoft.com/kinect/2013"
  xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" x:Class="KinectD.Camera"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="500"
Title="Camera">

<Grid>
    <k:KinectUserViewer k:KinectRegion.KinectRegion="{Binding ElementName=kinectRegion}" Height="100" HorizontalAlignment="Center" VerticalAlignment="Top" />
    <k:KinectSensorChooserUI HorizontalAlignment="Center" VerticalAlignment="Top" x:Name="sensorChooserUi" />

    <k:KinectRegion x:Name="kinectRegion">
        <Grid>
            <k:KinectCircleButton Label="Menu" HorizontalAlignment="Right" Height="200" VerticalAlignment="Top" Click="MenuButtonOnClick" >
                <StackPanel>
                    <Image Source="Images/smile.png" Height="30"/>
                </StackPanel>
            </k:KinectCircleButton>

        </Grid>
    </k:KinectRegion>
    <WpfViewers:KinectColorViewer HorizontalAlignment="Left" Height="240" Margin="10,10,0,0" VerticalAlignment="Top" Width="320" Kinect="{Binding ElementName=sensorChooserUi, Mode=OneWay, Path=Kinect}"/>


</Grid>

和 XAML.CS:

public partial class Camera : Page
{
    #region "Kinect"
    private KinectSensorChooser sensorChooser;
    #endregion

    public Camera()
    {

        this.InitializeComponent();
        // initialize the sensor chooser and UI
        this.sensorChooser = new KinectSensorChooser();
        //Assign the sensor chooser with the sensor chooser from the mainwindow. 
        //We are reusing the sensorchoosing declared in the first window that can in contact with kinect
        this.sensorChooser = Generics.GlobalKinectSensorChooser;
        //subscribe to the sensorChooserOnKinectChanged event
        this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
        //Assign Kinect Sensorchooser to the sensorchooser we got from our static class
        this.sensorChooserUi.KinectSensorChooser = sensorChooser;
        // Bind the sensor chooser's current sensor to the KinectRegion
        var regionSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
        BindingOperations.SetBinding(this.kinectRegion, KinectRegion.KinectSensorProperty, regionSensorBinding);
    }

    private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args)
    {
        bool error = false;
        if (args.OldSensor != null)
        {
            try
            {
                args.OldSensor.DepthStream.Range = DepthRange.Default;
                args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
                args.OldSensor.DepthStream.Disable();
                args.OldSensor.SkeletonStream.Disable();

                args.OldSensor.ColorStream.Disable();
            }
            catch (InvalidOperationException)
            {
                // KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
                // E.g.: sensor might be abruptly unplugged.
                error = true;
            }
        }

        if (args.NewSensor != null)
        {
            try
            {
                args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                args.NewSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                args.NewSensor.SkeletonStream.Enable();
            }
            catch (InvalidOperationException)
            {
                error = true;
                // KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
                // E.g.: sensor might be abruptly unplugged.
            }
        }

        if (!error)
            kinectRegion.KinectSensor = args.NewSensor;
    }

    private void MenuButtonOnClick(object sender, RoutedEventArgs e)
    {
        //Unsubscribe to the sensorchooser's  event SensorChooseronkinectChanged
        this.sensorChooser.KinectChanged -= SensorChooserOnKinectChanged;
        (Application.Current.MainWindow.FindName("_mainFrame") as Frame).Source = new Uri("MainMenu.xaml", UriKind.Relative);
    }
}

在教程中,我正在关注(http://channel9.msdn.com/Series/KinectQuickstart/Camera-Fundamentals)讲师只是将 KinectColorViewer 放到屏幕上,设置路径并且它正在工作。

4

2 回答 2

1

程序集中的KinectColorViewer可用在KinectWpfViewers“Kinect Explorer”示例中使用,这是了解它的使用和行为方式的最佳位置。从这个示例中,您将发现在 XAML 中初始化查看器的正确方法以及必要的绑定。

从您发布的代码中,您似乎将 Kinect 本身(对硬件的引用)绑定到KinectColorViewer,这不是它所期望的。您需要设置对KinectSensorManager类的引用,该类是KinectWpfViewers程序集的一部分。

这是一个简化的 XAML,带有KinectColorViewer

<Window x:Class="Microsoft.Samples.Kinect.KinectExplorer.KinectWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Microsoft.Samples.Kinect.KinectExplorer"
        xmlns:kt="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers"
        Title="Kinect Explorer" Width="812" Height="768">
    <Grid>
        <kt:KinectColorViewer x:Name="ColorViewer" KinectSensorManager="{Binding KinectSensorManager}" CollectFrameRate="True" RetainImageOnSensorChange="True" />
    </Grid> 
</Window>

然后,您的 XAML.CS 构造函数将类似于:

public KinectWindow()
{
    this.viewModel = new KinectWindowViewModel();

    // The KinectSensorManager class is a wrapper for a KinectSensor that adds
    // state logic and property change/binding/etc support, and is the data model
    // for KinectDiagnosticViewer.
    this.viewModel.KinectSensorManager = new KinectSensorManager();

    Binding sensorBinding = new Binding("KinectSensor");
    sensorBinding.Source = this;
    BindingOperations.SetBinding(this.viewModel.KinectSensorManager, KinectSensorManager.KinectSensorProperty, sensorBinding);

    // Attempt to turn on Skeleton Tracking for each Kinect Sensor
    this.viewModel.KinectSensorManager.SkeletonStreamEnabled = true;

    this.DataContext = this.viewModel;

    InitializeComponent();
}

查看“Kinect Explorer”示例以获取完整的详细信息。

于 2013-04-16T02:22:50.647 回答
0

我在使用 KinectSensorManager 时遇到了同样的问题。事实证明,您需要调用 Microsoft.Samples.Kinect.Wpf.Viewers 而不仅仅是 kinect.dll、kinect.toolkit.dll 和 kinect.controls.dll。但是,当我实现 KinectSensorManager 时,KinectUI 和 SensorChooser 停止在我的 xaml 接口上工作。

于 2013-04-23T21:14:39.137 回答