zoukankan      html  css  js  c++  java
  • yuv420转rgb 及 rgb转bmp保存

     /// <summary>
            /// 将一桢 YUV 格式的图像转换为一桢 RGB 格式图像。
            /// </summary>
            /// <param name="yuvFrame">YUV 格式图像数据。</param>
            /// <param name="rgbFrame">RGB 格式图像数据。</param>
            /// <param name="width">图像宽(单位:像素)。</param>
            /// <param name="height">图像高(单位:像素)。</param>
            static void ConvertYUV2RGB(byte[] yuvFrame, byte[] rgbFrame, int width, int height)
            {
                int uIndex = width * height;
                int vIndex = uIndex + ((width * height) >> 2);
                int gIndex = width * height;
                int bIndex = gIndex * 2;
    
                int temp = 0;
    
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        // R分量
                        temp = (int)(yuvFrame[y * width + x] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[0, 2]);
                        rgbFrame[y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
    
                        // G分量
                        temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 1] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 2]);
                        rgbFrame[gIndex + y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
    
                        // B分量
                        temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[2, 1]);
                        rgbFrame[bIndex + y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
                    }
                }
            }
    
            void WriteBMP(byte[] rgbFrame, int width, int height)
            {
                // 写 BMP 图像文件。
                int yu = width * 3 % 4;
                int bytePerLine = 0;
                yu = yu != 0 ? 4 - yu : yu;
                bytePerLine = width * 3 + yu;
    
                int length = rgbFrame.GetLength(0);
    
                //将文件头以及数据写到内存流里面
                using (MemoryStream stream = new MemoryStream(length + 54))
                {
                    using (BinaryWriter bw = new BinaryWriter(stream, Encoding.Default))
                    {
                        bw.Write('B');
                        bw.Write('M');
                        bw.Write(bytePerLine * height + 54);
                        bw.Write(0);
                        bw.Write(54);
                        bw.Write(40);
                        bw.Write(width);
                        bw.Write(height);
                        bw.Write((ushort)1);
                        bw.Write((ushort)24);
                        bw.Write(0);
                        bw.Write(bytePerLine * height);
                        bw.Write(0);
                        bw.Write(0);
                        bw.Write(0);
                        bw.Write(0);
    
                        byte[] data = new byte[bytePerLine * height];
                        int gIndex = width * height;
                        int bIndex = gIndex * 2;
                        for (int y = height - 1, j = 0; y >= 0; y--, j++)
                        {
                            for (int x = 0, i = 0; x < width; x++)
                            {
                                data[y * bytePerLine + i++] = rgbFrame[bIndex + j * width + x];
                                // B                    
                                data[y * bytePerLine + i++] = rgbFrame[gIndex + j * width + x];
                                // G          
                                data[y * bytePerLine + i++] = rgbFrame[j * width + x];
                                // R          
                            }
                        }
    
                        bw.Write(data, 0, data.Length);
                        Bitmap image = new Bitmap(stream);
                        this.pictureBox2.Image = image;
                        stream.Flush();
                        stream.Close();
                        bw.Flush();
                        bw.Close();
    
                    }
                }
            }
  • 相关阅读:
    《剑指offer》第六十八题(树中两个结点的最低公共祖先)
    《剑指offer》第六十七题(把字符串转换成整数)
    《剑指offer》第六十六题(构建乘积数组)
    《剑指offer》第六十五题(不用加减乘除做加法)
    ECShop 2.7.2版本,数据库表
    织梦在导航栏下拉菜单中调用当前栏目子类的方法
    让dedecms autoindex,itemindex 从0到1开始的办法!
    dedeCMS列表页中如何给前几条文章加单独样式?
    dedecms标签调用大全
    完美解决ecshop与jquery冲突兼容
  • 原文地址:https://www.cnblogs.com/jhlong/p/5433831.html
Copyright © 2011-2022 走看看