简体   繁体   English

将VideoFrame转换为字节数组

[英]Converting a VideoFrame to a byte array

I have been trying to convert a captured VideoFrame object to a byte array with little success. 我一直试图将捕获的VideoFrame对象转换为字节数组,但收效甚微。 It is clear from the documentation that each frame can be saved to a SoftwareBitmap object, eg 从文档中可以明显看出,每个框架都可以保存到SoftwareBitmap对象,例如

SoftwareBitmap bitmap = frame.SoftwareBitmap;

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. 许多SO问题已经解决了这个问题, 但是 SoftwareBitmap属于Windows.Graphics.Imaging命名空间(不是其他SO发布的更典型的Xaml.Controls.Image, 例如this ),所以传统方法如image.Save()不可用。

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. 似乎每个SoftwareBitmap都有一个CopyToBuffer()方法,但是有关如何实际使用此方法的文档非常简洁。 And I'm also not sure if that's the right way to go? 而且我也不确定这是否是正确的方法?

Edit: 编辑:

Using Alan's recommendation below I've managed to get this working. 使用下面的艾伦(Alan)的建议,我设法完成了这项工作。 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);
    }

By using the CopyToBuffer() method, you can copy pixel data to the PixelBuffer of a WriteableBitmap. 通过使用CopyToBuffer()方法,可以将像素数据复制到WriteableBitmap的PixelBuffer。

Then I think you can refer to the answer in this question to convert it to byte array. 然后,我认为您可以参考此问题的答案将其转换为字节数组。

For anyone looking to access an encoded byte[] array from the SoftwareBitmap (eg jpeg): 对于希望从SoftwareBitmap (例如jpeg)访问编码的 byte[]数组的任何人:

private async void PlayWithData(SoftwareBitmap softwareBitmap)
{
    var data = await EncodedBytes(softwareBitmap, BitmapEncoder.JpegEncoderId);

    // todo: save the bytes to a DB, etc
}

private async Task<byte[]> EncodedBytes(SoftwareBitmap soft, Guid encoderId)
{
    byte[] array = null;

    // First: Use an encoder to copy from SoftwareBitmap to an in-mem stream (FlushAsync)
    // Next:  Use ReadAsync on the in-mem stream to get byte[] array

    using (var ms = new InMemoryRandomAccessStream())
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, ms);
        encoder.SetSoftwareBitmap(soft);

        try
        {
            await encoder.FlushAsync();
        }
        catch ( Exception ex ){ return new byte[0]; }

        array = new byte[ms.Size];
        await ms.ReadAsync(array.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
    }
    return array;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM