Enum Class ImageFormat
- All Implemented Interfaces:
Serializable, Comparable<ImageFormat>, Constable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class Enum
Enum.EnumDesc<E> -
Enum Constant Summary
Enum Constants -
Method Summary
Modifier and TypeMethodDescriptionstatic ImageFormatfromExtension(String extension) Get format from file extension 从文件扩展名获取格式static ImageFormatfromMimeType(String mimeType) Get format from MIME type 从MIME类型获取格式Get file extension 获取文件扩展名Get MIME type 获取MIME类型booleanCheck if this format is available at runtime 检查此格式在运行时是否可用static booleanisSupported(String extension) Check if extension is supported 检查扩展名是否支持static booleanCheck if WebP support is available at runtime 检查运行时是否支持 WebPbooleanCheck if format supports transparency 检查格式是否支持透明度static ImageFormatReturns the enum constant of this class with the specified name.static ImageFormat[]values()Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
JPEG
JPEG format | JPEG格式 -
JPG
JPG format (alias for JPEG) | JPG格式(JPEG别名) -
PNG
PNG format | PNG格式 -
GIF
GIF format | GIF格式 -
BMP
BMP format | BMP格式 -
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
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
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 nameNullPointerException- if the argument is null
-
getExtension
-
getMimeType
-
supportsTransparency
public boolean supportsTransparency()Check if format supports transparency 检查格式是否支持透明度- Returns:
- true if transparency is supported | 如果支持透明度返回true
-
fromExtension
Get format from file extension 从文件扩展名获取格式- Parameters:
extension- the file extension | 文件扩展名- Returns:
- the image format | 图片格式
- Throws:
IllegalArgumentException- if format is not supported | 如果格式不支持则抛出异常
-
fromMimeType
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
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 检查运行时是否支持 WebPWebP 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
-