【发布时间】:2023-03-13 18:18:02
【问题描述】:
我使用 kinect v2 流来显示彩色图像和身体图像。 现在我想保存彩色图像,但是如果我在主线程中添加代码,我什么也不会显示,因为每秒在硬盘上写入 30 个文件。 所以我考虑使用 Thread,特别是 ThreadPool,但我对 frame 有异常:
ColorCameraSettings 'colorFrame.ColorCameraSettings' ha generato un'eccezione di tipo 'System.ObjectDisposedException' Microsoft.Kinect.ColorCameraSettings {System.ObjectDisposedException}
这是我的代码:
private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
var reference = e.FrameReference.AcquireFrame();
this.StatusText = "NO BODY";
frameCount++;
//Console.WriteLine(frameCount);
bool dataReceived = false;
//sw.WriteLine("frame nuovo");
using (ColorFrame frame = reference.ColorFrameReference.AcquireFrame())
{
if (frame != null)
{
//image.Source = ToBitmap(frame);
ThreadPool.QueueUserWorkItem(writeColorImage, frame);
}
}..............
然后写:
private void writeColorImage(Object frame)
{
ColorFrame colorFrame = (ColorFrame)frame;
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ToBitmap(colorFrame)));
using (var filestream = new FileStream(filelocation + "image" + frameCount + ".jpg", FileMode.Create))
encoder.Save(filestream);
}
private BitmapSource ToBitmap(ColorFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
PixelFormat format = PixelFormats.Bgr32;
byte[] pixels = new byte[width * height * ((PixelFormats.Bgr32.BitsPerPixel + 7) / 8)];
if (frame.RawColorImageFormat == ColorImageFormat.Bgra)
{
frame.CopyRawFrameDataToArray(pixels);
}
else
{
frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra);
}
int stride = width * format.BitsPerPixel / 8;
return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride);
}
如何将这张图片写入硬盘?谢谢
【问题讨论】:
标签: c# multithreading threadpool kinect filestream