In java how can I extract camera related informati

2020-07-15 15:12发布

问题:

I am looking to extract the camera related information from a jpg using Java. I have looked around but have not been able to find a solution to my problem. I am exporting my photos from Aperture on my mac (OS X 10.7) and want to use the data from Aperture that is available in the file info.

Any ideas?

I am looking to have Dimensions and Key Words extracted from photos like this one: 80.167.88.49/masters/test.html. Currently i get an exception when trying to use the Metadata Extractor. I don't know if Aperture is adding information that cannot be handled but it throws an exception on all photos from Aperture.

Exception in thread "main" java.lang.NoClassDefFoundError: com/adobe/xmp/XMPException
    at com.drew.imaging.jpeg.JpegMetadataReader.extractMetadataFromJpegSegmentReader(Unknown Source)
    at com.drew.imaging.jpeg.JpegMetadataReader.readMetadata(Unknown Source)
    at com.drew.imaging.ImageMetadataReader.readMetadata(Unknown Source)
    at com.drew.imaging.ImageMetadataReader.readMetadata(Unknown Source)
    at ImageScaler.main(ImageScaler.java:141)
Caused by: java.lang.ClassNotFoundException: com.adobe.xmp.XMPException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 5 more

Code:

public static void main (String[] args){

    File image = new File("/Users/peterla/Desktop/P8214462.jpg");

    Metadata metadata = null;
    try {
    metadata = ImageMetadataReader.readMetadata(image);
    } catch (ImageProcessingException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    Directory directory;

    // Keywords
    directory = metadata.getDirectory(IptcDirectory.class);
    String keywords[] = directory.getStringArray(IptcDirectory.TAG_KEYWORDS);

    // Dimensions
    directory = metadata.getDirectory(JpegDirectory.class);     
    String height = directory.getString(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);
    String width = directory.getString(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);     
}

回答1:

Metadata Extractor has a simple interface for reading several types of metadata from many digital image formats. This includes the EXIF metadata format used in jpeg images. The library has good Javadoc style documentation.

The primary entry point into the library is the ImageMetadataReader object.

The Getting Started page has a nice intro, including a nice example of how to get a value for a specific tag from EXIF format metadata.

Update: Example for Extracting Keywords and Dimensions

Directory directory;
// Keywords
directory = metadata.getDirectory(IptcDirectory.class);
String keywords[] = directory.getStringArray(IptcDirectory.TAG_KEYWORDS);

// Dimensions
directory = metadata.getDirectory(JpegDirectory.class);     
String height = directory.getString(JpegDirectory.TAG_JPEG_IMAGE_HEIGHT);
String width = directory.getString(JpegDirectory.TAG_JPEG_IMAGE_WIDTH);

Alternatives

Alternatives include the builtin java ImageIO library and Sanselan.



回答2:

You need a library to read the EXIF metadata. There's an example of how to do it with ImageIO on this blog post.

There are other libraries that you might also want to consider depending on your needs. For example MetadataExtractor or Sanselan.



回答3:

jhead, a JPEG EXIF header manipulation tool, is another option.