Class Image
java.lang.Object
cloud.opencode.base.image.Image
Image
图片处理包装类
Chainable image processing wrapper with fluent API.
支持链式调用的图片处理包装类。
Usage Examples | 使用示例:
// Chain operations
Image.from(path)
.resize(800, 600)
.rotate(90)
.watermark(TextWatermark.of("Copyright"))
.save(outputPath);
// Get bytes
byte[] bytes = Image.from(inputStream)
.crop(100, 100, 400, 300)
.toBytes(ImageFormat.PNG);
Features | 主要功能:
- Chainable fluent API for image operations - 可链式调用的流式图片操作 API
- Resize, crop, rotate, watermark operations - 缩放、裁剪、旋转、水印操作
- Save to file or convert to byte array - 保存到文件或转换为字节数组
- Multiple format support (JPEG, PNG, GIF, BMP) - 多格式支持(JPEG、PNG、GIF、BMP)
Security | 安全性:
- Thread-safe: No (mutable internal state) - 线程安全: 否(可变内部状态)
- Null-safe: No (throws on null image) - 空值安全: 否(null 图片抛异常)
- Since:
- JDK 25, opencode-base-image V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionImage(BufferedImage image) Create image wrapper 创建图片包装器Image(BufferedImage image, ImageFormat format) Create image wrapper with format 创建带格式的图片包装器 -
Method Summary
Modifier and TypeMethodDescriptioncompress(float quality) Compress image with quality 按质量压缩图片convert(ImageFormat targetFormat) Convert to specified format 转换到指定格式copy()Create a copy of this image 创建此图片的副本Get a copy of the underlying BufferedImage 获取底层BufferedImage的副本crop(int x, int y, int width, int height) Crop to specified region 裁剪到指定区域cropCenter(int width, int height) Crop from center 从中心裁剪Crop to square from center 从中心裁剪为正方形Flip horizontally 水平翻转Flip vertically 垂直翻转format(ImageFormat format) Set the image format 设置图片格式static Imagefrom(byte[] bytes) Create image from bytes 从字节数组创建图片static Imagefrom(BufferedImage image) Create image from BufferedImage 从BufferedImage创建图片static ImageCreate image from path 从路径创建图片Get the underlying BufferedImage 获取底层BufferedImageGet the image format 获取图片格式intGet image height 获取图片高度getInfo()Get image info 获取图片信息intgetWidth()Get image width 获取图片宽度Convert to grayscale 转换为灰度图resize(int width, int height) Resize to exact dimensions 调整到精确尺寸resizeToFit(int maxWidth, int maxHeight) Resize maintaining aspect ratio 保持宽高比调整尺寸rotate(double degrees) Rotate by degrees 按角度旋转Rotate 180 degrees 旋转180度Rotate 270 degrees (90 counter-clockwise) 旋转270度(逆时针90度)rotate90()Rotate 90 degrees clockwise 顺时针旋转90度voidSave to path 保存到路径voidsave(Path path, ImageFormat format) Save to path with format 保存到路径(指定格式)scale(double scale) Scale by percentage 按百分比缩放scaleToHeight(int height) Scale to fit height 缩放以适应高度scaleToWidth(int width) Scale to fit width 缩放以适应宽度toBase64()Convert to Base64 string 转换为Base64字符串toBase64(ImageFormat format) Convert to Base64 string with format 转换为Base64字符串(指定格式)byte[]toBytes()Convert to byte array 转换为字节数组byte[]toBytes(ImageFormat format) Convert to byte array with format 转换为字节数组(指定格式)watermark(ImageWatermark watermark) Add image watermark 添加图片水印watermark(TextWatermark watermark) Add text watermark 添加文字水印watermark(BufferedImage watermarkImage, Position position) Add image watermark at position 在指定位置添加图片水印Add text watermark at position 在指定位置添加文字水印voidwriteTo(OutputStream out) Write to output stream 写入输出流voidwriteTo(OutputStream out, ImageFormat format) Write to output stream with format 写入输出流(指定格式)
-
Constructor Details
-
Image
Create image wrapper 创建图片包装器- Parameters:
image- the buffered image | 缓冲图片
-
Image
Create image wrapper with format 创建带格式的图片包装器- Parameters:
image- the buffered image | 缓冲图片format- the image format | 图片格式
-
-
Method Details
-
from
Create image from path 从路径创建图片- Parameters:
path- the image path | 图片路径- Returns:
- the image wrapper | 图片包装器
- Throws:
ImageIOException- if reading fails | 如果读取失败
-
from
Create image from bytes 从字节数组创建图片- Parameters:
bytes- the image bytes | 图片字节数组- Returns:
- the image wrapper | 图片包装器
- Throws:
ImageIOException- if reading fails | 如果读取失败
-
from
Create image from BufferedImage 从BufferedImage创建图片- Parameters:
image- the buffered image | 缓冲图片- Returns:
- the image wrapper | 图片包装器
-
getBufferedImage
Get the underlying BufferedImage 获取底层BufferedImageWarning: Returns the internal mutable reference. Modifications to the returned BufferedImage will affect this Image instance. Use
copyBufferedImage()if you need an independent copy.警告:返回内部可变引用。对返回的BufferedImage的修改将影响此Image实例。 如果需要独立副本,请使用
copyBufferedImage()。- Returns:
- the buffered image (mutable reference) | 缓冲图片(可变引用)
-
copyBufferedImage
Get a copy of the underlying BufferedImage 获取底层BufferedImage的副本Returns an independent copy that can be modified without affecting this Image instance.
返回一个可以修改而不影响此Image实例的独立副本。
- Returns:
- a copy of the buffered image | 缓冲图片的副本
-
getFormat
-
format
Set the image format 设置图片格式- Parameters:
format- the format | 格式- Returns:
- this image for chaining | 用于链式调用的图片
-
getWidth
public int getWidth()Get image width 获取图片宽度- Returns:
- the width in pixels | 宽度(像素)
-
getHeight
public int getHeight()Get image height 获取图片高度- Returns:
- the height in pixels | 高度(像素)
-
getInfo
-
resize
Resize to exact dimensions 调整到精确尺寸- Parameters:
width- target width | 目标宽度height- target height | 目标高度- Returns:
- this image for chaining | 用于链式调用的图片
-
resizeToFit
Resize maintaining aspect ratio 保持宽高比调整尺寸- Parameters:
maxWidth- maximum width | 最大宽度maxHeight- maximum height | 最大高度- Returns:
- this image for chaining | 用于链式调用的图片
-
scale
Scale by percentage 按百分比缩放- Parameters:
scale- scale factor (e.g., 0.5 for 50%) | 缩放因子(如0.5表示50%)- Returns:
- this image for chaining | 用于链式调用的图片
-
scaleToWidth
Scale to fit width 缩放以适应宽度- Parameters:
width- target width | 目标宽度- Returns:
- this image for chaining | 用于链式调用的图片
-
scaleToHeight
Scale to fit height 缩放以适应高度- Parameters:
height- target height | 目标高度- Returns:
- this image for chaining | 用于链式调用的图片
-
crop
Crop to specified region 裁剪到指定区域- Parameters:
x- the x coordinate | X坐标y- the y coordinate | Y坐标width- the crop width | 裁剪宽度height- the crop height | 裁剪高度- Returns:
- this image for chaining | 用于链式调用的图片
-
cropCenter
Crop from center 从中心裁剪- Parameters:
width- the crop width | 裁剪宽度height- the crop height | 裁剪高度- Returns:
- this image for chaining | 用于链式调用的图片
-
cropSquare
Crop to square from center 从中心裁剪为正方形- Returns:
- this image for chaining | 用于链式调用的图片
-
rotate
Rotate by degrees 按角度旋转- Parameters:
degrees- rotation degrees | 旋转角度- Returns:
- this image for chaining | 用于链式调用的图片
-
rotate90
Rotate 90 degrees clockwise 顺时针旋转90度- Returns:
- this image for chaining | 用于链式调用的图片
-
rotate180
-
rotate270
Rotate 270 degrees (90 counter-clockwise) 旋转270度(逆时针90度)- Returns:
- this image for chaining | 用于链式调用的图片
-
flipHorizontal
-
flipVertical
-
watermark
Add text watermark 添加文字水印- Parameters:
watermark- the text watermark | 文字水印- Returns:
- this image for chaining | 用于链式调用的图片
-
watermark
-
watermark
Add image watermark 添加图片水印- Parameters:
watermark- the image watermark | 图片水印- Returns:
- this image for chaining | 用于链式调用的图片
-
watermark
Add image watermark at position 在指定位置添加图片水印- Parameters:
watermarkImage- the watermark image | 水印图片position- the position | 位置- Returns:
- this image for chaining | 用于链式调用的图片
-
compress
Compress image with quality 按质量压缩图片- Parameters:
quality- quality from 0.0 to 1.0 | 质量(0.0到1.0)- Returns:
- this image for chaining | 用于链式调用的图片
-
convert
Convert to specified format 转换到指定格式- Parameters:
targetFormat- the target format | 目标格式- Returns:
- this image for chaining | 用于链式调用的图片
-
grayscale
-
save
Save to path 保存到路径- Parameters:
path- the output path | 输出路径- Throws:
ImageWriteException- if writing fails | 如果写入失败
-
save
Save to path with format 保存到路径(指定格式)- Parameters:
path- the output path | 输出路径format- the image format | 图片格式- Throws:
ImageWriteException- if writing fails | 如果写入失败
-
writeTo
Write to output stream 写入输出流- Parameters:
out- the output stream | 输出流- Throws:
ImageWriteException- if writing fails | 如果写入失败
-
writeTo
Write to output stream with format 写入输出流(指定格式)- Parameters:
out- the output stream | 输出流format- the image format | 图片格式- Throws:
ImageWriteException- if writing fails | 如果写入失败
-
toBytes
Convert to byte array 转换为字节数组- Returns:
- the byte array | 字节数组
- Throws:
ImageOperationException- if conversion fails | 如果转换失败
-
toBytes
Convert to byte array with format 转换为字节数组(指定格式)- Parameters:
format- the image format | 图片格式- Returns:
- the byte array | 字节数组
- Throws:
ImageOperationException- if conversion fails | 如果转换失败
-
toBase64
Convert to Base64 string 转换为Base64字符串- Returns:
- the Base64 encoded string | Base64编码字符串
- Throws:
ImageOperationException- if conversion fails | 如果转换失败
-
toBase64
Convert to Base64 string with format 转换为Base64字符串(指定格式)- Parameters:
format- the image format | 图片格式- Returns:
- the Base64 encoded string | Base64编码字符串
- Throws:
ImageOperationException- if conversion fails | 如果转换失败
-
copy
-