将 VideoFrame 转换为字节数组 [英] Converting a VideoFrame to a byte array

查看:41
本文介绍了将 VideoFrame 转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试转换捕获的 VideoFrame 对象到字节数组,但收效甚微.从文档中可以清楚地看出,每个帧都可以保存到 SoftwareBitmap 对象,例如

I have been trying to convert a captured VideoFrame object to a byte array with little success. It is clear from the documentation that each frame can be saved to a SoftwareBitmap object, e.g.

SoftwareBitmap bitmap = frame.SoftwareBitmap;

我已经能够将此位图保存为图像,但我想获取它的数据并将其存储在字节数组中.许多 SO 问题已经解决了这个问题但是 SoftwareBitmap 属于 Windows.Graphics.Imaging 命名空间(不是其他 SO 发布地址的更典型的 Xaml.Controls.Image,比如这个) 所以像image.Save()这样的传统方法不可用.

I have been able to save this bitmap as an image but I would like to obtain it's data and store it in a byte array. Many SO questions already deal with this but the SoftwareBitmap belongs to the Windows.Graphics.Imaging namespace (not the more typical Xaml.Controls.Image which the other SO posts address, such as this one) so traditional methods like image.Save() are unavailable.

似乎每个 SoftwareBitmap 都有一个 CopyToBuffer() 方法,但是关于如何实际使用它的文档非常简洁.而且我也不确定这是否是正确的方法?

It seems that each SoftwareBitmap has a CopyToBuffer() method but the documentation on this is very terse with regards to how to actually use this. And I'm also not sure if that's the right way to go?

使用下面 Alan 的建议,我已经成功地完成了这项工作.我不确定它是否有用,但这是我在其他人遇到此问题时使用的代码:

Using Alan's recommendation below I've managed to get this working. I'm not sure if it's useful but here's the code I used if anyone else comes across this:

private void convertFrameToByteArray(SoftwareBitmap bitmap)
    {
        byte[] bytes;
        WriteableBitmap newBitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
        bitmap.CopyToBuffer(newBitmap.PixelBuffer);
        using (Stream stream = newBitmap.PixelBuffer.AsStream())
        using (MemoryStream memoryStream = new MemoryStream())
        {
            stream.CopyTo(memoryStream);
            bytes = memoryStream.ToArray();
        }

        // do what you want with the acquired bytes
        this.videoFramesAsBytes.Add(bytes);
    }

推荐答案

通过使用 CopyToBuffer() 方法,您可以将像素数据复制到 WriteableBitmap 的 PixelBuffer.

By using the CopyToBuffer() method, you can copy pixel data to the PixelBuffer of a WriteableBitmap.

那我觉得你可以参考这个问题的答案 将其转换为字节数组.

Then I think you can refer to the answer in this question to convert it to byte array.

这篇关于将 VideoFrame 转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