Class Image
java.lang.Object
cloud.opencode.base.image.Image
Image
图片处理包装类
Chainable Raster-based image processing wrapper with fluent API.
Backed by an internal mutable Raster; each operation replaces the
raster with the result of the corresponding apply(Raster, ...)
operator.
基于 Raster 的链式图像处理包装,内部持有可变 Raster;每个操作
都会用相应 apply(Raster, ...) 算子的结果替换它。
Usage Examples | 使用示例:
// Chain operations
Image.from(path)
.resize(800, 600)
.rotate(90)
.save(outputPath);
// Get bytes
byte[] bytes = Image.fromRaster(raster)
.crop(100, 100, 400, 300)
.toBytes(ImageFormat.PNG);
Features | 主要功能:
- Chainable fluent API for
Rasteroperations - 基于 Raster 的链式 API - Resize, crop, rotate, color, blur, edge, threshold, histogram - 多类操作
- Save to file or convert to byte array - 保存或转字节
Security | 安全性:
- Thread-safe: No (mutable internal state) - 线程安全: 否(内部可变状态)
- Null-safe: No (throws on null raster) - 空值安全: 否
- Since:
- JDK 25, opencode-base-image V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadaptiveThreshold(int blockSize, double c) Adaptive threshold. | 自适应阈值。autoOrient(int orientation) Auto-orient by EXIF orientation tag. | 按 EXIF orientation 自动正向。Bilateral filter. | 双边滤波。border(int thickness, int colorArgb) Draw a solid border of the given color. | 绘制纯色边框。boxBlur(int kernelSize) Box (mean) blur. | 均值模糊。brightness(double factor) Brightness. | 亮度。cannyEdge(double lowThreshold, double highThreshold) Canny edge detection. | Canny 边缘检测。clahe(double clipLimit, int tileGridSize) CLAHE histogram equalization. | CLAHE 直方图均衡化。compress(float quality) Compress (JPEG path). | JPEG 压缩。contrast(double factor) Contrast. | 对比度。convert(ImageFormat targetFormat) Convert pixel layout to suit the target format (e.g. drop alpha for JPEG).copy()Make a deep copy. | 深拷贝。crop(int x, int y, int width, int height) Crop to rectangle. | 裁剪到矩形。cropCenter(int width, int height) Crop centered to (width, height). | 居中裁剪。Crop to a centered square. | 裁剪为居中正方形。cropToAspectRatio(int aspectWidth, int aspectHeight) Crop to a target aspect ratio centered. | 裁剪为目标宽高比(居中)。Histogram equalization. | 直方图均衡化。Flip horizontally. | 水平翻转。Flip vertically. | 垂直翻转。format(ImageFormat format) Set image format 设置图片格式static Imagefrom(byte[] bytes) Create image from byte array 从字节数组创建图片static ImageCreate image from path 从路径创建图片static ImagefromRaster(Raster raster) static ImagefromRaster(Raster raster, ImageFormat format) gamma(double gamma) Gamma correction. | 伽马校正。gaussianBlur(double sigma) Gaussian blur. | 高斯模糊。Get image format 获取图片格式intGet image height 获取图片高度getInfo()Get image info 获取图片信息intgetWidth()Get image width 获取图片宽度Convert to grayscale. | 转灰度。invert()Invert colors. | 反色。medianBlur(int kernelSize) Median blur. | 中值模糊。Otsu's automatic threshold. | 大津法自动阈值。pad(int padding, int colorArgb) Pad uniformly with the given ARGB color. | 用 ARGB 颜色等距填充。pad(int top, int right, int bottom, int left, int colorArgb) Pad with explicit insets. | 按指定内边距填充。resize(int width, int height) Resize to exact dimensions. | 缩放到指定尺寸。resizeProgressive(int width, int height) Progressive resize for higher quality on large downscale. | 大幅缩小时使用渐进缩放保留质量。resizeToFit(int maxWidth, int maxHeight) Resize to fit within (maxWidth, maxHeight) preserving aspect ratio. | 等比缩放到 (maxWidth, maxHeight) 内。rotate(double degrees) Rotate by arbitrary angle (degrees, around center). | 任意角度旋转(绕中心,单位度)。Rotate 180 degrees. | 旋转 180°。Rotate 270 degrees clockwise. | 顺时针旋转 270°。rotate90()Rotate 90 degrees clockwise. | 顺时针旋转 90°。saturation(double factor) Saturation. | 饱和度。voidSave to path inferring format from extension. | 按扩展名保存到路径。voidsave(Path path, ImageFormat format) Save to path with explicit format. | 以指定格式保存到路径。scale(double scale) Scale by uniform factor. | 按统一系数缩放。scaleToHeight(int height) Scale to a target height (preserve aspect). | 缩放到目标高度(保持比例)。scaleToWidth(int width) Scale to a target width (preserve aspect). | 缩放到目标宽度(保持比例)。sepia()Sepia tone. | 棕褐色调。sharpen()Sharpen with default amount. | 默认强度锐化。sharpen(double amount) Sharpen with the given amount. | 指定强度锐化。Sobel edge detection. | Sobel 边缘检测。No-op stub for legacy EXIF strip API.threshold(int value) Fixed threshold. | 固定阈值。toBase64()Encode to Base64 using current format. | 以当前格式编码为 Base64。toBase64(ImageFormat format) Encode to Base64 with explicit format. | 以指定格式编码为 Base64。byte[]toBytes()Encode to bytes using current format. | 以当前格式编码为字节。byte[]toBytes(ImageFormat format) Encode to bytes with explicit format. | 以指定格式编码为字节。toRaster()Get the underlyingRaster.Auto white balance (gray world). | 灰度世界白平衡。voidwriteTo(OutputStream out) Write to output stream using current format. | 用当前格式写入输出流。voidwriteTo(OutputStream out, ImageFormat format) Write to output stream with explicit format. | 以指定格式写入输出流。
-
Constructor Details
-
Image
-
Image
Create image wrapper with explicit format. 创建带显式格式的图像包装。- Parameters:
raster- the raster | rasterformat- 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 byte array 从字节数组创建图片- Parameters:
bytes- the image bytes | 图片字节数组- Returns:
- the image wrapper | 图片包装器
- Throws:
ImageIOException- if reading fails | 如果读取失败
-
fromRaster
-
fromRaster
-
toRaster
-
getFormat
-
format
Set image format 设置图片格式- Parameters:
format- the new format | 新格式- Returns:
- this image for chaining | 用于链式调用的图片
-
getWidth
public int getWidth()Get image width 获取图片宽度- Returns:
- the width | 宽度
-
getHeight
public int getHeight()Get image height 获取图片高度- Returns:
- the height | 高度
-
getInfo
-
resize
Resize to exact dimensions. | 缩放到指定尺寸。 -
resizeToFit
Resize to fit within (maxWidth, maxHeight) preserving aspect ratio. | 等比缩放到 (maxWidth, maxHeight) 内。 -
scale
Scale by uniform factor. | 按统一系数缩放。 -
scaleToWidth
Scale to a target width (preserve aspect). | 缩放到目标宽度(保持比例)。 -
resizeProgressive
Progressive resize for higher quality on large downscale. | 大幅缩小时使用渐进缩放保留质量。 -
scaleToHeight
Scale to a target height (preserve aspect). | 缩放到目标高度(保持比例)。 -
crop
Crop to rectangle. | 裁剪到矩形。 -
cropCenter
Crop centered to (width, height). | 居中裁剪。 -
cropSquare
Crop to a centered square. | 裁剪为居中正方形。 -
cropToAspectRatio
Crop to a target aspect ratio centered. | 裁剪为目标宽高比(居中)。 -
rotate
Rotate by arbitrary angle (degrees, around center). | 任意角度旋转(绕中心,单位度)。 -
rotate90
Rotate 90 degrees clockwise. | 顺时针旋转 90°。 -
rotate180
Rotate 180 degrees. | 旋转 180°。 -
rotate270
Rotate 270 degrees clockwise. | 顺时针旋转 270°。 -
flipHorizontal
Flip horizontally. | 水平翻转。 -
flipVertical
Flip vertically. | 垂直翻转。 -
pad
Pad uniformly with the given ARGB color. | 用 ARGB 颜色等距填充。 -
pad
Pad with explicit insets. | 按指定内边距填充。 -
border
Draw a solid border of the given color. | 绘制纯色边框。 -
compress
Compress (JPEG path). | JPEG 压缩。 -
convert
Convert pixel layout to suit the target format (e.g. drop alpha for JPEG). -
grayscale
Convert to grayscale. | 转灰度。 -
gaussianBlur
Gaussian blur. | 高斯模糊。 -
medianBlur
Median blur. | 中值模糊。 -
boxBlur
Box (mean) blur. | 均值模糊。 -
sharpen
Sharpen with default amount. | 默认强度锐化。 -
sharpen
Sharpen with the given amount. | 指定强度锐化。 -
bilateralFilter
Bilateral filter. | 双边滤波。 -
gamma
Gamma correction. | 伽马校正。 -
saturation
Saturation. | 饱和度。 -
whiteBalance
Auto white balance (gray world). | 灰度世界白平衡。 -
brightness
Brightness. | 亮度。 -
contrast
Contrast. | 对比度。 -
sepia
Sepia tone. | 棕褐色调。 -
invert
Invert colors. | 反色。 -
sobelEdge
Sobel edge detection. | Sobel 边缘检测。 -
cannyEdge
Canny edge detection. | Canny 边缘检测。 -
threshold
Fixed threshold. | 固定阈值。 -
otsuThreshold
Otsu's automatic threshold. | 大津法自动阈值。 -
adaptiveThreshold
Adaptive threshold. | 自适应阈值。 -
equalizeHistogram
Histogram equalization. | 直方图均衡化。 -
clahe
CLAHE histogram equalization. | CLAHE 直方图均衡化。 -
autoOrient
Auto-orient by EXIF orientation tag. | 按 EXIF orientation 自动正向。 -
stripExif
No-op stub for legacy EXIF strip API. 旧版 stripExif 兼容空实现。EXIF metadata is not embedded in the
Rasterpipeline — encoded outputs fromOpenImage.write(Raster, Path)carry no EXIF, so stripping is implicit. This method exists only for fluent compatibility.Raster 管线不携带 EXIF 元数据 ——
OpenImage.write(Raster, Path)输出本身就不含 EXIF,无需剥离。本方法仅为流式 API 兼容保留。- Parameters:
tags- ignored | 忽略- Returns:
- this image | 当前图片
-
save
Save to path inferring format from extension. | 按扩展名保存到路径。- Throws:
ImageWriteException
-
save
Save to path with explicit format. | 以指定格式保存到路径。- Throws:
ImageWriteException
-
writeTo
Write to output stream using current format. | 用当前格式写入输出流。- Throws:
ImageWriteException
-
writeTo
Write to output stream with explicit format. | 以指定格式写入输出流。- Throws:
ImageWriteException
-
toBytes
Encode to bytes using current format. | 以当前格式编码为字节。- Throws:
ImageOperationException
-
toBytes
Encode to bytes with explicit format. | 以指定格式编码为字节。- Throws:
ImageOperationException
-
toBase64
Encode to Base64 using current format. | 以当前格式编码为 Base64。- Throws:
ImageOperationException
-
toBase64
Encode to Base64 with explicit format. | 以指定格式编码为 Base64。- Throws:
ImageOperationException
-
copy
Make a deep copy. | 深拷贝。
-