Class OpenImage

java.lang.Object
cloud.opencode.base.image.OpenImage

public final class OpenImage extends Object
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 Details

    • read

      public static Image read(Path path) throws ImageReadException
      Read image from path 从路径读取图片
      Parameters:
      path - the image path | 图片路径
      Returns:
      the image wrapper | 图片包装器
      Throws:
      ImageReadException - if reading fails | 如果读取失败
    • read

      public static Image read(InputStream in) throws ImageIOException
      Read image from input stream 从输入流读取图片
      Parameters:
      in - the input stream | 输入流
      Returns:
      the image wrapper | 图片包装器
      Throws:
      ImageIOException - if reading fails | 如果读取失败
    • read

      public static Image read(byte[] bytes) throws ImageIOException
      Read image from byte array 从字节数组读取图片
      Parameters:
      bytes - the image bytes | 图片字节数组
      Returns:
      the image wrapper | 图片包装器
      Throws:
      ImageIOException - if reading fails | 如果读取失败
    • fromBase64

      public static Image fromBase64(String base64) throws ImageIOException
      Read image from Base64 string 从Base64字符串读取图片
      Parameters:
      base64 - the Base64 encoded image | Base64编码的图片
      Returns:
      the image wrapper | 图片包装器
      Throws:
      ImageIOException - if reading fails | 如果读取失败
    • readBufferedImage

      public static BufferedImage readBufferedImage(Path path) throws ImageReadException
      Read BufferedImage directly from path 直接从路径读取BufferedImage
      Parameters:
      path - the image path | 图片路径
      Returns:
      the buffered image | 缓冲图片
      Throws:
      ImageReadException - if reading fails | 如果读取失败
    • write

      public static void write(BufferedImage image, Path path) throws ImageWriteException
      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

      public static ImageInfo getInfo(Path path) throws ImageReadException
      Get image info from path 从路径获取图片信息
      Parameters:
      path - the image path | 图片路径
      Returns:
      the image info | 图片信息
      Throws:
      ImageReadException - if reading fails | 如果读取失败
    • getDimensions

      public static int[] getDimensions(Path path) throws ImageReadException
      Get image dimensions without fully loading 获取图片尺寸(不完全加载)
      Parameters:
      path - the image path | 图片路径
      Returns:
      array of [width, height] | [宽度, 高度]数组
      Throws:
      ImageReadException - if reading fails | 如果读取失败
    • resize

      public static void resize(Path input, Path output, int width, int height) throws ImageIOException
      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

      public static void rotate(Path input, Path output, double degrees) throws ImageIOException
      Quick rotate operation 快速旋转操作
      Parameters:
      input - the input path | 输入路径
      output - the output path | 输出路径
      degrees - the rotation degrees | 旋转角度
      Throws:
      ImageIOException - if operation fails | 如果操作失败
    • convert

      public static void convert(Path input, Path output, ImageFormat format) throws ImageIOException
      Quick format conversion 快速格式转换
      Parameters:
      input - the input path | 输入路径
      output - the output path | 输出路径
      format - the target format | 目标格式
      Throws:
      ImageIOException - if operation fails | 如果操作失败
    • compress

      public static void compress(Path input, Path output, float quality) throws ImageIOException
      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

      public static void thumbnail(Path input, Path output, int size) throws ImageIOException
      Quick thumbnail creation 快速创建缩略图
      Parameters:
      input - the input path | 输入路径
      output - the output path | 输出路径
      size - the maximum dimension | 最大尺寸
      Throws:
      ImageIOException - if operation fails | 如果操作失败
    • thumbnail

      public static ThumbnailBuilder 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

      public static void flipHorizontal(Path input, Path output) throws ImageIOException
      Quick horizontal flip 快速水平翻转
      Parameters:
      input - the input path | 输入路径
      output - the output path | 输出路径
      Throws:
      ImageIOException - if operation fails | 如果操作失败
    • flipVertical

      public static void flipVertical(Path input, Path output) throws ImageIOException
      Quick vertical flip 快速垂直翻转
      Parameters:
      input - the input path | 输入路径
      output - the output path | 输出路径
      Throws:
      ImageIOException - if operation fails | 如果操作失败
    • grayscale

      public static void grayscale(Path input, Path output) throws ImageIOException
      Quick grayscale conversion 快速灰度转换
      Parameters:
      input - the input path | 输入路径
      output - the output path | 输出路径
      Throws:
      ImageIOException - if operation fails | 如果操作失败
    • toBytes

      public static byte[] toBytes(BufferedImage image, ImageFormat format) throws ImageIOException
      Convert image to byte array 将图片转换为字节数组
      Parameters:
      image - the image | 图片
      format - the format | 格式
      Returns:
      byte array | 字节数组
      Throws:
      ImageIOException - if conversion fails | 如果转换失败
    • toBytes

      public static byte[] toBytes(Path path) throws ImageIOException
      Convert image from path to byte array 将路径中的图片转换为字节数组
      Parameters:
      path - the image path | 图片路径
      Returns:
      byte array | 字节数组
      Throws:
      ImageIOException - if conversion fails | 如果转换失败
    • detectFormat

      public static ImageFormat detectFormat(Path path)
      Detect format from file path 从文件路径检测格式
      Parameters:
      path - the file path | 文件路径
      Returns:
      the detected format | 检测到的格式
    • isValidImage

      public static boolean isValidImage(Path path)
      Check if file is a valid image 检查文件是否为有效图片
      Parameters:
      path - the file path | 文件路径
      Returns:
      true if valid image | 如果是有效图片返回true
    • isSupported

      public static boolean isSupported(String extension)
      Check if format is supported 检查格式是否支持
      Parameters:
      extension - the file extension | 文件扩展名
      Returns:
      true if supported | 如果支持返回true
    • createBlank

      public static Image createBlank(int width, int height)
      Create a blank image 创建空白图片
      Parameters:
      width - the width | 宽度
      height - the height | 高度
      Returns:
      the image wrapper | 图片包装器
    • createBlank

      public static Image createBlank(int width, int height, int color)
      Create a blank image with color 创建带颜色的空白图片
      Parameters:
      width - the width | 宽度
      height - the height | 高度
      color - the background color (ARGB) | 背景颜色(ARGB)
      Returns:
      the image wrapper | 图片包装器