Enum Class ImageFormat

java.lang.Object
java.lang.Enum<ImageFormat>
cloud.opencode.base.image.ImageFormat
All Implemented Interfaces:
Serializable, Comparable<ImageFormat>, Constable

public enum ImageFormat extends Enum<ImageFormat>
Image Format 图片格式枚举

Supported image formats for reading and writing.

支持读写的图片格式。

Supported Formats | 支持的格式:

  • JPEG - Most common, no transparency | 最常用,不支持透明度
  • PNG - Supports transparency | 支持透明度
  • GIF - Static images only | 仅支持静态图片
  • BMP - Uncompressed | 无压缩
  • WEBP - Optional, requires TwelveMonkeys plugin | 可选,需要 TwelveMonkeys 插件

Features | 主要功能:

  • Enum for supported image formats (JPEG, PNG, GIF, BMP, WEBP) - 支持的图片格式枚举(JPEG、PNG、GIF、BMP、WEBP)
  • MIME type and file extension mapping - MIME 类型和文件扩展名映射
  • Transparency support detection - 透明度支持检测

Usage Examples | 使用示例:

// Get format info
ImageFormat format = ImageFormat.PNG;
String ext = format.getExtension();      // "png"
String mime = format.getMimeType();       // "image/png"
boolean alpha = format.supportsAlpha();   // true

// Detect from filename
ImageFormat detected = ImageFormat.fromExtension("jpg");

Security | 安全性:

  • Thread-safe: Yes (immutable enum) - 线程安全: 是(不可变枚举)
  • Null-safe: No (throws on unknown extension) - 空值安全: 否(未知扩展名抛异常)
Since:
JDK 25, opencode-base-image V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Enum Constant Details

    • JPEG

      public static final ImageFormat JPEG
      JPEG format | JPEG格式
    • JPG

      public static final ImageFormat JPG
      JPG format (alias for JPEG) | JPG格式(JPEG别名)
    • PNG

      public static final ImageFormat PNG
      PNG format | PNG格式
    • GIF

      public static final ImageFormat GIF
      GIF format | GIF格式
    • BMP

      public static final ImageFormat BMP
      BMP format | BMP格式
    • WEBP

      public static final ImageFormat WEBP
      WebP format | WebP格式

      Optional format, requires TwelveMonkeys imageio-webp plugin.

      可选格式,需要 TwelveMonkeys imageio-webp 插件。

      Add dependency to enable:

      <dependency>
          <groupId>com.twelvemonkeys.imageio</groupId>
          <artifactId>imageio-webp</artifactId>
          <version>3.12.0</version>
      </dependency>
      
  • Method Details

    • values

      public static ImageFormat[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static ImageFormat valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • getExtension

      public String getExtension()
      Get file extension 获取文件扩展名
      Returns:
      the file extension | 文件扩展名
    • getMimeType

      public String getMimeType()
      Get MIME type 获取MIME类型
      Returns:
      the MIME type | MIME类型
    • supportsTransparency

      public boolean supportsTransparency()
      Check if format supports transparency 检查格式是否支持透明度
      Returns:
      true if transparency is supported | 如果支持透明度返回true
    • fromExtension

      public static ImageFormat fromExtension(String extension)
      Get format from file extension 从文件扩展名获取格式
      Parameters:
      extension - the file extension | 文件扩展名
      Returns:
      the image format | 图片格式
      Throws:
      IllegalArgumentException - if format is not supported | 如果格式不支持则抛出异常
    • fromMimeType

      public static ImageFormat fromMimeType(String mimeType)
      Get format from MIME type 从MIME类型获取格式
      Parameters:
      mimeType - the MIME type | MIME类型
      Returns:
      the image format | 图片格式
      Throws:
      IllegalArgumentException - if MIME type is not supported | 如果MIME类型不支持则抛出异常
    • isSupported

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

      public static boolean isWebPAvailable()
      Check if WebP support is available at runtime 检查运行时是否支持 WebP

      WebP support requires the TwelveMonkeys imageio-webp plugin.

      WebP 支持需要 TwelveMonkeys imageio-webp 插件。

      Returns:
      true if WebP plugin is available | 如果 WebP 插件可用返回 true
    • isAvailable

      public boolean isAvailable()
      Check if this format is available at runtime 检查此格式在运行时是否可用

      Most formats are always available, but WEBP requires an optional plugin.

      大多数格式始终可用,但 WEBP 需要可选插件。

      Returns:
      true if format is available | 如果格式可用返回 true