Example usage for org.apache.commons.imaging.formats.jpeg JpegImageMetadata findEXIFValueWithExactMatch

List of usage examples for org.apache.commons.imaging.formats.jpeg JpegImageMetadata findEXIFValueWithExactMatch

Introduction

In this page you can find the example usage for org.apache.commons.imaging.formats.jpeg JpegImageMetadata findEXIFValueWithExactMatch.

Prototype

public TiffField findEXIFValueWithExactMatch(final TagInfo tagInfo) 

Source Link

Usage

From source file:net.tourbook.photo.Photo.java

/**
 * Image direction/*  w  w w . j  av  a2 s  .co m*/
 * 
 * @param tagInfo
 */
private double getExifValueDouble(final JpegImageMetadata jpegMetadata, final TagInfo tagInfo) {
    try {
        final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tagInfo);
        if (field != null) {
            return field.getDoubleValue();
        }
    } catch (final Exception e) {
        // ignore
    }

    return Double.MIN_VALUE;
}

From source file:net.tourbook.photo.Photo.java

/**
 * GPS area info/*  www . jav  a2  s .  co m*/
 */
private String getExifValueGpsArea(final JpegImageMetadata jpegMetadata) {

    try {
        final TiffField field = jpegMetadata
                .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_AREA_INFORMATION);
        if (field != null) {
            final Object fieldValue = field.getValue();
            if (fieldValue != null) {

                /**
                 * source: Exif 2.2 specification
                 * 
                 * <pre>
                 * 
                 * Table 6 Character Codes and their Designation
                 * 
                 * Character Code   Code Designation (8 Bytes)                   References
                 * ASCII           41.H, 53.H, 43.H, 49.H, 49.H, 00.H, 00.H, 00.H  ITU-T T.50 IA5
                 * JIS            A.H, 49.H, 53.H, 00.H, 00.H, 00.H, 00.H, 00.H   JIS X208-1990
                 * Unicode         55.H, 4E.H, 49.H, 43.H, 4F.H, 44.H, 45.H, 00.H  Unicode Standard
                 * Undefined      00.H, 00.H, 00.H, 00.H, 00.H, 00.H, 00.H, 00.H  Undefined
                 * 
                 * </pre>
                 */
                final byte[] byteArrayValue = field.getByteArrayValue();
                final int fieldLength = byteArrayValue.length;

                if (fieldLength > 0) {

                    /**
                     * <pre>
                     * 
                     * skipping 1 + 6 characters:
                     * 
                     * 1      character code
                     * 2...7  have no idea why these bytes are set to none valid characters
                     * 
                     * </pre>
                     */
                    final byte[] valueBytes = Arrays.copyOfRange(byteArrayValue, 7, fieldLength);

                    String valueString = null;

                    final byte firstByte = byteArrayValue[0];
                    if (firstByte == 0x55) {

                        valueString = new String(valueBytes, UI.UTF_16);

                    } else {

                        valueString = new String(valueBytes);
                    }

                    return valueString;
                }
            }
        }
    } catch (final Exception e) {
        // ignore
    }

    return null;
}

From source file:net.tourbook.photo.Photo.java

private int getExifValueInt(final JpegImageMetadata jpegMetadata, final TagInfo tiffTag,
        final int defaultValue) {

    try {/*  w  w  w  .  j  a  v a 2 s . c o m*/
        final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tiffTag);
        if (field != null) {
            return field.getIntValue();
        }
    } catch (final Exception e) {
        // ignore
    }

    return defaultValue;
}

From source file:net.tourbook.photo.Photo.java

private String getExifValueString(final JpegImageMetadata jpegMetadata, final TagInfo tagInfo) {

    try {/*w  w  w .j av a2 s .c om*/
        final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tagInfo);
        if (field != null) {
            return field.getStringValue();
        }
    } catch (final Exception e) {
        // ignore
    }

    return null;
}

From source file:org.apache.commons.imaging.examples.MetadataExample.java

