none
writableBitMap不成功 RRS feed

  • 问题

  • 我在学习writableBitMap的时候,按照《[译]Kinect for Windows SDK开发入门(三):基础知识 下》

    http://www.cnblogs.com/yangecnu/archive/2012/04/02/2429548.html

    进行了代码的修改,但是运行的时候,MainWindow一片空白(之前使用bitmap时都一切正常)。

    希望能帮我看看问题在哪?谢谢

    代码如下

    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 WpfApplication1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            //私有Kinectsensor对象
            private KinectSensor kinect;
    
            private WriteableBitmap colorImageBitmap;
            private Int32Rect colorImageBitmapRect;
            private int colorImageStride;
            //private byte[] colorImagePixelData;
    
            public KinectSensor Kinect
            {
                get { return this.kinect; }
                set
                {
                    //如果带赋值的传感器和目前的不一样
                    if (this.kinect != value)
                    {
                        //如果当前的传感对象不为null
                        if (this.kinect != null)
                        {
                            //uninitailize当前对象
                            this.kinect = null;
                        }
                        //如果传入的对象不为空,且状态为连接状态
                        if (value != null && value.Status == KinectStatus.Connected)
                        {
                            this.kinect = value;
                        }
                    }
                }
            }
    
            public MainWindow()
            {
                InitializeComponent();
                this.Loaded += (s, e) => DiscoverKinectSensor();
                this.Unloaded += (s, e) => this.kinect = null;
            }
    
            private void DiscoverKinectSensor()
            {
                KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;
                this.Kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
            }
    
            private void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
            {
                switch (e.Status)
                {
                    case KinectStatus.Connected:
                        if (this.kinect == null)
                            this.kinect = e.Sensor;
                        break;
                    case KinectStatus.Disconnected:
                        if (this.kinect == e.Sensor)
                        {
                            this.kinect = null;
                            this.kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
                            if (this.kinect == null)
                            {
                                //TODO:通知用于Kinect已拔出
                            }
                        }
                        break;
                    //TODO:处理其他情况下的状态
                }
            }
            private void InitializeKinectSensor(KinectSensor kinectSensor)
            {
                if (kinectSensor != null)
                {
                    ColorImageStream colorStream = kinectSensor.ColorStream;
                    kinectSensor.ColorStream.Enable();
                    this.colorImageBitmap = new WriteableBitmap(colorStream.FrameWidth, colorStream.FrameHeight,
                                                                                                96, 96, PixelFormats.Bgr32, null);
                    this.colorImageBitmapRect = new Int32Rect(0, 0, colorStream.FrameWidth, colorStream.FrameHeight);
                    this.colorImageStride = colorStream.FrameWidth * colorStream.FrameBytesPerPixel;
                    ColorImageElement.Source = this.colorImageBitmap;
                    kinectSensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
                    kinectSensor.Start();
                }
            }
    
            private void UninitializeKinectSensor(KinectSensor kinectSensor)
            {
                if (kinectSensor != null)
                {
                    kinectSensor.Stop();
                    kinectSensor.ColorFrameReady -= new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
                }
            }
            void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
            {
                using (ColorImageFrame frame = e.OpenColorImageFrame())
                {
                    if (frame != null)
                    {
                        byte[] pixelData = new byte[frame.PixelDataLength];
                        frame.CopyPixelDataTo(pixelData);
                        /*for (int i = 0; i < pixelData.Length; i += frame.BytesPerPixel) // inversed color 
                        {
                           pixelData[i]=(byte)~pixelData[i];
                           pixelData[i+1]=(byte)~pixelData[i+1];
                           pixelData[i+2]=(byte)~pixelData[i+2];
                        }*/
                        this.colorImageBitmap.WritePixels(this.colorImageBitmapRect, pixelData, this.colorImageStride, 0);
                    }
                }
            }
        }
    }
    


    2013年12月21日 8:31

全部回复