读取并处理YUV420P_Byte[]方法

来源:互联网 发布:rem js计算font size 编辑:程序博客网 时间:2024/06/16 22:53
一帧YUV420P需要拆分成三段 Y ,U, V
下面提供两种方法.关于效率请自行测试

一:

        byte[] data = new byte[Width * Height * 3 / 2];        byte[] dataY = new byte[Width * Height];        byte[] dataU = new byte[Width * Height / 4];        byte[] dataV = new byte[Width * Height / 4];        Buffer.BlockCopy(data, 0, dataY, 0, Width * Height);        Buffer.BlockCopy(data, Width * Height, dataU, 0, Width * Height / 4);        Buffer.BlockCopy(data, Width * Height * 5 / 4, dataV, 0, Width * Height / 4);

二:

 

        // 摘要:        //     从当前流中将 count 个字节读入字节数组,并使当前位置提升 count 个字节。        //        // 参数:        //   count:        //     要读取的字节数。        //        // 返回结果:        //     包含从基础流中读取的数据的字节数组。如果到达了流的末尾,则该字节数组可能小于所请求的字节数。        //        // 异常:        //   T:System.IO.IOException:        //     发生 I/O 错误。        //        //   T:System.ObjectDisposedException:        //     流已关闭。        //        //   T:System.ArgumentOutOfRangeException:        //     count 为负。        public virtual byte[] ReadBytes(int count);

        byte[] data = new byte[Width * Height * 3 / 2];        MemoryStream ms = new MemoryStream(data);        BinaryReader reader = new BinaryReader(ms);        byte[] dataY = reader.ReadBytes(Width * Height);        byte[] dataU = reader.ReadBytes(Width * Height / 4);        byte[] dataV = reader.ReadBytes(Width * Height / 4);