Class OpenImage
java.lang.Object
cloud.opencode.base.image.OpenImage
OpenImage
图片处理工具类
Static utility class for image operations. Entry point for the image processing API.
图片操作的静态工具类。图片处理API的入口点。
Usage Examples | 使用示例:
// Read and get info
ImageInfo info = OpenImage.getInfo(Path.of("photo.jpg"));
// Read as Image wrapper
Image image = OpenImage.read(path)
.resize(800, 600)
.save(outputPath);
// Quick resize
OpenImage.resize(inputPath, outputPath, 800, 600);
// Convert format
OpenImage.convert(inputPath, outputPath, ImageFormat.PNG);
Features | 主要功能:
- Static utility entry point for image processing - 图片处理的静态工具入口点
- Read, resize, crop, rotate, convert operations - 读取、缩放、裁剪、旋转、转换操作
- Image info extraction without full loading - 无需完全加载的图片信息提取
- Format detection from file content - 从文件内容检测格式
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: No (throws on null path/stream) - 空值安全: 否(null 路径/流抛异常)
- Since:
- JDK 25, opencode-base-image V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidQuick compress operation 快速压缩操作static voidconvert(Path input, Path output, ImageFormat format) Quick format conversion 快速格式转换static ImagecreateBlank(int width, int height) Create a blank image 创建空白图片static ImagecreateBlank(int width, int height, int color) Create a blank image with color 创建带颜色的空白图片static voidQuick crop operation 快速裁剪操作static ImageFormatdetectFormat(Path path) Detect format from file path 从文件路径检测格式static voidflipHorizontal(Path input, Path output) Quick horizontal flip 快速水平翻转static voidflipVertical(Path input, Path output) Quick vertical flip 快速垂直翻转static ImagefromBase64(String base64) Read image from Base64 string 从Base64字符串读取图片static int[]getDimensions(Path path) Get image dimensions without fully loading 获取图片尺寸(不完全加载)static ImageInfoGet image info from path 从路径获取图片信息static voidQuick grayscale conversion 快速灰度转换static booleanisSupported(String extension) Check if format is supported 检查格式是否支持static booleanisValidImage(Path path) Check if file is a valid image 检查文件是否为有效图片static Imageread(byte[] bytes) Read image from byte array 从字节数组读取图片static Imageread(InputStream in) Read image from input stream 从输入流读取图片static ImageRead image from path 从路径读取图片static BufferedImagereadBufferedImage(Path path) Read BufferedImage directly from path 直接从路径读取BufferedImagestatic voidQuick resize operation 快速调整尺寸操作static voidQuick rotate operation 快速旋转操作static ThumbnailBuilderCreate a thumbnail builder 创建缩略图构建器static voidQuick thumbnail creation 快速创建缩略图static byte[]toBytes(BufferedImage image, ImageFormat format) Convert image to byte array 将图片转换为字节数组static byte[]Convert image from path to byte array 将路径中的图片转换为字节数组static voidwrite(BufferedImage image, OutputStream out, ImageFormat format) Write image to output stream 写入图片到输出流static voidwrite(BufferedImage image, Path path) Write image to path 写入图片到路径static voidwrite(BufferedImage image, Path path, ImageFormat format) Write image to path with format 写入图片到路径(指定格式)
-
Method Details
-
read
Read image from path 从路径读取图片- Parameters:
path- the image path | 图片路径- Returns:
- the image wrapper | 图片包装器
- Throws:
ImageReadException- if reading fails | 如果读取失败
-
read
Read image from input stream 从输入流读取图片- Parameters:
in- the input stream | 输入流- Returns:
- the image wrapper | 图片包装器
- Throws:
ImageIOException- if reading fails | 如果读取失败
-
read
Read image from byte array 从字节数组读取图片- Parameters:
bytes- the image bytes | 图片字节数组- Returns:
- the image wrapper | 图片包装器
- Throws:
ImageIOException- if reading fails | 如果读取失败
-
fromBase64
Read image from Base64 string 从Base64字符串读取图片- Parameters:
base64- the Base64 encoded image | Base64编码的图片- Returns:
- the image wrapper | 图片包装器
- Throws:
ImageIOException- if reading fails | 如果读取失败
-
readBufferedImage
Read BufferedImage directly from path 直接从路径读取BufferedImage- Parameters:
path- the image path | 图片路径- Returns:
- the buffered image | 缓冲图片
- Throws:
ImageReadException- if reading fails | 如果读取失败
-
write
Write image to path 写入图片到路径- Parameters:
image- the buffered image | 缓冲图片path- the output path | 输出路径- Throws:
ImageWriteException- if writing fails | 如果写入失败
-
write
public static void write(BufferedImage image, Path path, ImageFormat format) throws ImageWriteException Write image to path with format 写入图片到路径(指定格式)- Parameters:
image- the buffered image | 缓冲图片path- the output path | 输出路径format- the image format | 图片格式- Throws:
ImageWriteException- if writing fails | 如果写入失败
-
write
public static void write(BufferedImage image, OutputStream out, ImageFormat format) throws ImageIOException Write image to output stream 写入图片到输出流- Parameters:
image- the buffered image | 缓冲图片out- the output stream | 输出流format- the image format | 图片格式- Throws:
ImageIOException- if writing fails | 如果写入失败
-
getInfo
Get image info from path 从路径获取图片信息- Parameters:
path- the image path | 图片路径- Returns:
- the image info | 图片信息
- Throws:
ImageReadException- if reading fails | 如果读取失败
-
getDimensions
Get image dimensions without fully loading 获取图片尺寸(不完全加载)- Parameters:
path- the image path | 图片路径- Returns:
- array of [width, height] | [宽度, 高度]数组
- Throws:
ImageReadException- if reading fails | 如果读取失败
-
resize
Quick resize operation 快速调整尺寸操作- Parameters:
input- the input path | 输入路径output- the output path | 输出路径width- the target width | 目标宽度height- the target height | 目标高度- Throws:
ImageIOException- if operation fails | 如果操作失败
-
crop
public static void crop(Path input, Path output, int x, int y, int width, int height) throws ImageIOException Quick crop operation 快速裁剪操作- Parameters:
input- the input path | 输入路径output- the output path | 输出路径x- the x coordinate | X坐标y- the y coordinate | Y坐标width- the crop width | 裁剪宽度height- the crop height | 裁剪高度- Throws:
ImageIOException- if operation fails | 如果操作失败
-
rotate
Quick rotate operation 快速旋转操作- Parameters:
input- the input path | 输入路径output- the output path | 输出路径degrees- the rotation degrees | 旋转角度- Throws:
ImageIOException- if operation fails | 如果操作失败
-
convert
Quick format conversion 快速格式转换- Parameters:
input- the input path | 输入路径output- the output path | 输出路径format- the target format | 目标格式- Throws:
ImageIOException- if operation fails | 如果操作失败
-
compress
Quick compress operation 快速压缩操作- Parameters:
input- the input path | 输入路径output- the output path | 输出路径quality- the quality (0.0 to 1.0) | 质量(0.0到1.0)- Throws:
ImageIOException- if operation fails | 如果操作失败
-
thumbnail
Quick thumbnail creation 快速创建缩略图- Parameters:
input- the input path | 输入路径output- the output path | 输出路径size- the maximum dimension | 最大尺寸- Throws:
ImageIOException- if operation fails | 如果操作失败
-
thumbnail
Create a thumbnail builder 创建缩略图构建器Returns a builder for creating thumbnails with various options.
返回用于创建各种选项缩略图的构建器。
Usage Example | 使用示例:
OpenImage.thumbnail() .source(Path.of("photo.jpg")) .size(200, 200) .format(ImageFormat.PNG) .output(Path.of("thumb.png")) .create();- Returns:
- the thumbnail builder | 缩略图构建器
-
flipHorizontal
Quick horizontal flip 快速水平翻转- Parameters:
input- the input path | 输入路径output- the output path | 输出路径- Throws:
ImageIOException- if operation fails | 如果操作失败
-
flipVertical
Quick vertical flip 快速垂直翻转- Parameters:
input- the input path | 输入路径output- the output path | 输出路径- Throws:
ImageIOException- if operation fails | 如果操作失败
-
grayscale
Quick grayscale conversion 快速灰度转换- Parameters:
input- the input path | 输入路径output- the output path | 输出路径- Throws:
ImageIOException- if operation fails | 如果操作失败
-
toBytes
Convert image to byte array 将图片转换为字节数组- Parameters:
image- the image | 图片format- the format | 格式- Returns:
- byte array | 字节数组
- Throws:
ImageIOException- if conversion fails | 如果转换失败
-
toBytes
Convert image from path to byte array 将路径中的图片转换为字节数组- Parameters:
path- the image path | 图片路径- Returns:
- byte array | 字节数组
- Throws:
ImageIOException- if conversion fails | 如果转换失败
-
detectFormat
Detect format from file path 从文件路径检测格式- Parameters:
path- the file path | 文件路径- Returns:
- the detected format | 检测到的格式
-
isValidImage
Check if file is a valid image 检查文件是否为有效图片- Parameters:
path- the file path | 文件路径- Returns:
- true if valid image | 如果是有效图片返回true
-
isSupported
Check if format is supported 检查格式是否支持- Parameters:
extension- the file extension | 文件扩展名- Returns:
- true if supported | 如果支持返回true
-
createBlank
Create a blank image 创建空白图片- Parameters:
width- the width | 宽度height- the height | 高度- Returns:
- the image wrapper | 图片包装器
-
createBlank
Create a blank image with color 创建带颜色的空白图片- Parameters:
width- the width | 宽度height- the height | 高度color- the background color (ARGB) | 背景颜色(ARGB)- Returns:
- the image wrapper | 图片包装器
-