3

我正在尝试创建一个从图像文件中提取元数据的程序。到目前为止,我已经设法创建了一个打印出所有元数据的程序,但我不知道如何指定程序只打印出某些东西(文件名、纬度、经度、访问日期)。在过去的四个小时里,我一直在努力解决这个问题。这是我到目前为止的代码......

主类:

package metadata;
import com.drew.metadata.exif.*; 
import com.drew.metadata.iptc.*; 
import com.drew.metadata.jpeg.*;
public class MetaData {

    public static void main(String[] args) {

        ExtractTags extractAllTags = new ExtractTags();

        //extractAllTags.getTags();
        System.out.println("\n\n\n Has this ");
        extractAllTags.getLatitude();     
    }
}

二等:

package metadata;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.GpsDirectory;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.drew.metadata.exif.*; 
import com.drew.metadata.iptc.*; 
import com.drew.metadata.jpeg.*;

public class ExtractTags {

    String allTags;
    String latitude;
    File jpegFile = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\HTC Desire.jpg");

    public String getTags() {

        try {
            Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);

            for (Directory directory : metadata.getDirectories()) {
                for (Tag allTags : directory.getTags()) {

                    System.out.println(allTags);
                }
            }
        } catch (ImageProcessingException ex) {
            Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return allTags;
    }

    public String getLatitude() {
        try {
            Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);
            if (metadata.containsDirectory(GpsDirectory.class)) {
                GpsDirectory gpsDir = (GpsDirectory) metadata.getDirectory(GpsDirectory.class);
                GpsDescriptor gpsDesc = new GpsDescriptor(gpsDir);
                System.out.println("Latitude: " + gpsDesc.getGpsLatitudeDescription());
            }
        } catch (ImageProcessingException ex) {
            Logger.getLogger(ExtractTags.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Error 1");
        } catch (IOException ex) {
            Logger.getLogger(ExtractTags.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Error 2");
        }

        return latitude;
    }
}

错误:

Exception in thread "main" java.lang.NoClassDefFoundError: metadata/GpsDescriptor
    at metadata.ExtractTags.getLatitude(ExtractTags.java:47)
    at metadata.MetaData.main(MetaData.java:13)
Caused by: java.lang.ClassNotFoundException: metadata.GpsDescriptor
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 2 more
4

1 回答 1

4

您可以Directory使用Metadata.getDirectory(Class). 每种类型Directory都有一个关联Descriptor,您可以使用它来解释Directory.

例如,纬度可以通过GpsDirectory使用GpsDescriptor这样的方法获得:

GpsDirectory gpsDir = (GpsDirectory) metadata.getFirstDirectoryOfType(GpsDirectory.class);

if (gpsDir != null) {
    GpsDescriptor gpsDesc = new GpsDescriptor(gpsDir);
    System.out.println("Latitude: " + gpsDesc.getGpsLatitudeDescription());
}

请参阅您正在使用的特定类型的文档,Descriptor以查看它提供的用于获取所需数据的方法。

于 2012-04-15T21:57:10.957 回答