Class ExifOp

java.lang.Object
cloud.opencode.base.image.exif.ExifOp

public final class ExifOp extends Object
EXIF Operations — Read, Strip and Auto-Orient EXIF 操作 — 读取、清除和自动旋转

Pure Java EXIF parser for JPEG images. Reads EXIF metadata from the APP1 segment without any third-party dependencies. Supports reading orientation, camera info, GPS coordinates, date/time, and software tags.

纯 Java 实现的 JPEG 图片 EXIF 解析器。从 APP1 段读取 EXIF 元数据, 不依赖任何第三方库。支持读取方向、相机信息、GPS 坐标、日期时间和软件标签。

Features | 主要功能:

  • Read EXIF metadata from JPEG files or byte arrays - 从 JPEG 文件或字节数组读取 EXIF 元数据
  • Strip EXIF tags selectively or entirely - 选择性或完全清除 EXIF 标签
  • Auto-orient images based on EXIF orientation - 根据 EXIF 方向自动旋转图片
  • Pure Java implementation, no third-party dependencies - 纯 Java 实现,无第三方依赖

Usage Examples | 使用示例:

// Read EXIF from file
ExifInfo info = ExifOp.read(Path.of("photo.jpg"));

// Read EXIF from bytes
ExifInfo info = ExifOp.read(jpegBytes);

// Strip all EXIF data
byte[] clean = ExifOp.strip(jpegBytes, ExifTag.ALL);

// Auto-orient image
BufferedImage corrected = ExifOp.autoOrient(image, info.orientation());

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
  • Null-safe: No (throws NullPointerException on null input) - 空值安全: 否(null 输入抛出 NullPointerException)
Since:
JDK 25, opencode-base-image V2.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • read

      public static ExifInfo read(Path path)
      Read EXIF metadata from a JPEG file 从 JPEG 文件读取 EXIF 元数据
      Parameters:
      path - the path to the JPEG file | JPEG 文件路径
      Returns:
      the parsed EXIF info, or empty if not a JPEG or no EXIF | 解析的 EXIF 信息,非 JPEG 或无 EXIF 则返回空
      Throws:
      ImageIOException - if the file cannot be read | 当文件无法读取时
      NullPointerException - if path is null | 当 path 为 null 时
    • read

      public static ExifInfo read(byte[] jpegBytes)
      Read EXIF metadata from JPEG bytes 从 JPEG 字节数组读取 EXIF 元数据
      Parameters:
      jpegBytes - the JPEG image bytes | JPEG 图片字节数组
      Returns:
      the parsed EXIF info, or empty if not a JPEG or no EXIF | 解析的 EXIF 信息,非 JPEG 或无 EXIF 则返回空
      Throws:
      NullPointerException - if jpegBytes is null | 当 jpegBytes 为 null 时
    • strip

      public static byte[] strip(byte[] jpegBytes, ExifTag... tags)
      Strip EXIF tags from JPEG bytes 从 JPEG 字节数组中清除 EXIF 标签

      If ExifTag.ALL is included, the entire APP1 segment is removed. Otherwise, only the specified tag categories are zeroed out within the existing EXIF structure.

      如果包含 ExifTag.ALL,则移除整个 APP1 段。 否则,仅在现有 EXIF 结构中将指定标签类别清零。

      Parameters:
      jpegBytes - the JPEG image bytes | JPEG 图片字节数组
      tags - the tag categories to strip | 要清除的标签类别
      Returns:
      the JPEG bytes with specified tags stripped | 清除指定标签后的 JPEG 字节数组
      Throws:
      NullPointerException - if jpegBytes or tags is null | 当 jpegBytes 或 tags 为 null 时
    • autoOrient

      public static BufferedImage autoOrient(BufferedImage image, int orientation)
      Auto-orient an image based on EXIF orientation value 根据 EXIF 方向值自动旋转图片
      Parameters:
      image - the source image | 源图片
      orientation - the EXIF orientation value (1-8) | EXIF 方向值 (1-8)
      Returns:
      the correctly oriented image | 正确方向的图片
      Throws:
      NullPointerException - if image is null | 当 image 为 null 时