Class ImageUtil
java.lang.Object
com.sure.tool.image.ImageUtil
图像工具类:基于 JDK
ImageIO 的读写、缩放、裁剪、灰度、旋转、格式转换,零依赖。- Since:
- 0.2.0
- Author:
- suretool
-
Method Summary
Modifier and TypeMethodDescriptionstatic BufferedImagecrop(BufferedImage source, int x, int y, int width, int height) 裁剪图像。static BufferedImagegray(BufferedImage source) 转为灰度图。static BufferedImage读取图像文件。static BufferedImagerotate(BufferedImage source, int degrees) 旋转图像(90 度整数倍)。static BufferedImagescale(BufferedImage source, int width, int height) 缩放图像(保持宽高比,按目标宽度计算高度;quality 高质量插值)。static Stringsize(BufferedImage image) 图像尺寸信息(宽 x 高)。static StringtoBase64(BufferedImage image, String format) 将图片编码为 Base64 字符串。static StringtoDataUri(BufferedImage image, String format) 将图片编码为 Data URI(可直接用于 <img src>)。static voidwrite(BufferedImage image, Path path, String format) 写入图像到文件(按扩展名推断格式,如 png/jpg)。
-
Method Details
-
read
读取图像文件。- Parameters:
path- 图像文件路径- Returns:
- 图像;文件不是有效图像返回 null
- Throws:
IOException- IO 异常
-
write
写入图像到文件(按扩展名推断格式,如 png/jpg)。- Parameters:
image- 图像path- 目标路径format- 格式(png/jpg/bmp/gif)- Throws:
IOException- IO 异常
-
scale
缩放图像(保持宽高比,按目标宽度计算高度;quality 高质量插值)。- Parameters:
source- 源图像width- 目标宽度height- 目标高度- Returns:
- 缩放后图像
-
crop
裁剪图像。- Parameters:
source- 源图像x- 起始 Xy- 起始 Ywidth- 宽度height- 高度- Returns:
- 裁剪后图像
-
gray
-
rotate
旋转图像(90 度整数倍)。- Parameters:
source- 源图像degrees- 旋转角度(90 的倍数)- Returns:
- 旋转后图像
-
size
图像尺寸信息(宽 x 高)。- Parameters:
image- 图像- Returns:
- 如
1920x1080
-
toBase64
将图片编码为 Base64 字符串。- Parameters:
image- 图片format- 格式(png/jpg 等)- Returns:
- Base64 字符串
- Throws:
IOException- IO 异常
-
toDataUri
将图片编码为 Data URI(可直接用于 <img src>)。- Parameters:
image- 图片format- 格式(png/jpg 等)- Returns:
data:image/png;base64,...- Throws:
IOException- IO 异常
-