Unity 代码集锦之图片处理
生活随笔
收集整理的這篇文章主要介紹了
Unity 代码集锦之图片处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、將Texture2D保存為jpg
void TestSaveImageToJPG(Texture2D buffer){File.WriteAllBytes("F:/output.jpg", buffer.EncodeToJPG());}?
2、將圖片的原始數據保存至txt
void TestSaveImageToTXT(Texture2D buffer){byte[] rawData= buffer.GetRawTextureData();//byte[] bt = ChangeRGB(rawData).GetRawTextureData();//轉換RGB//char[] ch = Encoding.ASCII.GetChars(bt);//byte[]轉為char[]StreamWriter sw = new StreamWriter(@"F:\\output.txt"); //保存到指定路徑for (int i = 0; i < rawData.Length; i++){sw.Write(rawData[i]);sw.Write(' ');}sw.Flush();sw.Close();}
?
3、轉換圖片的RGB
Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);for (int i = 0; i < result.height; ++i){for (int j = 0; j < result.width; ++j){Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);float temp = newColor.r;newColor.r = newColor.b;newColor.b = temp;result.SetPixel(j, i, newColor);}}result.Apply();return result;}
對于大多數紋理,更快的是使用GetPixels32,它返回低精度顏色數據,而無需進行昂貴的整數到浮點轉換。其中Color數組是Texture2D從左到右,從下到上的像素
Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);for (int i = 0; i < result.height; ++i){for (int j = 0; j < result.width; ++j){result.SetPixel(j, result.height - i, source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height));}}result.Apply();return result;}
下面代碼和上面代碼功能是一樣的,都是將圖片上下并鏡像翻轉,但是速度卻快了近一倍
Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);Color32[] sourceColor = source.GetPixels32();Color32[] newColor = new Color32[sourceColor.Length];for (int i = 0; i < result.height; ++i){for (int j = 0; j < result.width; ++j){newColor[(result.width * (result.height - i - 1)) + j] = sourceColor[(result.width * i) + j];}}result.SetPixels32(newColor);result.Apply();return result;}
或
Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);Color32[] sourceColor = source.GetPixels32();Color32[] newColor = new Color32[sourceColor.Length];int currentR = 0;//當前的行int currentC = 0;//當前的列for (int i = 0; i < sourceColor.Length; i++){if (i % result.width == 0){currentR = i / result.width;currentC = 0;}else{currentC++;}newColor[(result.width * (result.height - currentR - 1)) + currentC] = sourceColor[(result.width * currentR) + currentC];}result.SetPixels32(newColor);result.Apply();return result;}
附:
//順時針旋轉90度Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D( source.height, source.width, TextureFormat.RGB24, false);Color32[] sourceColor = source.GetPixels32();Color32[] newColor = new Color32[sourceColor.Length];for (int i = 0; i < source.height; ++i){for (int j = 0; j < source.width; ++j){newColor[(source.height * (source.width - 1-j)) + i] = sourceColor[(source.width * i) + j];}}result.SetPixels32(newColor);result.Apply();return result;}//逆時針旋轉90度Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D( source.height, source.width, TextureFormat.RGB24, false);Color32[] sourceColor = source.GetPixels32();Color32[] newColor = new Color32[sourceColor.Length];for (int i = 0; i < source.height; ++i){for (int j = 0; j < source.width; ++j){newColor[(source.height * j) + (source.height-1-i)] = sourceColor[(source.width * i) + j];}}result.SetPixels32(newColor);result.Apply();return result;}
4、將Texture轉為Texture2D,參數可傳入Texture也可傳入WebCamTexture類型的參數,和上述的“1”配合使用,可將攝像頭的數據保存為圖片
Texture2D TextureToTexture2D(Texture texture){Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);RenderTexture currentRT = RenderTexture.active;RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);Graphics.Blit(texture, renderTexture);RenderTexture.active = renderTexture;texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);texture2D.Apply();RenderTexture.active = currentRT;RenderTexture.ReleaseTemporary(renderTexture);return texture2D;}
?5、屏幕截圖
Texture2D frameBuffer;Rect camRect;frameBuffer = new Texture2D(width, height, TextureFormat.RGB24, false, false);camRect = new Rect(0, 0, width, height);frameBuffer.ReadPixels(camRect, 0, 0);frameBuffer.Apply();
轉載于:https://www.cnblogs.com/Jason-c/p/10812805.html
總結
以上是生活随笔為你收集整理的Unity 代码集锦之图片处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯阿里美团相继搞事,渣本程序员的出路在
- 下一篇: 你只会用 StringBuilder?试