604
汽車大全
yuv420 轉bmp的方法
yuv420 轉bmp的方法:我找到了一個c寫的exe ,但是c的水平我實在是不敢去修改那個源碼,所以我幹脆就調用這個dll,在c#裏建立了下麵這個類, 然後調用 Y2b.exe 來轉換, 而且不會顯示出來那個黑屏.要知道怎麼回事,就看代碼吧!嗬嗬.
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.IO;
namespace JXImage
{
public class YUV2BMP
{
/// <summary>
/// 檢查程序配置是否正常, 比如exe路徑,圖片文件夾等. 一般情況下設置了路徑和圖片文件路徑以及圖片後,應該使用屬性取得是否可以執行命令.
/// </summary>
public bool CanUse
{
get
{
if (System.IO.File.Exists(System.Windows.Forms.Application.StartupPath + "//Y2B.exe") )
{
return true;
}
return false;
}
}
private string bmpfile = null;
/// <summary>
/// BMP文件名, 在成功執行轉換後可以用此獲得文件路徑..
/// </summary>
/// <returns>返回文件名</returns>
public string GetBmpFileName()
{
return bmpfile;
}
/// <summary>
/// BMP數據流,成功轉換後可以從此獲得流 .
/// </summary>
/// <returns>數據流形式的BMP </returns>
public MemoryStream GetBmpFileStream()
{
try
{
return new MemoryStream(System.IO.File.ReadAllBytes(bmpfile));
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// Image 成功轉換後可以用此來獲得文件.
/// </summary>
/// <returns>返回一個圖片</returns>
public Image GetBmp()
{
try
{
return Image.FromFile(bmpfile);
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// YUV轉BMP
/// </summary>
/// <param name="yuvfilename">YUV文件名</param>
/// <returns>返回是否成功</returns>
public bool YUVtoBMP(string yuvfilename)
{
if (CanUse)
{
string ret = Execute(yuvfilename);
if (ret.IndexOf("OK")>=0)//從0字符開始會有個OK
{
if (System.IO.File.Exists(yuvfilename.Replace(".yuv", ".bmp")))
{
bmpfile = yuvfilename.Replace(".yuv", ".bmp");
return true;
}
}
}
return false;
}
/// <summary>
/// 執行DOS命令.
/// </summary>
/// <param name="canmand">命令</param>
/// <returns>返回執行結果 </returns>
public static string Execute(string yuvfilename)
{
int milliseconds = 2 * 1000;//兩秒
string output = ""; //輸出字符串
if (yuvfilename != null && yuvfilename != "")
{
Process process = new Process(); //創建進程對象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName =System.Windows.Forms.Application.StartupPath +"//Y2B.exe" ; //設定需要執行的命令
startInfo.Arguments = "/h352 /v288 /""+yuvfilename+"/" /"" +yuvfilename.Replace(".yuv",".bmp"); //設定參數,其中的“/C”表示執行完命令後馬上退出
startInfo.UseShellExecute = false; //不使用係統外殼程序啟動
startInfo.RedirectStandardInput = false; //不重定向輸入
startInfo.RedirectStandardOutput = true; //重定向輸出
startInfo.CreateNoWindow = true; //不創建窗口
process.StartInfo = startInfo;
try
{
if (process.Start()) //開始進程
{
if (milliseconds == 0)
process.WaitForExit(); //這裏無限等待進程結束
else
process.WaitForExit(milliseconds); //這裏等待進程結束,等待時間為指定的毫秒
output = process.StandardOutput.ReadToEnd();//讀取進程的輸出
}
}
catch
{
}
finally
{
if (process != null)
process.Close();
}
return output;
}
return null;
}
}
}

如果你要實現更多YUV到BMP的轉換,請使用
dirac-0.7.0 sf上可以搜到哦 . google裏也可以的. 我就不上傳了.
基本支持所有YUV方麵的轉換.
不過全部是exe方式的, 如果你打開win32目錄的話,支持vs2003和vs2005 ,也支持其他c++編輯器
如果不會c,那就用我上麵的方法弄就行了. 嗬嗬 .
最後更新:2017-04-02 00:06:24