Class ImageScaler

java.lang.Object
io.leandev.appfuse.image.ImageScaler

public class ImageScaler extends Object

圖片縮放工具類

提供多種縮放策略:

  • scale - 縮放到指定尺寸(可放大或縮小)
  • shrink - 只縮小不放大,避免圖片模糊
  • scaleByWidth/Height - 依單一維度等比縮放

所有縮放操作都保持原圖長寬比

  • Constructor Details

    • ImageScaler

      public ImageScaler()
  • Method Details

    • shrinkByWidth

      public static BufferedImage shrinkByWidth(BufferedImage bufferedImage, int width)
      依寬度縮小圖片(只在需要時縮小)
      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度
      Returns:
      縮小後的圖片,如果原圖寬度小於目標寬度則返回原圖
    • shrinkByHeight

      public static BufferedImage shrinkByHeight(BufferedImage bufferedImage, int height)
      依高度縮小圖片(只在需要時縮小)
      Parameters:
      bufferedImage - 原始圖片
      height - 目標高度
      Returns:
      縮小後的圖片,如果原圖高度小於目標高度則返回原圖
    • shrink

      public static BufferedImage shrink(BufferedImage bufferedImage, Integer width, Integer height)
      縮小圖片到指定範圍內(只在需要時縮小)
      Parameters:
      bufferedImage - 原始圖片
      width - 最大寬度(null 表示不限制)
      height - 最大高度(null 表示不限制)
      Returns:
      縮小後的圖片
    • shrink

      public static BufferedImage shrink(BufferedImage bufferedImage, int width, int height)
      縮小圖片到指定範圍內(只在需要時縮小)
      Parameters:
      bufferedImage - 原始圖片
      width - 最大寬度
      height - 最大高度
      Returns:
      縮小後的圖片,如果原圖在範圍內則返回原圖
    • scaleByWidth

      public static BufferedImage scaleByWidth(BufferedImage bufferedImage, int width)
      依寬度等比縮放圖片
      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度
      Returns:
      縮放後的圖片
    • scaleByHeight

      public static BufferedImage scaleByHeight(BufferedImage bufferedImage, int height)
      依高度等比縮放圖片
      Parameters:
      bufferedImage - 原始圖片
      height - 目標高度
      Returns:
      縮放後的圖片
    • scale

      public static BufferedImage scale(BufferedImage bufferedImage, Integer width, Integer height)
      縮放圖片到指定範圍內(保持長寬比)
      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度(null 表示不限制)
      height - 目標高度(null 表示不限制)
      Returns:
      縮放後的圖片
    • scale

      public static BufferedImage scale(BufferedImage bufferedImage, int width, int height)

      縮放圖片到指定範圍內(保持長寬比,類似 CSS object-fit: contain)

      圖片會縮放至完全顯示在指定範圍內,可能不會填滿整個範圍

      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度
      height - 目標高度
      Returns:
      縮放後的圖片
    • fill

      public static BufferedImage fill(BufferedImage bufferedImage, int width, int height)

      縮放圖片到精確尺寸(可能變形,類似 CSS object-fit: fill)

      圖片會拉伸或壓縮以完全填滿指定尺寸,不保持長寬比

      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度
      height - 目標高度
      Returns:
      縮放後的圖片
    • scaleByWidth

      public static BufferedImage scaleByWidth(BufferedImage bufferedImage, Length width, int ppi)
      依寬度等比縮放圖片(支援物理單位)
      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度
      ppi - 解析度
      Returns:
      縮放後的圖片
    • scaleByWidth

      public static BufferedImage scaleByWidth(BufferedImage bufferedImage, Length width)
      依寬度等比縮放圖片(使用預設 300 PPI)
      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度
      Returns:
      縮放後的圖片
    • scaleByHeight

      public static BufferedImage scaleByHeight(BufferedImage bufferedImage, Length height, int ppi)
      依高度等比縮放圖片(支援物理單位)
      Parameters:
      bufferedImage - 原始圖片
      height - 目標高度
      ppi - 解析度
      Returns:
      縮放後的圖片
    • scaleByHeight

      public static BufferedImage scaleByHeight(BufferedImage bufferedImage, Length height)
      依高度等比縮放圖片(使用預設 300 PPI)
      Parameters:
      bufferedImage - 原始圖片
      height - 目標高度
      Returns:
      縮放後的圖片
    • scale

      public static BufferedImage scale(BufferedImage bufferedImage, Length width, Length height, int ppi)
      縮放圖片到指定尺寸(支援物理單位)
      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度
      height - 目標高度
      ppi - 解析度
      Returns:
      縮放後的圖片
    • scale

      public static BufferedImage scale(BufferedImage bufferedImage, Length width, Length height)
      縮放圖片到指定尺寸(使用預設 300 PPI)
      Parameters:
      bufferedImage - 原始圖片
      width - 目標寬度
      height - 目標高度
      Returns:
      縮放後的圖片