在C#/ WPF中使用SDK 1.7的一台PC上的两个Kinect

时间:2013-11-06 20:38:05

标签: c# wpf xaml kinect kinect-sdk

我正在尝试从一个C#/ WPF项目中运行两个kinects 。 kinects连接到不同的USB控制器(使用不同的控制器,我并不意味着不同的端口)。

XAML 中,我的代码如下:

<Window x:Class="Microsoft.Samples.Kinect.ColorBasicsind file .MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Color Basics" Height="290" Width="770" Loaded="WindowLoaded" Closing="WindowClosing">
    <Grid>
        <Image Name="Image1" Width="320" Height="240" Margin="10,10,432,458"/>
        <Image Name="Image2" Width="320" Height="240" Margin="432,10,10,63"/>
    </Grid>
</Window>

在C#中,代码隐藏文件如下:

namespace Microsoft.Samples.Kinect.ColorBasics
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Microsoft.Kinect;

    public partial class MainWindow : Window
    {
        private KinectSensor sensor1;
    private KinectSensor sensor2;

    private WriteableBitmap colorBitmap1;
    private WriteableBitmap colorBitmap2;

    private byte[] colorPixels1;
    private byte[] colorPixels2;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        //init sensor 1
        this.sensor1.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
        this.colorPixels1 = new byte[this.sensor1.ColorStream.FramePixelDataLength];
        this.colorBitmap1 = new WriteableBitmap(this.sensor1.ColorStream.FrameWidth, this.sensor1.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
        this.Image1.Source = this.colorBitmap1;
        this.sensor1.ColorFrameReady += this.SensorColorFrameReady1;
        this.sensor1.Start();

        //init sensor 2
        this.sensor2.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
        this.colorPixels2 = new byte[this.sensor2.ColorStream.FramePixelDataLength];
        this.colorBitmap2 = new WriteableBitmap(this.sensor2.ColorStream.FrameWidth, this.sensor2.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
        this.Image2.Source = this.colorBitmap2;
        this.sensor2.ColorFrameReady += this.SensorColorFrameReady2;
        this.sensor2.Start();
    }

    //sensor 1 new-frame-event-handler
    private void SensorColorFrameReady1(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame != null)
            {
                colorFrame.CopyPixelDataTo(this.colorPixels1);
                this.colorBitmap1.WritePixels(
                    new Int32Rect(0, 0, this.colorBitmap1.PixelWidth, this.colorBitmap1.PixelHeight),
                    this.colorPixels1,
                    this.colorBitmap1.PixelWidth * sizeof(int),
                    0);
            }
        }
    }

    //sensor 2 new-frame-event-handler
    private void SensorColorFrameReady2(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame != null)
            {
                colorFrame.CopyPixelDataTo(this.colorPixels2);
                this.colorBitmap2.WritePixels(
                    new Int32Rect(0, 0, this.colorBitmap2.PixelWidth, this.colorBitmap2.PixelHeight),
                    this.colorPixels2,
                    this.colorBitmap2.PixelWidth * sizeof(int),
                    0);
            }
        }
    }

    private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (null != this.sensor1)
        {
            this.sensor1.Stop();
        }
    }
}
}

然而,其中一个 XAML-image-objects 仍为黑色,而另一个则显示不动图片

你能告诉我为什么会这样吗?

(P.S。:请不要深入讨论为什么我发布的代码不是面向对象的!我知道DRY,KISS,Coherence和Cohesion是什么。)

0 个答案:

没有答案