Recently I have been investigating image library in Java.
In this post, I will show you the code snippet for extracting jpeg image exif metadata using Sanselan.
Code
package com.dulesoftware.image; import java.io.File; import java.io.IOException; import java.util.List; import org.apache.sanselan.ImageReadException; import org.apache.sanselan.Sanselan; import org.apache.sanselan.common.IImageMetadata; import org.apache.sanselan.common.ImageMetadata.Item; import org.apache.sanselan.formats.jpeg.JpegImageMetadata; import org.apache.sanselan.formats.tiff.TiffField; import org.apache.sanselan.formats.tiff.TiffImageMetadata; public class SanselanMetadataExample { public static void main(String[] args) throws Exception { printMeatadata(new File("c:/temp/metadata_eample.jpg")); } public static void printMeatadata(File file) throws ImageReadException, IOException { IImageMetadata sanselanmetadata = Sanselan.getMetadata(file); if (sanselanmetadata instanceof JpegImageMetadata) { JpegImageMetadata jpegMetadata = (JpegImageMetadata) sanselanmetadata; TiffImageMetadata tiffImageMetadata = jpegMetadata.getExif(); // print all items System.out.println("=== print all items ==="); printMetadataList(jpegMetadata.getItems()); // print all EXIF System.out.println("=== print all EXIF ==="); printExif(tiffImageMetadata); // print all GPS System.out.println("=== print GPS ==="); TiffImageMetadata.GPSInfo gpsInfo = tiffImageMetadata.getGPS(); System.out.println(gpsInfo); } } private static void printExif(TiffImageMetadata tiffImageMetadata) throws ImageReadException { for(Object field : tiffImageMetadata.getAllFields()) { if(field instanceof TiffField) { TiffField tiffField = (TiffField)field; System.out.println(tiffField.getTagName()+ ": " + tiffField.getValueDescription()); } } } private static void printMetadataList(List items) { for (Object item : items) { if (item instanceof org.apache.sanselan.common.ImageMetadata.Item) { Item tiffItem = (Item) item; System.out.println("item: " + tiffItem); } } } }
Example Result
I will show you the example execution result of the above code.The test image is below.
The code output following result.
=== print all items === item: Image Description: 'MetaData Example' item: Software: 'Picasa' item: Exif Offset: 2172 item: XPTitle: 77, 0, 101, 0, 116, 0, 97, 0, 68, 0, 97, 0, 116, 0, 97, 0, 32, 0, 69, 0, 120, 0, 97, 0, 109, 0, 112, 0, 108, 0, 101, 0, 0, 0 item: XPComment: 84, 0, 101, 0, 115, 0, 116, 0, 32, 0, 67, 0, 111, 0, 109, 0, 109, 0, 101, 0, 110, 0, 116, 0, 115, 0, 0, 0 item: Unknown Tag (0xea1c): 28, -22, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... (2060) item: Exif Version: 48, 50, 50, 48 item: Exif Image Width: 1440 item: Exif Image Length: 1440 item: Image Unique ID: '199e785efd74401dff564baa559c2f25' item: Unknown Tag (0xea1c): 28, -22, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... (2060) item: Compression: 6 item: XResolution: 72 item: YResolution: 72 item: Resolution Unit: 2 item: Jpg From Raw Start: 4488 item: Jpg From Raw Length: 6780 === print all EXIF === Image Description: 'MetaData Example' Software: 'Picasa' Exif Offset: 2172 XPTitle: 77, 0, 101, 0, 116, 0, 97, 0, 68, 0, 97, 0, 116, 0, 97, 0, 32, 0, 69, 0, 120, 0, 97, 0, 109, 0, 112, 0, 108, 0, 101, 0, 0, 0 XPComment: 84, 0, 101, 0, 115, 0, 116, 0, 32, 0, 67, 0, 111, 0, 109, 0, 109, 0, 101, 0, 110, 0, 116, 0, 115, 0, 0, 0 Unknown Tag (0xea1c): 28, -22, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... (2060) Exif Version: 48, 50, 50, 48 Exif Image Width: 1440 Exif Image Length: 1440 Image Unique ID: '199e785efd74401dff564baa559c2f25' Unknown Tag (0xea1c): 28, -22, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... (2060) Compression: 6 XResolution: 72 YResolution: 72 Resolution Unit: 2 Jpg From Raw Start: 4488 Jpg From Raw Length: 6780 === print GPS === null
コメント