Example usage for org.apache.commons.imaging.formats.tiff TiffField getDirectoryType

List of usage examples for org.apache.commons.imaging.formats.tiff TiffField getDirectoryType

Introduction

In this page you can find the example usage for org.apache.commons.imaging.formats.tiff TiffField getDirectoryType.

Prototype

public int getDirectoryType() 

Source Link

Usage

From source file:name.hampton.mike.gallery.MetadataExtractor.java

public Map<String, Object> extractImageMetaData(final InputStream inputStream, Map<String, Object> metadataH)
        throws ImageReadException, IOException {
    // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
    final ImageMetadata metadata = Imaging.getMetadata(inputStream, null);

    if (metadata instanceof JpegImageMetadata) {
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

        // Jpeg EXIF metadata is stored in a TIFF-based directory structure
        // and is identified with TIFF tags.
        // Here we look for the "x resolution" tag, but
        // we could just as easily search for any other tag.
        ////from   w w w  .jav a  2  s  .com
        // see the TiffConstants file for a list of TIFF tags.

        // out.println("file: " + file.getPath());
        JpegPhotoshopMetadata photoshopmetadata = jpegMetadata.getPhotoshop();
        metadataH.put("photoshop", photoshopmetadata);
        BufferedImage thumbnail = jpegMetadata.getEXIFThumbnail();
        metadataH.put("hasEXIFThumbnail", null != thumbnail);

        Map<String, Object> jpegMetadataH = new HashMap<String, Object>();
        metadataH.put("jpegMetadata", jpegMetadataH);

        // print out various interesting EXIF tags.
        recordTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_XRESOLUTION, jpegMetadataH);
        recordTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_DATE_TIME, jpegMetadataH);
        recordTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, jpegMetadataH);
        recordTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED, jpegMetadataH);
        recordTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_ISO, jpegMetadataH);
        recordTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_SHUTTER_SPEED_VALUE, jpegMetadataH);
        recordTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_APERTURE_VALUE, jpegMetadataH);
        recordTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_BRIGHTNESS_VALUE, jpegMetadataH);
        recordTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF, jpegMetadataH);
        recordTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE, jpegMetadataH);
        recordTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF, jpegMetadataH);
        recordTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE, jpegMetadataH);

        // simple interface to GPS data
        final TiffImageMetadata exifMetadata = jpegMetadata.getExif();
        if (null != exifMetadata) {
            final TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
            if (null != gpsInfo) {
                final String gpsDescription = gpsInfo.toString();
                final double longitude = gpsInfo.getLongitudeAsDegreesEast();
                final double latitude = gpsInfo.getLatitudeAsDegreesNorth();

                jpegMetadataH.put("GPSDescription", gpsDescription);
                jpegMetadataH.put("GPSLongitude", longitude);
                jpegMetadataH.put("GPSLatitude", latitude);
            }
            List<TiffField> allFields = exifMetadata.getAllFields();
            List<Map<String, Object>> allFieldsL = new ArrayList<Map<String, Object>>();
            jpegMetadataH.put("allFields", allFieldsL);
            if (null != allFields) {
                for (TiffField field : allFields) {
                    Map<String, Object> fieldH = new HashMap<String, Object>();
                    allFieldsL.add(fieldH);
                    fieldH.put("fieldType", field.getFieldTypeName());
                    if (field.getFieldType().equals(FieldType.RATIONAL)) {
                        // These fields format oddly, must be strings
                        fieldH.put("value", field.getValue().toString());
                    }
                    fieldH.put("descriptionWithoutValue", field.getDescriptionWithoutValue());
                    fieldH.put("directoryType", field.getDirectoryType());
                    fieldH.put("tagName", field.getTagName());
                    fieldH.put("valueDescription", field.getValueDescription());
                }
            }
        }

        // more specific example of how to manually access GPS values
        final TiffField gpsLatitudeRefField = jpegMetadata
                .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
        final TiffField gpsLatitudeField = jpegMetadata
                .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE);
        final TiffField gpsLongitudeRefField = jpegMetadata
                .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
        final TiffField gpsLongitudeField = jpegMetadata
                .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
        if (gpsLatitudeRefField != null && gpsLatitudeField != null && gpsLongitudeRefField != null
                && gpsLongitudeField != null) {
            // all of these values are strings.
            final String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
            final RationalNumber gpsLatitude[] = (RationalNumber[]) (gpsLatitudeField.getValue());
            final String gpsLongitudeRef = (String) gpsLongitudeRefField.getValue();
            final RationalNumber gpsLongitude[] = (RationalNumber[]) gpsLongitudeField.getValue();

            final RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
            final RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
            final RationalNumber gpsLatitudeSeconds = gpsLatitude[2];

            final RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
            final RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
            final RationalNumber gpsLongitudeSeconds = gpsLongitude[2];

            // This will format the gps info like so:
            //
            // gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S
            // gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E

            Map<String, Object> gpsH = new HashMap<String, Object>();
            jpegMetadataH.put("gps", gpsH);
            Map<String, Object> gpsLatitudeH = new HashMap<String, Object>();
            Map<String, Object> gpsLongitudeH = new HashMap<String, Object>();
            gpsH.put("latitude", gpsLatitudeH);
            gpsH.put("longitude", gpsLongitudeH);

            gpsLatitudeH.put("degrees", gpsLatitudeDegrees.toDisplayString());
            gpsLatitudeH.put("minutes", gpsLatitudeMinutes.toDisplayString());
            gpsLatitudeH.put("seconds", gpsLatitudeSeconds.toDisplayString());
            gpsLatitudeH.put("display", gpsLatitudeRef);
            gpsLongitudeH.put("degrees", gpsLongitudeDegrees.toDisplayString());
            gpsLongitudeH.put("minutes", gpsLongitudeMinutes.toDisplayString());
            gpsLongitudeH.put("seconds", gpsLongitudeSeconds.toDisplayString());
            gpsLongitudeH.put("display", gpsLongitudeRef);

        }
    }
    return metadataH;
}