예제 #1
0
    private void MultiFrameArrived(object sender, MultiSourceFrameArrivedEventArgs args)
    {
        bool             dataReceived     = false;
        MultiSourceFrame multiSourceFrame = args.FrameReference.AcquireFrame();

        using (DepthFrame depthFrame = multiSourceFrame.DepthFrameReference.AcquireFrame()) {
            if (depthFrame != null)
            {
                var pDepthData = GCHandle.Alloc(pDepthBuffer, GCHandleType.Pinned);
                depthFrame.CopyFrameDataToIntPtr(pDepthData.AddrOfPinnedObject(), (uint)pDepthBuffer.Length * sizeof(ushort));
                pDepthData.Free();
                dataReceived = true;
            }
        }

        using (ColorFrame colorFrame = multiSourceFrame.ColorFrameReference.AcquireFrame()) {
            if (colorFrame != null)
            {
                var pColorData = GCHandle.Alloc(pColorBuffer, GCHandleType.Pinned);
                colorFrame.CopyConvertedFrameDataToIntPtr(pColorData.AddrOfPinnedObject(), (uint)pColorBuffer.Length, ColorImageFormat.Bgra);
                pColorData.Free();
                dataReceived = true;
            }
        }

        using (BodyIndexFrame bodyIndexFrame = multiSourceFrame.BodyIndexFrameReference.AcquireFrame()) {
            if (bodyIndexFrame != null)
            {
                var pBodyIndexData = GCHandle.Alloc(pBodyIndexBuffer, GCHandleType.Pinned);
                bodyIndexFrame.CopyFrameDataToIntPtr(pBodyIndexData.AddrOfPinnedObject(), (uint)pBodyIndexBuffer.Length);
                pBodyIndexData.Free();
                dataReceived = true;
            }
        }

        using (BodyFrame bodyFrame = multiSourceFrame.BodyFrameReference.AcquireFrame()) {
            if (bodyFrame != null)
            {
                if (pBodyData == null)
                {
                    pBodyData = new Body[bodyFrame.BodyCount];
                }
                bodyFrame.GetAndRefreshBodyData(pBodyData);
                dataReceived = true;
            }
        }

        if (dataReceived)
        {
//////// FPS - BEGIN
            frameCount++;
//////// FPS - END
            ProcessFrame();
        }
    }
예제 #2
0
        private unsafe void FrameReader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            _Stopwatch.Restart();
            MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame();

            if (multiSourceFrame != null)
            {
                ColorFrame     colorFrame     = null;
                DepthFrame     depthFrame     = null;
                BodyFrame      bodyFrame      = null;
                BodyIndexFrame bodyIndexFrame = null;

                try
                {
                    bool allRequiredDataReceived = true;

                    if (_FrameSourceTypes.HasFlag(FrameSourceTypes.Color))
                    {
                        colorFrame = multiSourceFrame.ColorFrameReference.AcquireFrame();
                        if (colorFrame != null)
                        {
                            fixed(byte *colorBytesPointer = ColorPixels)
                            {
                                IntPtr colorPtr = (IntPtr)colorBytesPointer;
                                uint   size     = (uint)(_ColorFrameDescription.Width * _ColorFrameDescription.Height * _ColorFrameDescription.BytesPerPixel);

                                if (colorFrame.RawColorImageFormat == ImageFormat)
                                {
                                    colorFrame.CopyRawFrameDataToIntPtr(colorPtr, size);
                                }
                                else
                                {
                                    colorFrame.CopyConvertedFrameDataToIntPtr(colorPtr, size, ImageFormat);
                                }
                            }
                        }
                        else
                        {
                            allRequiredDataReceived = false;
                        }
                    }


                    if (_FrameSourceTypes.HasFlag(FrameSourceTypes.Depth) && allRequiredDataReceived)
                    {
                        depthFrame = multiSourceFrame.DepthFrameReference.AcquireFrame();
                        if (depthFrame != null)
                        {
                            fixed(ushort *depthBytesPointer = DepthPixels)
                            {
                                IntPtr depthPtr = (IntPtr)depthBytesPointer;

                                depthFrame.CopyFrameDataToIntPtr(depthPtr, (uint)(_DepthFrameDescription.Width * _DepthFrameDescription.Height * _DepthFrameDescription.BytesPerPixel));
                            }
                        }
                        else
                        {
                            allRequiredDataReceived = false;
                        }
                    }


                    if (_FrameSourceTypes.HasFlag(FrameSourceTypes.Body) && allRequiredDataReceived)
                    {
                        bodyFrame = multiSourceFrame.BodyFrameReference.AcquireFrame();
                        if (bodyFrame != null)
                        {
                            bodyFrame.GetAndRefreshBodyData(Bodies);
                        }
                        else
                        {
                            allRequiredDataReceived = false;
                        }
                    }


                    if (_FrameSourceTypes.HasFlag(FrameSourceTypes.BodyIndex) && allRequiredDataReceived)
                    {
                        bodyIndexFrame = multiSourceFrame.BodyIndexFrameReference.AcquireFrame();
                        if (bodyIndexFrame != null)
                        {
                            fixed(byte *bodyIndexBytesPointer = BodyIndexPixels)
                            {
                                IntPtr bodyIndexPtr = (IntPtr)bodyIndexBytesPointer;

                                bodyIndexFrame.CopyFrameDataToIntPtr(bodyIndexPtr, (uint)(_BodyIndexFrameDescription.Width * _BodyIndexFrameDescription.Height * _BodyIndexFrameDescription.BytesPerPixel));
                            }
                        }
                        else
                        {
                            allRequiredDataReceived = false;
                        }
                    }

                    if (allRequiredDataReceived)
                    {
                        _KinectFrameArrivedEventArgs.ColorPixels     = ColorPixels;
                        _KinectFrameArrivedEventArgs.DepthPixels     = DepthPixels;
                        _KinectFrameArrivedEventArgs.Bodies          = Bodies;
                        _KinectFrameArrivedEventArgs.BodyIndexPixels = BodyIndexPixels;
                        _KinectFrameArrivedEventArgs.KinectSensor    = multiSourceFrame.KinectSensor;
                        _KinectFrameArrivedEventArgs.FrameNumber     = _FrameNumber;

                        EventHandler <KinectFrameArrivedEventArgs> handler = FrameArrived;
                        if (handler != null)
                        {
                            handler(this, _KinectFrameArrivedEventArgs);
                        }
                    }
                }
                finally
                {
                    if (colorFrame != null)
                    {
                        colorFrame.Dispose();
                    }

                    if (depthFrame != null)
                    {
                        depthFrame.Dispose();
                    }

                    if (bodyFrame != null)
                    {
                        bodyFrame.Dispose();
                    }

                    if (bodyIndexFrame != null)
                    {
                        bodyIndexFrame.Dispose();
                    }
                }
            }

            _Stopwatch.Stop();
            RaiseKinectFrameComplete(_Stopwatch.Elapsed);
            _FrameNumber++;
        }