当前位置:   article > 正文

Unity Texture2D的裁剪、镜像、翻转、缩放、合并、分辨率_unity 缩图合成

unity 缩图合成

本人最近做了一个拍照并打印的程序,使用到了多种图片编辑功能,现在罗列一下,希望对大家有所帮助。

裁剪,将贴图上的某个区域裁剪 

  1. /// <summary>
  2. /// 裁剪Texture2D
  3. /// </summary>
  4. /// <param name="originalTexture"></param>
  5. /// <param name="offsetX"></param>
  6. /// <param name="offsetY"></param>
  7. /// <param name="originalWidth"></param>
  8. /// <param name="originalHeight"></param>
  9. /// <returns></returns>
  10. public static Texture2D ScaleTextureCutOut(Texture2D originalTexture, int offsetX,int offsetY, float originalWidth, float originalHeight)
  11. {
  12. Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalWidth), Mathf.CeilToInt(originalHeight));
  13. int maxX = originalTexture.width - 1;
  14. int maxY = originalTexture.height - 1;
  15. for (int y = 0; y < newTexture.height; y++)
  16. {
  17. for (int x = 0; x < newTexture.width; x++)
  18. {
  19. float targetX = x + offsetX;
  20. float targetY = y + offsetY;
  21. int x1 = Mathf.Min(maxX, Mathf.FloorToInt(targetX));
  22. int y1 = Mathf.Min(maxY, Mathf.FloorToInt(targetY));
  23. int x2 = Mathf.Min(maxX, x1 + 1);
  24. int y2 = Mathf.Min(maxY, y1 + 1);
  25. float u = targetX - x1;
  26. float v = targetY - y1;
  27. float w1 = (1 - u) * (1 - v);
  28. float w2 = u * (1 - v);
  29. float w3 = (1 - u) * v;
  30. float w4 = u * v;
  31. Color color1 = originalTexture.GetPixel(x1, y1);
  32. Color color2 = originalTexture.GetPixel(x2, y1);
  33. Color color3 = originalTexture.GetPixel(x1, y2);
  34. Color color4 = originalTexture.GetPixel(x2, y2);
  35. Color color = new Color(Mathf.Clamp01(color1.r * w1 + color2.r * w2 + color3.r * w3 + color4.r * w4),
  36. Mathf.Clamp01(color1.g * w1 + color2.g * w2 + color3.g * w3 + color4.g * w4),
  37. Mathf.Clamp01(color1.b * w1 + color2.b * w2 + color3.b * w3 + color4.b * w4),
  38. Mathf.Clamp01(color1.a * w1 + color2.a * w2 + color3.a * w3 + color4.a * w4)
  39. );
  40. newTexture.SetPixel(x, y, color);
  41. }
  42. }
  43. newTexture.anisoLevel = 2;
  44. newTexture.Apply();
  45. return newTexture;
  46. }

 缩放,缩放和放大原有贴图

  1. /// <summary>
  2. /// 缩放Textur2D
  3. /// </summary>
  4. /// <param name="source"></param>
  5. /// <param name="targetWidth"></param>
  6. /// <param name="targetHeight"></param>
  7. /// <returns></returns>
  8. public static Texture2D ScaleTexture(Texture2D source, float targetWidth, float targetHeight)
  9. {
  10. Texture2D result = new Texture2D((int)targetWidth, (int)targetHeight, source.format, false);
  11. float incX = (1.0f / targetWidth);
  12. float incY = (1.0f / targetHeight);
  13. for (int i = 0; i < result.height; ++i)
  14. {
  15. for (int j = 0; j < result.width; ++j)
  16. {
  17. Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
  18. result.SetPixel(j, i, newColor);
  19. }
  20. }
  21. result.Apply();
  22. return result;
  23. }

 水平镜像

  1. //水平翻转
  2. public static Texture2D HorizontalFlipTexture(Texture2D texture)
  3. {
  4. //得到图片的宽高
  5. int width = texture.width;
  6. int height = texture.height;
  7. Texture2D flipTexture = new Texture2D(width, height);
  8. for (int i = 0; i < width; i++)
  9. {
  10. flipTexture.SetPixels(i, 0, 1, height, texture.GetPixels(width - i - 1, 0, 1, height));
  11. }
  12. flipTexture.Apply();
  13. return flipTexture;
  14. }

 垂直镜像

  1. // 垂直翻转
  2. public static Texture2D VerticalFlipTexture(Texture2D texture)
  3. {
  4. //得到图片的宽高
  5. int width = texture.width;
  6. int height = texture.height;
  7. Texture2D flipTexture = new Texture2D(width, height);
  8. for (int i = 0; i < height; i++)
  9. {
  10. flipTexture.SetPixels(0, i, width, 1, texture.GetPixels(0, height - i - 1, width, 1));
  11. }
  12. flipTexture.Apply();
  13. return flipTexture;
  14. }

 逆时针旋转90度

  1. /// <summary>
  2. /// 图片逆时针旋转90
  3. /// </summary>
  4. /// <param name="src">原图片二进制数据</param>
  5. /// <param name="srcW">原图片宽度</param>
  6. /// <param name="srcH">原图片高度</param>
  7. /// <param name="desTexture">输出目标图片</param>
  8. public static Texture2D RotationLeft90(Color32[] src, int srcW, int srcH)
  9. {
  10. Color32[] des = new Color32[src.Length];
  11. Texture2D desTexture = new Texture2D(srcH, srcW);
  12. //if (desTexture.width != srcH || desTexture.height != srcW)
  13. //{
  14. // desTexture.Resize(srcH, srcW);
  15. //}
  16. for (int i = 0; i < srcW; i++)
  17. {
  18. for (int j = 0; j < srcH; j++)
  19. {
  20. des[i * srcH + j] = src[(srcH - 1 - j) * srcW + i];
  21. }
  22. }
  23. desTexture.SetPixels32(des);
  24. desTexture.Apply();
  25. return desTexture;
  26. }

