Unity 圖片視訊讀取類
阿新 • • 發佈:2018-12-20
using QFramework; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; public class FileLoad : MonoSingleton<FileLoad> { public static string DirectoryPath = "D:\\ManagerSysteam\\Images\\"; public static string oldVideoName = "shabi"; private FileLoad(){ } /// <summary> /// 以IO方式進行載入 /// </summary> public void LoadByIO(string fileName, RawImage rawImage) { string path = DirectoryPath + fileName; if (File.Exists(path)) { print("-------------->" + path); //建立檔案讀取流 "D:\\test.jpg" FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); fileStream.Seek(0, SeekOrigin.Begin); //建立檔案長度緩衝區 byte[] bytes = new byte[fileStream.Length]; //讀取檔案 fileStream.Read(bytes, 0, (int)fileStream.Length); //釋放檔案讀取流 fileStream.Close(); fileStream.Dispose(); fileStream = null; //建立Texture Texture2D texture = new Texture2D(Screen.width, Screen.height); texture.LoadImage(bytes); rawImage.texture = texture; //建立Sprite // Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); // image.sprite = sprite; } } /// <summary> /// 根據傳進來的檔名,返回字尾名 /// </summary> /// <param name="name"></param> /// <returns></returns> public void LoadImageByName(string name , RawImage rawImage) { string path = DirectoryPath + name; if (File.Exists(path + ".jpg")) { string FullPath = path + ".jpg"; loadImage(FullPath, rawImage); }else if (File.Exists(path + ".png")) { string FullPath = path + ".png"; loadImage(FullPath, rawImage); } else if (File.Exists(path + ".jpeg")) { string FullPath = path + ".jpeg"; loadImage(FullPath, rawImage); } } void loadImage(string FullPath,RawImage rawImage) { FileStream fileStream = new FileStream(FullPath, FileMode.Open, FileAccess.Read); fileStream.Seek(0, SeekOrigin.Begin); //建立檔案長度緩衝區 byte[] bytes = new byte[fileStream.Length]; //讀取檔案 fileStream.Read(bytes, 0, (int)fileStream.Length); //釋放檔案讀取流 fileStream.Close(); fileStream.Dispose(); fileStream = null; //建立Texture Texture2D texture = new Texture2D(Screen.width, Screen.height); texture.LoadImage(bytes); rawImage.texture = texture; } /// <summary> /// 以IO方式進行載入 /// </summary> public void LoadByIO(string fileName, RawImage rawImage,int rawImageWidth,int rawImageHeight) { string path = DirectoryPath + fileName; //建立檔案讀取流 "D:\\test.jpg" FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); fileStream.Seek(0, SeekOrigin.Begin); //建立檔案長度緩衝區 byte[] bytes = new byte[fileStream.Length]; //讀取檔案 fileStream.Read(bytes, 0, (int)fileStream.Length); //釋放檔案讀取流 fileStream.Close(); fileStream.Dispose(); fileStream = null; //建立Texture Texture2D texture = new Texture2D(rawImageWidth, rawImageHeight); texture.LoadImage(bytes); rawImage.texture = texture; //建立Sprite // Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); // image.sprite = sprite; } /// <summary> /// 掃描檔案並載入 /// </summary> /// <param name="fileName"></param> /// <param name="rawImage"></param> public void scan(string fileName,RawImage rawImage) { string jpgPath = DirectoryPath + fileName + ".jpg"; // 改名字檔案存在mp4檔案 刪除以前的截圖 重新生成 if (File.Exists(DirectoryPath + fileName + ".mp4")) { // ffmpeg 截圖 FfmpegFun(jpgPath, DirectoryPath + fileName + ".mp4"); } else if (File.Exists(DirectoryPath + fileName + ".flv")) { FfmpegFun(jpgPath, DirectoryPath + fileName + ".flv"); } else if (File.Exists(DirectoryPath + fileName + ".mov")) { FfmpegFun(jpgPath, DirectoryPath + fileName + ".mov"); } else if (File.Exists(DirectoryPath + fileName + ".avi")) { FfmpegFun(jpgPath, DirectoryPath + fileName + ".avi"); } LoadImageByName(fileName, rawImage); //// 如果該檔案存在jpg格式 //if (File.Exists(jpgPath)) //{ // // 根據字尾名判斷是 視訊 還是 圖片 // string extensionName = Path.GetExtension(jpgPath); // if (extensionName == ".jpg" || extensionName == ".png") // { // LoadByIO(fileName+".jpg",rawImage); // } //} }//end scan /// <summary> /// 判斷一個資料夾下包含特殊字串檔名的數量 /// </summary> public int ScanNum(string str) { // string str = "B1-"; int num = 0; DirectoryInfo folder = new DirectoryInfo(DirectoryPath); foreach (FileInfo file in folder.GetFiles(str + "*" + ".jpg")) { num++; } foreach (FileInfo file in folder.GetFiles(str + "*" + ".jpeg")) { num++; } foreach (FileInfo file in folder.GetFiles(str + "*" + ".png")) { num++; } return num; } /// <summary> /// 是否存在檔案 /// </summary> /// <param name="name"></param> /// <returns></returns> public bool ExitFile(string name) { if (File.Exists(DirectoryPath+name)) { return true; } else { return false; } } /// <summary> /// fffmpegFun方式實現播放視訊 /// </summary> private void FfmpegFun(string jpgPath,string MP4Path) { // jpg圖片名字 string jpgTempName = jpgPath; // 如果圖片已存在 刪除 DeleteFile(jpgTempName); // 擷取視訊的預覽圖 string para = "-i " + MP4Path + " -y -f image2 -t 0.001 -s 300x180 " + jpgTempName; GetPicFromVideo(para); } /// <summary> /// 根據路徑刪除檔案 /// </summary> /// <param name="path"></param> public void DeleteFile(string path) { if (File.Exists(path)) { FileAttributes attr = File.GetAttributes(path); if (attr == FileAttributes.Directory) { Directory.Delete(path, true); } else { File.Delete(path); } } } public static string FFmpegPath = @"D:\ManagerSysteam\ffmpeg-20180902-93b35a0-win64-static\bin\ffmpeg.exe"; // 從視訊畫面中擷取一幀畫面為圖片 /// <summary> /// 從視訊畫面中擷取一幀畫面為圖片 /// </summary> /// <param name="VideoName">視訊檔案,絕對路徑</param> /// <param name="WidthAndHeight">圖片的尺寸如:240*180</param> /// <param name="CutTimeFrame">開始擷取的時間如:"1"</param> /// <returns></returns> public void GetPicFromVideo(string Parameters) { var p = new System.Diagnostics.Process(); p.StartInfo.FileName = FFmpegPath; p.StartInfo.Arguments = Parameters; //是否使用作業系統shell啟動 p.StartInfo.UseShellExecute = false; //不顯示程式視窗 p.StartInfo.CreateNoWindow = true; p.Start(); // Debug.Log("\n開始轉碼...\n"); p.WaitForExit(); p.Close(); // Debug.Log("\n轉碼完畢...\n"); } }