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(); 

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM