//キャリブレーション装置LEDの中心をとる //カラーイメージ取得 void ColorFrame_Arrived(object sender, ColorFrameArrivedEventArgs e) { try { ColorFrame colorFrame = e.FrameReference.AcquireFrame(); //フレームがなければ終了、あれば格納 if (colorFrame == null) { return; } colorFrame.CopyConvertedFrameDataToArray(this.colors, this.colorImageFormat); //表示 this.colorImg.WritePixels(this.bitmapRect, this.colors, this.bitmapStride, 0); //データ送信 if (this.IsCalib == true || this.SendData_LEDPt.IsChecked == true) { this.SendData_LEDPt.IsChecked = true; this.SendData(); this.IsCalib = false; } //破棄 colorFrame.Dispose(); } catch { this.ErrorPoint(System.Reflection.MethodBase.GetCurrentMethod().ToString()); } }
//重点 /// <summary> /// 处理从传感器接收到的颜色帧数据 /// </summary> private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrameArrivedEventArgs:为事件传递参数,返回当前彩色图像帧 // ColorFrame:表示某一帧彩色图像帧 using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { // FrameDescription:表示来自Kinect传感器的图像帧的属性 FrameDescription colorFrameDescription = colorFrame.FrameDescription; // 运行Kinect实例访问内存 using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { // 不知道什么意思,可能是对内存的读写控制 this.colorBitmap.Lock(); // 验证数据并将新的颜色框数据写入显示位图 if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { // 没找到,但我之前见过 colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); // 不知道什么意思,估计是画图 this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } // 不知道什么意思,可能是对内存的读写控制 this.colorBitmap.Unlock(); } } } }
/// <summary> /// creates a color picture from the colorframe data package and broadcasts it /// </summary> /// <param name="e">the colorframe data package</param> void CalculateColorPicture(ColorFrameArrivedEventArgs e) { using (ColorFrame cf = e.FrameReference.AcquireFrame()) { if (cf != null) { byte[] colorPixels = new byte[cf.FrameDescription.Width * cf.FrameDescription.Height * ((PixelFormats.Bgr32.BitsPerPixel + 7) / 8)]; WriteableBitmap colorBitmap = new WriteableBitmap( cf.FrameDescription.Width, cf.FrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); if ((cf.FrameDescription.Width == colorBitmap.PixelWidth) && (cf.FrameDescription.Height == colorBitmap.PixelHeight)) { if (cf.RawColorImageFormat == ColorImageFormat.Bgra) { cf.CopyRawFrameDataToArray(colorPixels); } else { cf.CopyConvertedFrameDataToArray(colorPixels, ColorImageFormat.Bgra); } colorBitmap.WritePixels( new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight), colorPixels, colorBitmap.PixelWidth * ((PixelFormats.Bgr32.BitsPerPixel + 7) / 8), 0); colorBitmap.Freeze(); OnColorPictureEvent.BeginInvoke(colorBitmap, null, null); } } } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { //use the current image frame in a memory safe manner using (ColorFrame frame = e.FrameReference.AcquireFrame()) { //sometimes this frame isn't available //defensive programming if (frame == null) { return; } using (KinectBuffer colorBuffer = frame.LockRawImageBuffer()) { //threadsafe lock on this data so it doesn't get modified pushupColorBitmap.Lock(); //let application to know where the image is being stored frame.CopyConvertedFrameDataToIntPtr(pushupColorBitmap.BackBuffer, (uint)(1920 * 1080 * 4), ColorImageFormat.Bgra); //the number is width*height*bytes per pixel //redraw the screen in this area pushupColorBitmap.AddDirtyRect(new Int32Rect(0, 0, pushupColorBitmap.PixelWidth, pushupColorBitmap.PixelHeight)); //unlock pushupColorBitmap.Unlock(); } } }
/// <summary> /// Handles the color frame data arriving from the sensor /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
private void ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (!isRecording) { return; } colorFrameTick += 1; var props = sensor.ColorFrameSource.FrameDescription; if ((colorFrameTick % Resolution == 0)) { var buffer = new byte[props.Width * props.Height * 4]; bool shouldProcess = false; using (var frame = e.FrameReference.AcquireFrame()) { if (frame != null) { shouldProcess = true; frame.CopyConvertedFrameDataToArray(buffer, ColorImageFormat.Bgra); } } if (shouldProcess) { if (colorQueue.Count < 5) { BufferForFrame x; x.buffer = buffer; x.frame_id = colorFrameTick; colorQueue.Add(x); } } } }
// ColorFrameReader event handler void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame == null) { return; } // Locks raw pixel data for the captured frame using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { colorBitmap.Lock(); colorFrame.CopyConvertedFrameDataToIntPtr( colorBitmap.BackBuffer, (uint)(1920 * 1080 * 4), ColorImageFormat.Bgra); // Defines the entire image as update space colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); colorBitmap.Unlock(); } } }
private void Read_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr(colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); //colorframe의 현제 프레임 정보를 colorbitmap에 복사하는 기능, 이미지 크기 = 가로*세로*4바이트의 색상, 이미지포멧은 Bgra포맷 this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { colorBitmap.Lock(); if ((colorFrameDescription.Width == colorBitmap.PixelWidth) && (colorFrameDescription.Height == colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); } colorBitmap.Unlock(); } } } }
private void ColorReader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (this.processingColorFrame) { return; } this.processingColorFrame = true; bool colorFrameProcessed = false; // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { if (colorFrame.RawColorImageFormat == ColorImageFormat.Rgba) { colorFrame.CopyRawFrameDataToArray(this.colorPixels); } else { colorFrame.CopyConvertedFrameDataToArray(this.colorPixels, ColorImageFormat.Rgba); } colorFrameProcessed = true; } } } if (colorFrameProcessed) { this.colorFrameCallback(this.colorPixels); } this.processingColorFrame = false; }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { var colorFrameProcessed = false; using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; if ((colorFrameDescription.Width == colorBitmap.PixelWidth) && (colorFrameDescription.Height == colorBitmap.PixelHeight)) { if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra) { colorFrame.CopyRawFrameDataToBuffer(colorBitmap.PixelBuffer); } else { colorFrame.CopyConvertedFrameDataToBuffer(colorBitmap.PixelBuffer, ColorImageFormat.Bgra); } colorFrameProcessed = true; } } } if (colorFrameProcessed) { colorBitmap.Invalidate(); } }
void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // Get the current image frame in a memory-safe manner using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { // Defensive programming: Just in case the sensor frame is no longer valid, exit the function if (colorFrame == null) { return; } using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { // Put a thread-safe lock on this data so it doesn't get modified elsewhere colorBitmap.Lock(); // Let the application know where the image is being stored colorFrame.CopyConvertedFrameDataToIntPtr( colorBitmap.BackBuffer, (uint)(1920 * 1080 * 4), // Width * Height * BytesPerPixel ColorImageFormat.Bgra); // Let the application know that it needs to redraw the screen in this area (the whole image) colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); // Remove the thread-safe lock on this data colorBitmap.Unlock(); } } }
//színes képi adatok kezelése, ami a Kinect felől jön private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame elérhető using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // adatok ellenőrzése és továbbítása a bittérképként a colorBitmap bufferbe if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
/// <summary> /// Handle color frame arrived from dedicated Color frames reader. /// Send frame to all registered processors and Dispose it. /// </summary> /// <param name="sender"></param> /// <param name="evt"></param> private void ColorFrameArrived(object sender, ColorFrameArrivedEventArgs evt) { try { if (ColorFrameEvent != null) { using (var colorFrame = evt.FrameReference.AcquireFrame()) { if (!_firstFrameRelativeTimeEventFired) { OnFirstFrameRelativeTimeEvent(colorFrame.RelativeTime); } ColorFrameEvent(colorFrame); } } _colorSourceFpsWatcher.Tick(); } catch (Exception e) { if (FrameProcessExceptionEvent != null) { FrameProcessExceptionEvent(e); } } }
private void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame frame = e.FrameReference.AcquireFrame()) { if (frame == null) { return; } using (KinectBuffer buffer = frame.LockRawImageBuffer()) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; Rectangle rect = new Rectangle(0, 0, width, height); uint size = (uint)(width * height * 4); if (bmp == null) { bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); } BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); frame.CopyConvertedFrameDataToIntPtr(bmpData.Scan0, size, ColorImageFormat.Bgra); bmp.UnlockBits(bmpData); } } if (bmp != null) { pictureBox1.Image = bmp; pictureBox1.Refresh(); } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { count += 1; // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } //on first image recieved if (count == 1) { { if (this.colorBitmap != null) { // create a png bitmap encoder which knows how to save a .png file BitmapEncoder encoder = new PngBitmapEncoder(); // create frame from the writable bitmap and add to encoder encoder.Frames.Add(BitmapFrame.Create(this.colorBitmap)); string path = Path.Combine("test_images", "image1.jpg"); // write the new file to disk try { // FileStream is IDisposable using (FileStream fs = new FileStream(path, FileMode.Create)) { encoder.Save(fs); } } catch (IOException) { Debug.WriteLine("failed to send screencap"); } } }; } }
/// <summary> /// 彩色图回调函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { //获取彩色坐标信息 FrameDescription colorFrameDescription = colorFrame.FrameDescription; //绘制彩色图 using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); //分析数据将新的彩色图数据写入位图 if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
/// <summary> /// Dispatcher to update the color image control /// 用 Dispatcher 更新主线程 (UI 线程) 组件 /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void Color_ShowImage(Object sender, ColorFrameArrivedEventArgs e) { this.Dispatcher.BeginInvoke( (Action) (() => this.RenderColorImage(ref this.colorBitmap, this.colorImage, e)) ); }
private void ColorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { _colorCameraIntrinsics = e.CameraIntrinsics; var bitmap = e.GetDisplayableBitmap(BitmapPixelFormat.Bgra8); bitmap = Interlocked.Exchange(ref _colorBackBuffer, bitmap); bitmap?.Dispose(); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed ColorOutput.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { if (Interlocked.CompareExchange(ref _isRenderingColor, 1, 0) == 0) { try { SoftwareBitmap availableFrame = null; while ((availableFrame = Interlocked.Exchange(ref _colorBackBuffer, null)) != null) { await((SoftwareBitmapSource)ColorOutput.Source).SetBitmapAsync(availableFrame); availableFrame.Dispose(); } } finally { Interlocked.Exchange(ref _isRenderingColor, 0); } } }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed }
/// <summary> /// Handles the color frame data arriving from the sensor. /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { bool colorFrameProcessed = false; // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; // verify data and write the new color frame data to the Writeable bitmap if ((colorFrameDescription.Width == this.bitmap.PixelWidth) && (colorFrameDescription.Height == this.bitmap.PixelHeight)) { if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra) { colorFrame.CopyRawFrameDataToBuffer(this.bitmap.PixelBuffer); } else { colorFrame.CopyConvertedFrameDataToBuffer(this.bitmap.PixelBuffer, ColorImageFormat.Bgra); } colorFrameProcessed = true; } } } // we got a frame, render if (colorFrameProcessed) { this.bitmap.Invalidate(); } }
/// <summary> /// Display color image on image control /// </summary> /// <param name="bitmap">color bitmap</param> /// <param name="image">color image control</param> /// <param name="args">frame args</param> private void RenderColorImage(ref WriteableBitmap bitmap, System.Windows.Controls.Image image, ColorFrameArrivedEventArgs args) { using (ColorFrame colorFrame = args.FrameReference.AcquireFrame()) { if (colorFrame == null) { return; } else { using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { bitmap.Lock(); colorFrame.CopyConvertedFrameDataToIntPtr( bitmap.BackBuffer, (uint)(1920 * 1080 * 4), ColorImageFormat.Bgra); bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); bitmap.Unlock(); image.Source = bitmap; } } } }
private void ColorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { var colorFrame = e.FrameReference.AcquireFrame(); if (colorFrame != null) { using (colorFrame) { lastColorGain = colorFrame.ColorCameraSettings.Gain; lastColorExposureTimeTicks = colorFrame.ColorCameraSettings.ExposureTime.Ticks; if (yuvFrameReady.Count > 0) { lock (yuvByteBuffer) colorFrame.CopyRawFrameDataToArray(yuvByteBuffer); lock (yuvFrameReady) foreach (var autoResetEvent in yuvFrameReady) { autoResetEvent.Set(); } } if (rgbFrameReady.Any(ready => !ready.WaitOne(0))) { lock (rgbByteBuffer) colorFrame.CopyConvertedFrameDataToArray(rgbByteBuffer, ColorImageFormat.Bgra); lock (rgbFrameReady) foreach (var autoResetEvent in rgbFrameReady) { autoResetEvent.Set(); } } } } }
/// <summary> /// カラーフレームを取得した時のイベント /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (var colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame == null) { return; } //BGRAデータを登録 colorBuffer = new byte[colorFrameDesc.Width * colorFrameDesc.Height * colorFrameDesc.BytesPerPixel]; colorFrame.CopyConvertedFrameDataToArray(colorBuffer, ColorImageFormat.Bgra); bitmapSource = BitmapSource.Create(colorFrameDesc.Width, colorFrameDesc.Height, 96, 96, PixelFormats.Bgra32, null, colorBuffer, colorFrameDesc.Width * (int)colorFrameDesc.BytesPerPixel); //ImageColor.Source = bitmapSource; ImageColor.SetCurrentValue(System.Windows.Controls.Image.SourceProperty, bitmapSource); if (RecordPoints.IsChecked == true && frameCount % 3 == 0) { using (Stream stream = new FileStream(pathSaveFolder + "image/" + StopWatch.ElapsedMilliseconds + ".jpg", FileMode.Create)) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); encoder.Save(stream); stream.Close(); } } frameCount++; } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } //calculate FPS this.fps = 1.0 / colorFrame.ColorCameraSettings.FrameInterval.TotalSeconds; this.OnFrameArrived.Invoke(this, AgleView.KinectColor); this.colorBitmap.Unlock(); } } } }
private void Runtime_ColorFrameReady(object sender, ColorFrameArrivedEventArgs e) { if (this.ColorFrameReady != null) { this.ColorFrameReady(sender, e); } }
private void cfr_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (e.FrameReference == null) { return; } using (ColorFrame cf = e.FrameReference.AcquireFrame()) { if (cf == null) { return; } cf.CopyConvertedFrameDataToArray(colorData, format); var fd = cf.FrameDescription; // Creating BitmapSource var bytesPerPixel = (PixelFormats.Bgr32.BitsPerPixel) / 8; var stride = bytesPerPixel * cf.FrameDescription.Width; bmpSource = BitmapSource.Create(fd.Width, fd.Height, 96.0, 96.0, PixelFormats.Bgr32, null, colorData, stride); // WritableBitmap to show on UI wbmp = new WriteableBitmap(bmpSource); image.Source = wbmp; //image1.Source = People.png } }
void OnFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // I don't *think* this event is re-entrant in the sense that I // don't think we'll get it fired while we're handling it. Once // we return to the caller it can call us again though and that // can happen after I've called Task.Run and returned below. if (this.bufferIdle) { this.bufferIdle = false; Task.Run( async () => { var frame = e.FrameReference.AcquireFrame(); if (frame != null) { frame.CopyConvertedFrameDataToArray(this.frameArray, ColorImageFormat.Bgra); await this.frameHandler( this.frameArray, (int)this.FrameSize.Width, (int)this.FrameSize.Height); frame.Dispose(); this.bufferIdle = true; } else { this.bufferIdle = true; } }); } }
/** * @brief Converts Bitmap into .jpeg Image * Recieves the frame captured by the Kinect Sensor as BITMAP and saves as a .jpeg every 3 frames * to reduce game lag. * * @param object sender * @param ColorFramArrivedEventArgs e * @return void * @pre * @post */ private void cfr_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (e.FrameReference == null) { return; } using (ColorFrame cf = e.FrameReference.AcquireFrame()) { if (cf == null) { return; } cf.CopyConvertedFrameDataToArray(colorData, format); var fd = cf.FrameDescription; bmpSource = new Bitmap(fd.Width, fd.Height, PixelFormat.Format32bppRgb); BitmapData bmpData = bmpSource.LockBits(new Rectangle(0, 0, bmpSource.Width, bmpSource.Height), ImageLockMode.WriteOnly, bmpSource.PixelFormat); Marshal.Copy(colorData, 0, bmpData.Scan0, colorData.Length); bmpSource.UnlockBits(bmpData); if (count % 3 == 0) { bmpSource.Save("..\\ffmpeg\\Images\\img" + (imageSerial++) + ".jpeg", myImageCodecInfo, myEncoderParameters); } } count++; }
private void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { // 防御性编程:以防传感器跳过一个框架,退出功能 if (colorFrame == null) { return; } // 设置一个数组,可以容纳所有的字节的图像 var colorFrameDescription = colorFrame.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); var frameSize = colorFrameDescription.Width * colorFrameDescription.Height * colorFrameDescription.BytesPerPixel; var colorData = new byte[frameSize]; //填写的数组数据从相机 colorFrame.CopyConvertedFrameDataToArray(colorData, ColorImageFormat.Bgra); // 使用字节数组,使图像在屏幕上 CameraImage.Source = BitmapSource.Create( colorFrame.ColorFrameSource.FrameDescription.Width, colorFrame.ColorFrameSource.FrameDescription.Height, 96, 96, PixelFormats.Bgr32, null, colorData, colorFrameDescription.Width * 4); } }
private void ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (null == e.FrameReference) { return; } // If you do not dispose of the frame, you never get another one... using (ColorFrame _ColorFrame = e.FrameReference.AcquireFrame()) { if (null == _ColorFrame) { return; } BitmapToDisplay.Lock(); _ColorFrame.CopyConvertedFrameDataToIntPtr( BitmapToDisplay.BackBuffer, Convert.ToUInt32(BitmapToDisplay.BackBufferStride * BitmapToDisplay.PixelHeight), ColorImageFormat.Bgra); BitmapToDisplay.AddDirtyRect( new Int32Rect( 0, 0, _ColorFrame.FrameDescription.Width, _ColorFrame.FrameDescription.Height)); BitmapToDisplay.Unlock(); } }
void _colorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (_displayType == FrameTypes.Color) { _colorBitmap.Update(e.FrameReference); } }
private void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { // Defensive programming: Just in case the sensor skips a frame, exit the function if (colorFrame == null) { return; } // Setup an array that can hold all of the bytes of the image var colorFrameDescription = colorFrame.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); var frameSize = colorFrameDescription.Width * colorFrameDescription.Height * colorFrameDescription.BytesPerPixel; var colorData = new byte[frameSize]; // Fill in the array with the data from the camera colorFrame.CopyConvertedFrameDataToArray(colorData, ColorImageFormat.Bgra); // Use the byte array to make an image and put it on the screen CameraImage.Source = BitmapSource.Create( colorFrame.ColorFrameSource.FrameDescription.Width, colorFrame.ColorFrameSource.FrameDescription.Height, 96, 96, PixelFormats.Bgr32, null, colorData, colorFrameDescription.Width * 4); } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (!hasColorArrived) { hasArrived.Signal(); Console.WriteLine("Signal at color"); hasColorArrived = true; } if (recordMode != RecordMode.Playingback) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { // Write the data from colorFrame into rgbBitmap colorFrame.CopyConvertedFrameDataToArray(rgbValues, ColorImageFormat.Bgra); BitmapData bmapdata = rgbBitmap.LockBits( new Rectangle(0, 0, colorFrameDescription.Width, colorFrameDescription.Height), ImageLockMode.WriteOnly, rgbBitmap.PixelFormat); IntPtr ptr = bmapdata.Scan0; Marshal.Copy(rgbValues, 0, ptr, colorFrameDescription.Width * colorFrameDescription.Height * 4); rgbBitmap.UnlockBits(bmapdata); this.widthAspect = (float)this.rgbBoard.Width / colorFrameDescription.Width; this.heightAspect = (float)this.rgbBoard.Height / colorFrameDescription.Height; // Show rgbBitmap into rgbBoard this.rgbBoard.Image = rgbBitmap; if (recordMode == RecordMode.Recording && this.writer != null) { tmspStartRecording = tmspStartRecording ?? DateTime.Now.TimeOfDay; var currentTime = DateTime.Now.TimeOfDay; if (!startRecordingRgb.HasValue) { startRecordingRgb = DateTime.Now; } TimeSpan elapse = currentTime - tmspStartRecording.Value; //ResizeAndWriteImageAsync(); WriteImageIntoBufferAsync(elapse); } } } } } }
void colorFrameReader_FrameArrived( ColorFrameReader sender, ColorFrameArrivedEventArgs args ) { using ( var colorFrame = args.FrameReference.AcquireFrame()){ // BGRAデータを取得する colorFrame.CopyConvertedFrameDataToArray( colorBuffer, ColorImageFormat.Bgra ); // ビットマップにする var stream = colorBitmap.PixelBuffer.AsStream(); stream.Write( colorBuffer, 0, colorBuffer.Length ); colorBitmap.Invalidate(); } }
/// <summary> /// Delegate which will be called, if a frame arrived /// </summary> /// <param name="sender">source which has fired this event</param> /// <param name="e">event arguments containing the incoming frame</param> private void Reader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { //load color frame using (ColorFrame frame = e.FrameReference.AcquireFrame()) { //pass color frame for interpretation, only if this handler is enabled if (frame != null && Enabled) { controller.Ui.color_background.Source = ToBitmap(frame); } } }
private void UpdateColorFrame( ColorFrameArrivedEventArgs e ) { // カラーフレームを取得する using ( var colorFrame = e.FrameReference.AcquireFrame() ) { if ( colorFrame == null ) { return; } // BGRAデータを取得する colorFrame.CopyConvertedFrameDataToArray( colorBuffer, colorFormat ); } }
void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (var colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame == null) { return; } colorbuffer = new byte[frame.Width * frame.Height * frame.BytesPerPixel]; colorFrame.CopyConvertedFrameDataToArray(colorbuffer, ColorImageFormat.Bgra); ImageColor.Source = BitmapSource.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null, colorbuffer, frame.Width * (int)frame.BytesPerPixel); } }
void colorFrameReader_FrameArrived( object sender, ColorFrameArrivedEventArgs e ) { using ( var colorFrame = e.FrameReference.AcquireFrame() ) { if ( colorFrame == null ) { return; } byte[] colorBuffer = new byte[colorFrame.FrameDescription.Width * colorFrame.FrameDescription.Height * 4]; colorFrame.CopyConvertedFrameDataToArray( colorBuffer, ColorImageFormat.Bgra ); ImageColor.Source = BitmapSource.Create( colorFrame.FrameDescription.Width, colorFrame.FrameDescription.Height, 96, 96, PixelFormats.Bgra32, null, colorBuffer, colorFrame.FrameDescription.Width * 4 ); } }
void colorFrameReader_FrameArrived( object sender, ColorFrameArrivedEventArgs e ) { // カラーフレームを取得する using ( var colorFrame = e.FrameReference.AcquireFrame() ) { if ( colorFrame == null ) { return; } // BGRAデータを取得する colorBuffer = new byte[colorFrameDesc.Width * colorFrameDesc.Height * colorFrameDesc.BytesPerPixel]; colorFrame.CopyConvertedFrameDataToArray( colorBuffer, ColorImageFormat.Bgra ); // ビットマップにする ImageColor.Source = BitmapSource.Create( colorFrameDesc.Width, colorFrameDesc.Height, 96, 96, PixelFormats.Bgra32, null, colorBuffer, colorFrameDesc.Width * (int)colorFrameDesc.BytesPerPixel ); } }
private static void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { //throw new NotImplementedException(); using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { var blob = colorFrame.Serialize(); foreach (var client in clients) { if (blob != null) { client.Send(blob); Console.WriteLine("After color Blob sent"); } } } } }
/// <summary> /// Handles the color frame data arriving from the sensor /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { //update the audio view box visibility this.AudioViewBox.Visibility = this.AudioMeterVisibility; //render color layer this.colorRenderer.Reader_ColorFrameArrived(sender, e); elapsedFrames++; var systemReady = (this.kinectSensor != null && this.kinectSensor.IsAvailable && this.kinectSensor.IsOpen && this.panTilt.IsReady); if (systemReady) { this.statusText = Microsoft.Samples.Kinect.BodyBasics.Properties.Resources.RunningStatusText; } string safetyText; if (this.firingControl.VirtualSafetyOn) { safetyText = Microsoft.Samples.Kinect.BodyBasics.Properties.Resources.SafetyDisengagedText; } else { safetyText = Microsoft.Samples.Kinect.BodyBasics.Properties.Resources.SafetyEngagedText; } //draw the headsup display initially this.hudRenderer.RenderHud(new HudRenderingParameters() { CannonX = this.CannonX, CannonY = this.CannonY, //CannonTheta = this.CannonTheta, StatusText = this.statusText, SystemReady = systemReady, FrameRate = this.FrameRate, TrackingMode = this.trackingMode, FiringSafety = this.firingControl.VirtualSafetyOn, FiringSafetyText = safetyText }); }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(310, 0, this.colorBitmap.PixelWidth - 620, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
private void ColorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame frame = e.FrameReference.AcquireFrame()) { if (frame != null) { FrameDescription colorFrameDescription = frame.FrameDescription; using (KinectBuffer colorBuffer = frame.LockRawImageBuffer()) { this.ColorImage.Lock(); if ((colorFrameDescription.Width == this.ColorImage.PixelWidth) && (colorFrameDescription.Height == this.ColorImage.PixelHeight)) { frame.CopyConvertedFrameDataToIntPtr( this.ColorImage.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.ColorImage.AddDirtyRect(new Int32Rect(0, 0, this.ColorImage.PixelWidth, this.ColorImage.PixelHeight)); } OnFrameArrived(e); this.ColorImage.Unlock(); } } } }
void ColorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (var frame = e.FrameReference.AcquireFrame()) { if (frame != null) { this.Back.Source = frame.ToBitmap(); Canvas.SetLeft(Back, -400); } } }
void colorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame frame = e.FrameReference.AcquireFrame()) { if (frame != null) { FrameDescription desc = frame.FrameDescription; KinectBase.ColorFrameEventArgs colorE = new KinectBase.ColorFrameEventArgs(); colorE.bytesPerPixel = 4; //This is fixed to 4 because we are converting to bgra below) colorE.pixelFormat = PixelFormats.Bgra32; colorE.height = desc.Height; colorE.width = desc.Width; colorE.kinectID = kinectID; colorE.timeStamp = frame.RelativeTime; colorE.isIR = false; colorE.image = colorImagePool.GetObject(); //colorE.image = new byte[desc.LengthInPixels * colorE.bytesPerPixel]; //frame.CopyConvertedFrameDataToArray(colorE.image, ColorImageFormat.Bgra); unsafe { fixed (byte* ptr = colorE.image) { frame.CopyConvertedFrameDataToIntPtr((IntPtr)ptr, desc.LengthInPixels * sizeof(byte) * 4, ColorImageFormat.Bgra); } } OnColorFrameReceived(colorE); } } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { Frame_Update(); if (_needColor) { //lowers fps to improve performance if (_frameCount % 2 == 0) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription frameDescription = this.GetFrameDescriptionForMode(BackgroundMode.Color); using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { // verify data and write the new color frame data to the display bitmap if (this._source == null || (frameDescription.Width != this._source.PixelWidth) || (frameDescription.Height != this._source.PixelHeight)) { this._source = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); } this._source.Lock(); colorFrame.CopyConvertedFrameDataToIntPtr(this._source.BackBuffer, (uint)(frameDescription.Width * frameDescription.Height * 4), ColorImageFormat.Bgra); this._source.AddDirtyRect(new Int32Rect(0, 0, this._source.PixelWidth, this._source.PixelHeight)); _gaussianSource = this._source.Resize(frameDescription.Width / 4, frameDescription.Height / 4, WriteableBitmapExtensions.Interpolation.Bilinear); _gaussianSource = _gaussianSource.Convolute(WriteableBitmapExtensions.KernelGaussianBlur5x5); this.Update(); this._source.Unlock(); } } } } _frameCount++; } }
//カラーイメージ取得時のイベント void ColorFrame_Arrived(object sender, ColorFrameArrivedEventArgs e) { ColorFrame colorFrame = e.FrameReference.AcquireFrame(); //フレームがなければ終了、あれば格納 if (colorFrame == null) return; colorFrame.CopyConvertedFrameDataToArray(this.colors, this.colorImageFormat); this.package.K_colors = this.colors; //破棄 colorFrame.Dispose(); }
/// <summary> /// A new frame arrives 15 to 30 times per second. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async void reader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { ColorFrameReference frameReference = e.FrameReference; if (this.startTime.Ticks == 0) { this.startTime = frameReference.RelativeTime; } try { ColorFrame frame = frameReference.AcquireFrame(); if (frame != null) { // ColorFrame is IDisposable using (frame) { SensorStatus = SensorStatus.Active; FrameDescription frameDescription = frame.FrameDescription; // verify data and write the new color frame data to the display bitmap if ((frameDescription.Width == this.videoImageSource.PixelWidth) && (frameDescription.Height == this.videoImageSource.PixelHeight)) { await DisplayColorFrame(frame); } CalculateFPS(); } } } catch (Exception ex) { // ignore if the frame is no longer available string msg = ex.Message; Console.WriteLine(msg); } }
/// <summary> /// Handles the color frame data arriving from the sensor /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> public void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.imageSource.PixelWidth) && (colorFrameDescription.Height == this.imageSource.PixelHeight)) { if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra) { colorFrame.CopyRawFrameDataToArray(this.colorPixels); } else { colorFrame.CopyConvertedFrameDataToArray(this.colorPixels, ColorImageFormat.Bgra); } } this.RenderColorPixels(); } } }
private void ColorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (var frame = e.FrameReference.AcquireFrame()) { if (frame != null) { camera.Source = frame.ToBitmap(); } } }
//subscribed event set during kinect initialization (called each time a color frame is available) private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { bool colorFrameProcessed = false; // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra) { colorFrame.CopyRawFrameDataToArray(this.colorPixels); } else { colorFrame.CopyConvertedFrameDataToArray(this.colorPixels, ColorImageFormat.Bgra); } colorFrameProcessed = true; } //update the image collection buffer with the most recent frame and remove the old frames if the buffer is full if (currentFrame % frameAcceptance == 0) //set to only add every frameAcceptanceth'd frame _videoArray.Add(colorFrame.ToOpenCVImage<Rgb, Byte>()); if (_videoArray.Count() > videoRecordLength / frameAcceptance) // Frame limiter _videoArray.RemoveAt(0); } } // push the color frame data to the bitmap (set to UI from ColorSource call below) if (colorFrameProcessed) { this.colorBitmap.WritePixels(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight), this.colorPixels, this.colorBitmap.PixelWidth * (int)this.bytesPerPixel, 0); } }
private void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { if ((bool)chk_skin.IsChecked) { var handLeft = coordinateMapper.MapCameraPointToColorSpace(leftHandPostition); var handRight = coordinateMapper.MapCameraPointToColorSpace(rightHandPostition); colorFrame.CopyConvertedFrameDataToArray(colorFrameData, ColorImageFormat.Bgra); getSkinColor(handLeft, handRight); } colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * BytesPerPixel), ColorImageFormat.Bgra); //32Bit(4Byte) this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
private void _colorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (var frame = e.FrameReference.AcquireFrame()) { if (_isStarted && colorFpsCounter > 0) { if (frame != null) { //colorSw.Start(); _recordColorQueue.Enqueue(new RecordColorFrame(frame)); //colorSw.Stop(); //colorSum += colorSw.Elapsed.TotalMilliseconds; //colorSw.Reset(); TimeCheckColor(); colorFrames++; colorCounter++; //Console.WriteLine("Color Enqueue time = {0}", colorSw.Elapsed.TotalMilliseconds); //System.Diagnostics.Debug.WriteLine("+++ Enqueued Color Frame ({0})", _recordQueue.Count); } else { // System.Diagnostics.Debug.WriteLine("!!! FRAME SKIPPED (Color in KinectRecorder)"); } colorFpsCounter -= ColorFramerate; } else if( colorFpsCounter == -50 || colorFpsCounter == -125) { colorFpsCounter -= ColorFramerate; } else { colorFpsCounter = 100; // System.Diagnostics.Debug.WriteLine("!!! FRAME SKIPPED "); } } }
void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { var colorFrame = e.FrameReference.AcquireFrame(); if (colorFrame != null) { using (colorFrame) { lastColorGain = colorFrame.ColorCameraSettings.Gain; lastColorExposureTimeTicks = colorFrame.ColorCameraSettings.ExposureTime.Ticks; if (yuvFrameReady.Count > 0) { lock (yuvByteBuffer) colorFrame.CopyRawFrameDataToArray(yuvByteBuffer); lock (yuvFrameReady) foreach (var autoResetEvent in yuvFrameReady) autoResetEvent.Set(); } if ((rgbFrameReady.Count > 0) || (jpegFrameReady.Count > 0)) { lock (rgbByteBuffer) colorFrame.CopyConvertedFrameDataToArray(rgbByteBuffer, ColorImageFormat.Bgra); lock (rgbFrameReady) foreach (var autoResetEvent in rgbFrameReady) autoResetEvent.Set(); } if (jpegFrameReady.Count > 0) { // should be put in a separate thread? stopWatch.Restart(); var bitmapSource = new Bitmap(imagingFactory, Kinect2Calibration.colorImageWidth, Kinect2Calibration.colorImageHeight, SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad); var bitmapLock = bitmapSource.Lock(BitmapLockFlags.Write); Marshal.Copy(rgbByteBuffer, 0, bitmapLock.Data.DataPointer, Kinect2Calibration.colorImageWidth * Kinect2Calibration.colorImageHeight * 4); bitmapLock.Dispose(); var memoryStream = new MemoryStream(); //var fileStream = new FileStream("test" + frame++ + ".jpg", FileMode.Create); //var stream = new WICStream(imagingFactory, "test" + frame++ + ".jpg", SharpDX.IO.NativeFileAccess.Write); var stream = new WICStream(imagingFactory, memoryStream); var jpegBitmapEncoder = new JpegBitmapEncoder(imagingFactory); jpegBitmapEncoder.Initialize(stream); var bitmapFrameEncode = new BitmapFrameEncode(jpegBitmapEncoder); bitmapFrameEncode.Options.ImageQuality = 0.5f; bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(Kinect2Calibration.colorImageWidth, Kinect2Calibration.colorImageHeight); var pixelFormatGuid = PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid); bitmapFrameEncode.WriteSource(bitmapSource); bitmapFrameEncode.Commit(); jpegBitmapEncoder.Commit(); //fileStream.Close(); //fileStream.Dispose(); //Console.WriteLine(stopWatch.ElapsedMilliseconds + "ms " + memoryStream.Length + " bytes"); lock (jpegByteBuffer) { nJpegBytes = (int)memoryStream.Length; memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.Read(jpegByteBuffer, 0, nJpegBytes); } lock (jpegFrameReady) foreach (var autoResetEvent in jpegFrameReady) autoResetEvent.Set(); //var file = new FileStream("test" + frame++ + ".jpg", FileMode.Create); //file.Write(jpegByteBuffer, 0, nJpegBytes); //file.Close(); bitmapSource.Dispose(); memoryStream.Close(); memoryStream.Dispose(); stream.Dispose(); jpegBitmapEncoder.Dispose(); bitmapFrameEncode.Dispose(); } } } }
private void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { ColorFrame colorFrame = e.FrameReference.AcquireFrame(); if (colorFrame == null) return; /*以下はカラーデータの取得等の処理が入る・・・らしい*/ byte[] colors = new byte[colorFrameDescription.Width * colorFrameDescription.Height * colorFrameDescription.BytesPerPixel]; colorFrame.CopyConvertedFrameDataToArray(colors, this.colorImageFormat); /*ここの処理が複雑なので解説、(画像の横幅指定,画像の縦幅指定,dpi...?,dpi?,フォーマット,(たしか)エフェクト,画素データのバッファ量指定,ストライド(横一列)のbyte数指定*/ BitmapSource bitmapSource = BitmapSource.Create(colorFrameDescription.Width, colorFrameDescription.Height, 96, 96, PixelFormats.Bgr32, null, colors, colorFrameDescription.Width * (int)colorFrameDescription.BytesPerPixel); //キャンバスに表示する。 this.canvas.Background = new ImageBrush(bitmapSource); /*ここまで*/ colorFrame.Dispose();//データの破棄、メモリの解放 }
private void ColorReader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (this.processingColorFrame) { return; } this.processingColorFrame = true; bool colorFrameProcessed = false; // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { colorFrame.CopyConvertedFrameDataToArray(this.colorPixels, ColorImageFormat.Rgba); colorFrameProcessed = true; } } } if (colorFrameProcessed) { this.colorFrameCallback(this.colorPixels); } this.processingColorFrame = false; }
void ColorFrame_Arrived(object sender, ColorFrameArrivedEventArgs e) { var frameReference = e.FrameReference; if (frameReference == null) return; var frame = frameReference.AcquireFrame(); if (frame == null) return; using (frame) { var frameDescription = frame.FrameDescription; if (frameDescription.Width == bitmap.PixelWidth && frameDescription.Height == bitmap.PixelHeight) { if (frame.RawColorImageFormat == ColorImageFormat.Bgra) { frame.CopyRawFrameDataToArray(intermediaryArray); } else { frame.CopyConvertedFrameDataToArray(intermediaryArray, ColorImageFormat.Bgra); } bitmap.WritePixels(new Int32Rect(0, 0, frameDescription.Width, frameDescription.Height), intermediaryArray, (int)(frameDescription.Width * bytesPerPixel), 0); } } }
private void DepthFrameReady(object sender, ColorFrameArrivedEventArgs e) { var frame = e.FrameReference.AcquireFrame(); if (frame != null) { using (frame) { lock (m_lock) { frame.CopyConvertedFrameDataToIntPtr(this.depthwrite,1920 * 1080 * 4, ColorImageFormat.Bgra); IntPtr swap = this.depthread; this.depthread = this.depthwrite; this.depthwrite = swap; } this.FInvalidate = true; this.frameindex = frame.RelativeTime.Ticks; } } }
private void colorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (var frame = e.FrameReference.AcquireFrame()) { if (frame != null) { var width = frame.FrameDescription.Width; var height = frame.FrameDescription.Height; var pixels = new byte[width * height * (PixelFormats.Bgr32.BitsPerPixel / 8)]; frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra); ColorImage.Source = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgr32, BitmapPalettes.WebPalette, pixels, width * PixelFormats.Bgr32.BitsPerPixel / 8); } } }