Example usage for org.apache.commons.imaging Imaging getMetadata

List of usage examples for org.apache.commons.imaging Imaging getMetadata

Introduction

In this page you can find the example usage for org.apache.commons.imaging Imaging getMetadata.

Prototype

public static IImageMetadata getMetadata(final File file) throws ImageReadException, IOException 

Source Link

Document

Parses the metadata of an image file.

Usage

From source file:it.inserpio.mapillary.gopro.importer.exif.EXIFPropertyReader.java

public static long getDateTimeOriginal(File imageFile) throws ImageReadException, IOException, ParseException {
    final IImageMetadata metadata = Imaging.getMetadata(imageFile);
    final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

    String dateTimeOriginal = jpegMetadata.getExif()
            .getFieldValue(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)[0];

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

    return dateFormat.parse(dateTimeOriginal).getTime();
}

From source file:it.inserpio.mapillary.gopro.importer.exif.EXIFPropertyWriter.java

public static void setExifGPSTag(File jpegImageFile, File jpegImageOutputFile, Coordinates coordinates)
        throws IOException, ImageReadException, ImageWriteException {
    OutputStream os = null;//w w w.  j  a  va2 s  . co m

    boolean canThrow = false;

    try {
        TiffOutputSet outputSet = null;

        final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

        if (jpegMetadata != null) {
            final TiffImageMetadata exif = jpegMetadata.getExif();

            if (exif != null) {
                outputSet = exif.getOutputSet();
            }
        }

        if (outputSet == null) {
            outputSet = new TiffOutputSet();
        }

        outputSet.setGPSInDegrees(coordinates.getLongitude(), coordinates.getLatitude());

        outputSet.getGPSDirectory().removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF);
        outputSet.getGPSDirectory().add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF,
                GpsTagConstants.GPS_TAG_GPS_DEST_BEARING_REF_VALUE_TRUE_NORTH);

        outputSet.getGPSDirectory().removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION);
        outputSet.getGPSDirectory().add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION,
                new RationalNumber[] { RationalNumber.valueOf(coordinates.getDirection()) });

        //outputSet.getRootDirectory().removeField(305);

        os = new FileOutputStream(jpegImageOutputFile);
        os = new BufferedOutputStream(os);

        new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet);

        canThrow = true;
    } finally {
        IoUtils.closeQuietly(canThrow, os);
    }
}

From source file:MetadataExample.java

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

    if (metadata instanceof JpegImageMetadata) {
        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.
        //// ww w  .j a v  a  2s . c o m
        // 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_DATE_TIME);
            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);
            printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_ORIENTATION);
        }
        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);

        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<IImageMetadataItem> items = jpegMetadata.getItems();
        for (int i = 0; i < items.size(); i++) {
            final TiffImageMetadata.Item item = (Item) items.get(i);
            System.out.println("    " + "item: " + item + "::" + item.getClass());
        }

        System.out.println();
    }
}

From source file:de.tehame.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.  j a  v  a 2  s.com
        // 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:it.inserpio.mapillary.gopro.importer.exif.EXIFPropertyReaderTest.java

@Test
public void shouldPrintImageMetadata() {
    try {/*  w w w.j av  a 2 s  .  com*/
        File jpegImageFile = new File("src/test/resources", "IMG_2096.JPG");

        final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

        System.out.println(jpegMetadata);
    } catch (Exception e) {
        e.printStackTrace();

        Assert.fail();
    }
}

From source file:de.aikiit.fotorenamer.image.MetaDataExtractor.java

/**
 * Returns the requested tag as String from the image file.
 *
 * @param image Image file to extract Metadata from.
 * @param tag   Tag to extract from the given file, @see TiffConstants
 * @return Returns exif tag value, in case of any errors the value is an
 * empty String.// ww w .j  a v a 2 s . c  o m
 * @throws IOException        if file cannot be accessed.
 * @throws ImageReadException if an error occurred during image processing.
 */
public static String getExifMetadata(final File image, final TagInfo tag)
        throws IOException, ImageReadException {
    assert image != null : "Parameter image must not be null";
    assert tag != null : "Parameter tag must not be null";

    String result = EMPTY_STRING;
    ImageMetadata metadata = Imaging.getMetadata(image);

    /*
            for(ImageMetadata.ImageMetadataItem item:metadata.getItems()) {
    System.out.println("WWW: " + item.getSelectedDirectory());
            }
      */
    if (metadata instanceof JpegImageMetadata) {
        JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
        TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tag);
        if (field != null) {
            result = field.getValueDescription();
            LOG.info("extraction of " + tag.getDescription() + " yields " + result);
        }
    }
    return (result == null ? EMPTY_STRING : result);
}

From source file:it.inserpio.mapillary.gopro.importer.exif.EXIFPropertyReaderTest.java

@Test
public void shouldPrintImageMetadata2() {
    try {/*from   w ww.j  ava2  s.c  o m*/
        File jpegImageFile = new File("src/test/resources", "IMG_4303.JPG");

        final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

        System.out.println(jpegMetadata);
    } catch (Exception e) {
        e.printStackTrace();

        Assert.fail();
    }
}

From source file:it.inserpio.mapillary.gopro.importer.exif.EXIFPropertyReaderTest.java

@Test
public void shouldPrintImageMetadata3() {
    try {/*from   ww  w .  j  av  a2 s  .com*/
        File jpegImageFile = new File("src/test/resources", "CIMG6404.JPG");

        final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

        System.out.println(jpegMetadata);
    } catch (Exception e) {
        e.printStackTrace();

        Assert.fail();
    }
}

From source file:com.cons.gps2exif.exif.ReadMetaData.java

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

    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 ww w.  j ava  2  s  .com*/
        // see the TiffConstants file for a list of TIFF tags.

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

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

        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:joachimeichborn.geotag.io.jpeg.PictureMetadataReader.java

/**
 * Constructor//  w ww  . j av  a 2s  .  co m
 * 
 * @param aPicture
 *            The pictureFile whose meta data is represented by the
 *            constructed object
 */
public PictureMetadataReader(final Path aPicture) {
    pictureFile = aPicture;
    ImageMetadata metadata = null;
    try {
        metadata = Imaging.getMetadata(aPicture.toFile());
    } catch (ImageReadException | IOException e) {
        logger.severe("Could not get metadata section from file '" + pictureFile + "': " + e.getMessage());
    }

    if (metadata instanceof JpegImageMetadata) {
        jpegMetadata = (JpegImageMetadata) metadata;
    } else {
        jpegMetadata = null;
        logger.warning("Could not get JPEG metadate for file " + pictureFile);
    }
}