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

  

6、List根据某个属性去重

var listtg = listTg.GroupBy(c => c.Name).Select(c => c.First()).ToList();

  

posted on 2019-05-05 14:19  Jason_c  阅读(2423)  评论(0编辑  收藏  举报