【问题标题】:Convert Color32 array to byte array to send over network将 Color32 数组转换为字节数组以通过网络发送
【发布时间】:2017-11-30 14:08:53
【问题描述】:

我一直致力于在 Unity3D 中的 Android 设备上进行网络摄像头流式传输,以捕获视频和照片。我发现的大多数用于捕获网络摄像头提要的示例都使用特定的 WebCamTexture 对象来访问设备的摄像头硬件。我目前能够捕获相机输入,但 WebCamTexture 将数据存储为 Color32[]。我在下面找到了将 Color32[] 转换为 byte[] 的解决方案,但它似乎正在交换红色和蓝色通道。

https://stackoverflow.com/a/21575147/8115962

有没有办法防止红蓝通道颠倒?

【问题讨论】:

  • 通过网络发送纹理真的很繁重并且浪费带宽。为什么不在发送之前将其转换为 jpg 或 png?见this 发帖....

标签: c# unity3d image-capture color-channel


【解决方案1】:

这是将Color32 数组从WebCamTexture 转换为字节数组的另一种方法:

首先,创建一个结构来保存转换后的数组:

[StructLayout(LayoutKind.Explicit)]
public struct Color32Array
{
    [FieldOffset(0)]
    public byte[] byteArray;

    [FieldOffset(0)]
    public Color32[] colors;
}

要转换的WebCamTexture

WebCamTexture webcamTex = new WebCamTexture();

创建该结构的新实例:

Color32Array colorArray = new Color32Array();

用合适的尺寸初始化 Color32:

colorArray.colors = new Color32[webcamTex.width * webcamTex.height];

填充Color32自动填充字节数组:

webcamTex.GetPixels32(colorArray.colors);

现在,您可以使用colorArray.byteArray,它是字节数组。

如果需要,加载到 Texture 2D:

Texture2D tex = new Texture2D(2, 2);
tex.LoadRawTextureData(colorArray.byteArray);
tex.Apply();

就像我在comment 中所说,最好将WebCamTexture 转换为Texture2D,然后再转换为jpegpng,然后通过网络发送。这将减小图像的大小。有关更多信息,请参阅this 答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多