/* 在WindowsMobile和WinForm上都可以,我测过了
* 这个不是我写的,我找了好几天才找到的,我只是改了几句话而已,不过效率不是很高,供大家参考吧
* 在这里谢谢 Galactica , 也希望大家有比这更好的方法给小弟共享一下...
* 文件下载地址:http://trace.eas.asu.edu/yuv/tempete.zip
* */
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace deCodeYUV
{
public partial class MainForm : Form
{
static double[,] YUV2RGB_CONVERT_MATRIX =
new double[3, 3] { { 1, 0, 1.4022 }, { 1, -0.3456, -0.7145 }, { 1, 1.771, 0 } };
public MainForm()
{
InitializeComponent();
GetByteData();
}
public void GetByteData()
{
int width = 352;
int height = 288;
int imgSize = width * height;
int frameSize = imgSize + (imgSize >> 1);
byte[] yuv = new byte[frameSize];
byte[] rgb = new byte[3 * imgSize];
//这里需要注意,这个是本地文件夹,是模拟器共享的文件夹或手机设备的文件夹
using (FileStream fs = File.OpenRead(@"\Storage Card\tempete.cif"))
{
int frame = (int)fs.Length / frameSize;
using (BinaryReader br = new BinaryReader(fs))
{
int index = 0;
while (br.PeekChar() != -1)
{
// 循环读取每一桢
br.Read(yuv, 0, frameSize);
//转换为 RGB
ConvertYUV2RGB(yuv, rgb, width, height);
// 写 BMP 文件。
WriteBMP(rgb, width, height);
index++;
}
}
}
}
/// <summary>
/// 将转换后的 RGB 图像数据按照 BMP 格式写入文件。
/// </summary>
/// <param name="rgbFrame">RGB 格式图像数据。</param>
/// <param name="width">图像宽(单位:像素)。</param>
/// <param name="height">">图像高(单位:像素)。</param>
/// <param name="bmpFile"> BMP 文件名。</param>
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.pictureBox1.Image = image;
stream.Flush();
stream.Close();
bw.Flush();
bw.Close();
}
}
}
/// <summary>
/// 将一桢 YUV 格式的图像转换为一桢 RGB 格式图像。
/// </summary>
/// <param name="yuvFrame">YUV 格式图像数据。</param>
/// <param name="rgbFrame">RGB 格式图像数据。</param>
/// <param name="width">图像宽(单位:像素)。</param>
/// <param name="height">图像高(单位:像素)。</param>
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));
}
}
}
}
}
转载于:https://www.cnblogs.com/shaodong/archive/2010/08/25/1807779.html