コード例 #1
0
 /// <summary>
 /// 新しいフレームを取得したときのハンドラ
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 private void HandleFrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
 {
     // プラグイン外からイベントハンドラが設定されていない場合は何もしない
     if (OnFrameArrived == null)
     {
         return;
     }
     // 最新のフレームを取得
     using (var frame = _frameReader.TryAcquireLatestFrame())
     {
         if (frame != null)
         {
             // SoftwareBitmapとして保持し、サブスクライバにはBitmapのサイズを通知
             _bitmap = frame.VideoMediaFrame.SoftwareBitmap;
             OnFrameArrived?.Invoke(4 * _bitmap.PixelHeight * _bitmap.PixelWidth);
         }
     }
 }
コード例 #2
0
        void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            byte[] _pixelsFrame = null;

            var reference = e.FrameReference.AcquireFrame();

            if (this.frameSourceType == FrameSourceTypes.Color)
            {
                using (var frame = reference.ColorFrameReference.AcquireFrame())
                {
                    if (frame != null)
                    {
                        if (frame.RawColorImageFormat == ColorImageFormat.Bgra)
                        {
                            frame.CopyRawFrameDataToArray(_pixelsColor);
                        }
                        else
                        {
                            frame.CopyConvertedFrameDataToArray(_pixelsColor, ColorImageFormat.Bgra);
                        }
                        _pixelsFrame = _pixelsColor;
                    }
                }
            }
            else if (this.frameSourceType == FrameSourceTypes.Infrared)
            {
                using (var frame = reference.InfraredFrameReference.AcquireFrame())
                {
                    if (frame != null)
                    {
                        using (KinectBuffer infrBuffer = frame.LockImageBuffer())
                        {
                            if ((infraredFrameDescription.Width * infraredFrameDescription.Height) == (infrBuffer.Size / infraredFrameDescription.BytesPerPixel))
                            {
                                this.ProcessInfraredFrameData(ref _pixelsInfra, infrBuffer.UnderlyingBuffer, infrBuffer.Size);
                            }
                        }
                        _pixelsFrame = _pixelsInfra;
                    }
                }
            }
            else if (this.frameSourceType == FrameSourceTypes.Depth)
            {
                using (var frame = reference.DepthFrameReference.AcquireFrame())
                {
                    if (frame != null)
                    {
                        using (KinectBuffer depthBuffer = frame.LockImageBuffer())
                        {
                            if ((depthFrameDescription.Width * depthFrameDescription.Height) == (depthBuffer.Size / depthFrameDescription.BytesPerPixel))
                            {
                                ushort maxDepth = ushort.MaxValue;

                                ProcessDepthFrameData(ref _pixelsDepth, depthBuffer.UnderlyingBuffer, depthBuffer.Size, frame.DepthMinReliableDistance, maxDepth);
                            }
                        }
                        _pixelsFrame = _pixelsDepth;
                    }
                }
            }

            using (var frame = reference.BodyFrameReference.AcquireFrame())
            {
                if (frame != null)
                {
                    _bodies = new Body[bodyCountTotal];
                    frame.GetAndRefreshBodyData(_bodies);

                    for (int i = 0; i < bodyCountTotal; i++)
                    {
                        FaceTracker curTrack = this.FaceTrackers[i];
                        if (!curTrack.Source.IsTrackingIdValid)
                        {
                            if (_bodies[i].IsTracked)
                            {
                                this.FaceTrackers[i].TrackingId = _bodies[i].TrackingId;
                                this._faces[i].TrackingId       = _bodies[i].TrackingId;
                            }
                        }
                        if (curTrack.Reader != null)
                        {
                            HighDefinitionFaceFrame hdFrame = curTrack.Reader.AcquireLatestFrame();

                            if (hdFrame != null)
                            {
                                this._faces[i].IsTracked = hdFrame.IsFaceTracked;
                                if (hdFrame.FaceModel != null && hdFrame.IsFaceTracked)
                                {
                                    hdFrame.GetAndRefreshFaceAlignmentResult(curTrack.Alignment);
                                    var vertices = curTrack.Model.CalculateVerticesForAlignment(curTrack.Alignment);
                                    this._faces[i].Update(vertices);
                                }
                            }
                        }
                    }
                }
            }

            KinectFrameEventArgs data = new KinectFrameEventArgs(_pixelsFrame, this.frameSourceType, _bodies, _faces);

            OnFrameArrived?.Invoke(this, data);
        }