• 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"); }

    声明:此博客为个人学习之用,如与其他作品雷同,纯属巧合,并请明示指出
  • 相关阅读:
    2020-10-24;我对机器学习实现的理解
    2020软件工程作业03
    友链
    导航页
    POJ3694 Network (板子题)
    洛谷 P1084 [NOIP2012 提高组] 疫情控制
    AcWing 355. 异象石
    洛谷P4556 雨天的尾巴
    POJ1639 Picnic Planning (K度限制最小生成树)
    POJ2728 Desert King
  • 原文地址:https://www.cnblogs.com/fws94/p/6288481.html
Copyright © 2020-2023  润新知