简体   繁体   English

在java中如何从图像中提取相机相关信息?

[英]In java how can I extract camera related information from an image?

I am looking to extract the camera related information from a jpg using Java. 我希望使用Java从jpg中提取相机相关信息。 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. 我在我的Mac(OS X 10.7)上从Aperture导出我的照片,并希望使用文件信息中提供的Aperture数据。

Any ideas? 有任何想法吗?

I am looking to have Dimensions and Key Words extracted from photos like this one: 80.167.88.49/masters/test.html. 我希望从像这样的照片中提取尺寸和关键词: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. 我不知道Aperture是否正在添加无法处理的信息,但它会在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);     
}

You need a library to read the EXIF metadata. 您需要一个库来读取EXIF元数据。 There's an example of how to do it with ImageIO on this blog post . 在这篇博客文章中有一个如何使用ImageIO进行操作的示例。

There are other libraries that you might also want to consider depending on your needs. 根据您的需要,您可能还需要考虑其他库。 For example MetadataExtractor or Sanselan . 例如MetadataExtractorSanselan

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. 这包括jpeg图像中使用的EXIF元数据格式。 The library has good Javadoc style documentation . 该库具有良好的Javadoc样式文档

The primary entry point into the library is the ImageMetadataReader object. 进入库的主要入口点是ImageMetadataReader对象。

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. 入门”页面有一个很好的介绍,包括如何从EXIF格式元数据中获取特定标记的值的一个很好的示例。

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 . 替代方案包括内置的java ImageIO库和Sanselan

jhead是一个JPEG EXIF标题操作工具,是另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM