BorderDetectionResult.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using SixLabors.ImageSharp.PixelFormats;
  2. namespace Masuit.Tools.Media;
  3. /// <summary>
  4. /// 边框检测结果(包含多层边框信息)
  5. /// </summary>
  6. public struct BorderDetectionResult
  7. {
  8. private int CroppedBorderCount { get; set; }
  9. public BorderDetectionResult(int croppedBorderCount)
  10. {
  11. CroppedBorderCount = croppedBorderCount;
  12. }
  13. /// <summary>原始图片宽度</summary>
  14. public int ImageWidth { get; set; }
  15. /// <summary>原始图片高度</summary>
  16. public int ImageHeight { get; set; }
  17. /// <summary>内容上边界位置</summary>
  18. public int ContentTop { get; set; }
  19. /// <summary>内容下边界位置</summary>
  20. public int ContentBottom { get; set; }
  21. /// <summary>内容左边界位置</summary>
  22. public int ContentLeft { get; set; }
  23. /// <summary>内容右边界位置</summary>
  24. public int ContentRight { get; set; }
  25. /// <summary>检测到的边框层数</summary>
  26. public int BorderLayers { get; set; }
  27. /// <summary>边框颜色层次(从外到内)</summary>
  28. public List<Rgba32> BorderColors { get; set; }
  29. /// <summary>顶部边框总宽度(像素)</summary>
  30. public int TopBorderWidth => ContentTop;
  31. /// <summary>底部边框总宽度(像素)</summary>
  32. public int BottomBorderWidth => ImageHeight - 1 - ContentBottom;
  33. /// <summary>左侧边框总宽度(像素)</summary>
  34. public int LeftBorderWidth => ContentLeft;
  35. /// <summary>右侧边框总宽度(像素)</summary>
  36. public int RightBorderWidth => ImageWidth - 1 - ContentRight;
  37. /// <summary>是否有顶部边框</summary>
  38. public bool HasTopBorder => TopBorderWidth > 0;
  39. /// <summary>是否有底部边框</summary>
  40. public bool HasBottomBorder => BottomBorderWidth > 0;
  41. /// <summary>是否有左侧边框</summary>
  42. public bool HasLeftBorder => LeftBorderWidth > 0;
  43. /// <summary>是否有右侧边框</summary>
  44. public bool HasRightBorder => RightBorderWidth > 0;
  45. /// <summary>是否有任意边框</summary>
  46. public bool HasAnyBorder => BorderCount > 0;
  47. /// <summary>是否满足裁剪条件(至少两个边)</summary>
  48. public bool CanBeCropped => BorderCount >= CroppedBorderCount;
  49. public int BorderCount => (HasTopBorder ? 1 : 0) + (HasBottomBorder ? 1 : 0) + (HasLeftBorder ? 1 : 0) + (HasRightBorder ? 1 : 0);
  50. /// <summary>内容区域宽度</summary>
  51. public int ContentWidth => ContentRight - ContentLeft + 1;
  52. /// <summary>内容区域高度</summary>
  53. public int ContentHeight => ContentBottom - ContentTop + 1;
  54. }