You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Apache Commons Imaging(EXIF)标签写入问题:缺失与写入失败

Troubleshooting Apache Commons Imaging EXIF Tag Issues (Java 8)

Let me walk you through solving each of these EXIF tag headaches with Apache Commons Imaging—I’ve hit a few of these myself, so I know the frustration:

1. Locating & Writing "Origin" (Authors/Date Acquired), GPSAltitude, and Missing Artist Tags

  • Win10's "Authors" vs. Artist Tag: The "Authors" field you see in Win10 is Microsoft's extended tag MicrosoftTagConstants.EXIF_TAG_XPAUTHOR, not the traditional EXIF Artist tag. If the Artist tag seems missing from docs, double-check for ExifTagConstants.EXIF_TAG_ARTIST—it should still exist in most recent library builds (Win10 just doesn’t surface it in the "Origin" section by default).
  • GPSAltitude Not Showing Up: GpsTagConstants.GPS_TAG_GPS_ALTITUDE doesn’t accept raw numbers—it requires a Rational object (EXIF stores GPS altitude as a numerator/denominator fraction). Also, this tag belongs to a separate GPS sub-directory, not the main EXIF directory. You need to create or retrieve this sub-directory first before adding the tag.
  • XPAUTHOR Not Taking Effect: Microsoft’s XP tags rely on UTF-16LE encoding. Instead of passing a raw string, encode it to bytes with this charset when adding the tag.

2. Writing Maker Note (EXIF_TAG_MAKER_NOTE)

Since EXIF_TAG_MAKER_NOTE is a TagInfoUndefineds type, it expects raw byte data (not a string or primitive value). Here’s how to handle it:

  • Convert your maker note content to a byte array (note: some camera manufacturers use proprietary formats for Maker Notes—if you want other tools to recognize the data, make sure it matches your camera’s expected format).
  • Add it directly using the byte array:
    byte[] makerNoteBytes = yourMakerNoteContent.getBytes(StandardCharsets.US_ASCII); // Adjust charset as needed
    exifDir.add(ExifTagConstants.EXIF_TAG_MAKER_NOTE, makerNoteBytes);
    

3. Fixing DATE_TIME_DIGITIZED Write Exception

The error Tag expects 20 byte(s), not 1 happens because EXIF_TAG_DATE_TIME_DIGITIZED requires a strictly formatted ASCII string: YYYY:MM:DD HH:MM:SS (19 characters plus a null terminator equals 20 bytes total).

  • Make sure you’re passing a string in exactly this format—for example, "2024:05:20 14:30:00"—not a timestamp number or a differently formatted date.
  • Commons Imaging handles ASCII encoding automatically if you pass the correct string, but you can encode it manually with StandardCharsets.US_ASCII if you run into edge cases.

4. Ensuring XPAUTHOR and GPS_ALTITUDE Tags Save Correctly

Let’s break down working fixes for each:

  • XPAUTHOR: Use UTF-16LE encoding for the value:
    String authorName = "Jane Smith";
    byte[] authorBytes = authorName.getBytes(StandardCharsets.UTF_16LE);
    exifDir.add(MicrosoftTagConstants.EXIF_TAG_XPAUTHOR, authorBytes);
    
  • GPS_ALTITUDE: Work with the GPS sub-directory explicitly:
    // Retrieve or create the GPS sub-directory
    ExifDirectoryBase gpsDir = exifImageMetadata.getOrCreateGpsDirectory();
    // Create a Rational for altitude (e.g., 150 meters = 150/1)
    Rational altitude = new Rational(150, 1);
    gpsDir.add(GpsTagConstants.GPS_TAG_GPS_ALTITUDE, altitude);
    // Add altitude reference (0 = above sea level, 1 = below)
    gpsDir.add(GpsTagConstants.GPS_TAG_GPS_ALTITUDE_REF, (byte) 0);
    

After adding any tags, make sure you use ExifRewriter to write the updated metadata back to the image file—don’t skip this final step!

内容的提问来源于stack exchange,提问作者Neph

火山引擎 最新活动