public static void metadataExample(final File file) throws ImageReadException, IOException {
    // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
    final ImageMetadata metadata = Imaging.getMetadata(file);

    // System.out.println(metadata);

    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  .ja  v a  2 s .  c om
        // see the TiffConstants file for a list of TIFF tags.

        System.out.println("file: " + file.getPath());

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

        System.out.println();

        // 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();

                System.out.println("    " + "GPS Description: " + gpsDescription);
                System.out.println("    " + "GPS Longitude (Degrees East): " + longitude);
                System.out.println("    " + "GPS Latitude (Degrees North): " + latitude);
            }
        }

        // 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

            System.out.println("    " + "GPS Latitude: " + gpsLatitudeDegrees.toDisplayString() + " degrees, "
                    + gpsLatitudeMinutes.toDisplayString() + " minutes, " + gpsLatitudeSeconds.toDisplayString()
                    + " seconds " + gpsLatitudeRef);
            System.out.println("    " + "GPS Longitude: " + gpsLongitudeDegrees.toDisplayString() + " degrees, "
                    + gpsLongitudeMinutes.toDisplayString() + " minutes, "
                    + gpsLongitudeSeconds.toDisplayString() + " seconds " + gpsLongitudeRef);

        }

        System.out.println();

        final List<ImageMetadataItem> items = jpegMetadata.getItems();
        for (int i = 0; i < items.size(); i++) {
            final ImageMetadataItem item = items.get(i);
            System.out.println("    " + "item: " + item);
        }

        System.out.println();
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.oauth.UploadTest.java

/**
 * Tests the {@link UploadUtils#updateFile(MapillaryImportedImage)} method.
 *//*from   www  .j a  v a  2 s . c o m*/
@Test
public void updateFileTest() throws IOException {
    File image = new File("images/icon16.png");
    MapillaryImportedImage img = ImageUtil.readImagesFrom(image, new LatLon(0, 0)).get(0);
    File updatedFile = null;
    try {
        updatedFile = UploadUtils.updateFile(img);
        ImageMetadata metadata = Imaging.getMetadata(updatedFile);
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
        assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF) != null);
        assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE) != null);
        assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF) != null);
        assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE) != null);
        assertTrue(jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION) != null);
        assertTrue(
                jpegMetadata.findEXIFValueWithExactMatch(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL) != null);
        assertEquals(0,
                MapillaryUtils.degMinSecToDouble(
                        (RationalNumber[]) jpegMetadata
                                .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE).getValue(),
                        jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF)
                                .getValue().toString()),
                0.01);
        assertEquals(0,
                MapillaryUtils.degMinSecToDouble(
                        (RationalNumber[]) jpegMetadata
                                .findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE).getValue(),
                        jpegMetadata.findEXIFValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF)
                                .getValue().toString()),
                0.01);

    } catch (ImageReadException | ImageWriteException | IOException e) {
        fail();
    } finally {
        updatedFile.delete();
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.utils.ImageImportUtil.java

private static Object getTiffFieldValue(JpegImageMetadata meta, TagInfo tag) {
    TiffField field = meta.findEXIFValueWithExactMatch(tag);
    if (field != null) {
        try {/*from w w  w. j a va2s  . c om*/
            return field.getValue();
        } catch (ImageReadException e) {
            // If value couldn't be read, assume it's not set.
        }
    }
    return null;
}

From source file:zz.filecollector.fileprocessor.JpgPhotoProcessor.java

@Override
public void extractFileInfo(File file, FileInfo info) {
    FileProcessor.getBasicFileInfo(file, info);
    if (info.getFileType() == FileInfo.UNKNOWN) {
        info.setFileType(FileInfo.PHOTO);
    }/*ww  w .  j  av a 2s  . c o m*/

    try {
        ImageMetadata metadata = Imaging.getMetadata(file);
        if (metadata instanceof JpegImageMetadata) {
            final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

            if (info.getCreateDate() == null) {
                try {
                    TiffField dateTime = jpegMetadata
                            .findEXIFValueWithExactMatch(TiffTagConstants.TIFF_TAG_DATE_TIME);
                    if (dateTime != null) {
                        String str = dateTime.getStringValue(); // 2014:02:06 12:11:26
                        if (StringUtils.isNotEmpty(str)) {
                            Date date = dateFormat.parse(str);
                            info.setCreateDate(date);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (info.getDevice() == null) {
                TiffField model = jpegMetadata.findEXIFValueWithExactMatch(TiffTagConstants.TIFF_TAG_MODEL);
                if (model != null) {
                    byte[] bytes = model.getByteArrayValue();
                    String modelStr = new String(bytes).trim();
                    if (StringUtils.isNotEmpty(modelStr)) {
                        info.setDevice(modelStr);
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}