顺时针旋转90度 

  1. /// <summary>
  2. /// 图片顺时针旋转90
  3. /// </summary>
  4. /// <param name="src">原图片二进制数据</param>
  5. /// <param name="srcW">原图片宽度</param>
  6. /// <param name="srcH">原图片高度</param>
  7. /// <param name="desTexture">输出目标图片</param>
  8. public static Texture2D RotationRight90(Color32[] src, int srcW, int srcH)
  9. {
  10. Color32[] des = new Color32[src.Length];
  11. Texture2D desTexture = new Texture2D(srcH, srcW);
  12. for (int i = 0; i < srcH; i++)
  13. {
  14. for (int j = 0; j < srcW; j++)
  15. {
  16. des[(srcW - j - 1) * srcH + i] = src[i * srcW + j];
  17. }
  18. }
  19. desTexture.SetPixels32(des);
  20. desTexture.Apply();
  21. return desTexture;
  22. }

两张贴图合并,可以实现水印等功能,该代码是我实现3行3列9张相同照片排列 

  1. /// <summary>
  2. /// 两张图合并
  3. /// </summary>
  4. /// <param name="_baseTexture2D"></param>
  5. /// <param name="_texture2D"></param>
  6. /// <param name="_x"></param>
  7. /// <param name="_y"></param>
  8. /// <param name="_w"></param>
  9. /// <param name="_h"></param>
  10. /// <returns></returns>
  11. public static Texture2D MergeImage(Texture2D _baseTexture2D, Texture2D _texture2D,int _x,int _y,int _w,int _h)
  12. {
  13. //取图
  14. Color32[] color = _texture2D.GetPixels32(0);
  15. for (int j = 0; j < 3; j++)
  16. {
  17. for (int i = 0; i < 3; i++)
  18. {
  19. _baseTexture2D.SetPixels32(_x + i * (_texture2D.width+ _w), _y + j * (_texture2D.height+_h), _texture2D.width, _texture2D.height, color); //宽度
  20. }
  21. }
  22. //应用
  23. _baseTexture2D.Apply();
  24. return _baseTexture2D;
  25. }

修改图片的水平和垂直分辨率(像素/英寸,每英寸距离的图像包含的像素数目。)

Bitmap操作需要用到System.Drawing.dll,请自行下载!

  1. /// <summary>
  2. /// 修改图片水平和垂直像素量
  3. /// </summary>
  4. public static Texture2D SetPictureResolution(string _path)
  5. {
  6. Bitmap bm = new Bitmap(_path);
  7. bm.SetResolution(300, 300);
  8. string _idPath = Application.persistentDataPath + "/";
  9. string _name = "print.jpg";
  10. bm.Save(_idPath + _name, ImageFormat.Jpeg);
  11. Texture2D tex = loadTexture(_idPath, _name);
  12. File.WriteAllBytes(Application.persistentDataPath + "/aa.jpg", tex.EncodeToJPG());
  13. bm.Dispose();
  14. return tex;
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/107924
推荐阅读
相关标签
  

闽ICP备14008679号