• Unity 压缩texture


    当我们往服务器保存图片时

    并不会仅仅保留原图

    一般会另外保存一张缩略图

    当加载文件夹时只加载缩略图

    当在点击缩略图打开图片时

    再加载原缩略图

    以节省时间和内存

    下面以将屏幕截图保存到服务器为例

    将屏幕截图和截图缩略图保存到服务器

        private IEnumerator UploadPNG(string id)
        {
            yield return new WaitForEndOfFrame();
    
            int width = Screen.width;
            int height = Screen.height;
    
            //屏幕截图
            Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
            tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            tex.Apply();
            byte[] bytes = tex.EncodeToPNG();
    
            //缩略图(256*256)
            Texture2D texThumb = new Texture2D(256, 256);
            
            //压缩图片
            Color[] destPix = new Color[texThumb.width * texThumb.height];
            float warpFactor = 1.0f;
            int y = 0;
            while (y < texThumb.height)
            {
                int x = 0;
                while (x < texThumb.width)
                {
                    float xFrac = x * 1.0F / (texThumb.width - 1);
                    float yFrac = y * 1.0F / (texThumb.height - 1);
                    float warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    float warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c0 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    xFrac = (x - 1) * 1.0F / (texThumb.width - 1);
                    yFrac = y * 1.0F / (texThumb.height - 1);
                    warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c1 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    xFrac = (x + 1) * 1.0F / (texThumb.width - 1);
                    yFrac = y * 1.0F / (texThumb.height - 1);
                    warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c2 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    xFrac = (x + 1) * 1.0F / (texThumb.width - 1);
                    yFrac = (y - 1) * 1.0F / (texThumb.height - 1);
                    warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c3 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    xFrac = (x - 0) * 1.0F / (texThumb.width - 1);
                    yFrac = (y - 1) * 1.0F / (texThumb.height - 1);
                    warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c4 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    xFrac = (x - 1) * 1.0F / (texThumb.width - 1);
                    yFrac = (y - 1) * 1.0F / (texThumb.height - 1);
                    warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c5 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    xFrac = (x + 1) * 1.0F / (texThumb.width - 1);
                    yFrac = (y + 1) * 1.0F / (texThumb.height - 1);
                    warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c6 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    xFrac = (x - 0) * 1.0F / (texThumb.width - 1);
                    yFrac = (y + 1) * 1.0F / (texThumb.height - 1);
                    warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c7 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    xFrac = (x - 1) * 1.0F / (texThumb.width - 1);
                    yFrac = (y + 1) * 1.0F / (texThumb.height - 1);
                    warpXFrac = Mathf.Pow(xFrac, warpFactor);
                    warpYFrac = Mathf.Pow(yFrac, warpFactor);
                    Color c8 = tex.GetPixelBilinear(warpXFrac, warpYFrac);
    
                    Color cr = c0 * 0.25f + c1 * 0.125f + c2 * 0.125f + c3 * 0.0625f + c4 * 0.125f + c5 * 0.0625f + c6 * 0.0625f + c7 * 0.125f + c8 * 0.0625f;
    
                    destPix[y * texThumb.width + x] = cr;
    
                    x++;
                }
                y++;
            }
            texThumb.SetPixels(destPix);
            texThumb.Apply();
            byte[] bytesThumb = texThumb.EncodeToPNG();
    Destroy(tex); Destroy(texThumb);
    //WWWForm上传到服务器 WWWForm form = new WWWForm(); form.AddField("cmd", "savedraw"); form.AddBinaryData("screenshot", bytesThumb); form.AddBinaryData("draw", bytes); WWW w = new WWW(url, form); yield return w; if (w.error != null) { Debug.Log("Error:" + w.error); } else Debug.Log("OK"); }

    声明:此博客为个人学习之用,如与其他作品雷同,纯属巧合,并请明示指出
  • 相关阅读:
    HDU1013 Digital Roots
    DP---背包问题
    新生代与老年代
    JVM常见问题(二)
    JVM常见问题 一(转载)
    JVM内存模型及分区
    (转载)JVM知识小集
    类加载机制:全盘负责和双亲委托
    mysql 函数GROUP_CONCAT(temp.amount SEPARATOR ',')的用法
    mysql优化--explain关键字
  • 原文地址:https://www.cnblogs.com/fws94/p/6288481.html
Copyright © 2020-2023  润新知