Esempio n. 1
0
  public Date getPhotographyDate(final File file) throws ImageReadException, IOException {
    // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
    final ImageMetadata metadata = Imaging.getMetadata(file);

    String rtnValue = "";

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

      // print out various interesting EXIF tags.
      rtnValue = getTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);

      if (null == rtnValue || rtnValue.isEmpty()) {
        rtnValue = getTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED);
      }

      if (null == rtnValue || rtnValue.isEmpty()) {
        rtnValue = getTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_DATE_TIME);
      }

      if (null == rtnValue || rtnValue.isEmpty()) {
        return null;
      }
    }
    rtnValue = rtnValue.replace("'", "");

    return getDateFromTiffDateStr(rtnValue);
  }
Esempio n. 2
0
  public boolean getGpsFromJpgAndSet(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.
      //
      // see the TiffConstants file for a list of TIFF tags.

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

      // print out various interesting EXIF tags.
      this.lat_ref =
          Double.parseDouble(getTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF));
      this.lat =
          Double.parseDouble(getTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE));
      this.lon_ref =
          Double.parseDouble(getTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF));
      this.lon =
          Double.parseDouble(getTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE));

      return true;
    }

    return false;
  }
Esempio n. 3
0
  public void showGpsData(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.
      //
      // see the TiffConstants file for a list of TIFF tags.

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

      // print out various interesting EXIF tags.
      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);

      System.out.println();
    }
  }
Esempio n. 4
0
 /**
  * Writes an image to a file. Uses the Apache Commons Imaging library to write to a file.
  *
  * @param fileName The file name and location to write to.
  */
 public void writeImage(String fileName) {
   try {
     Imaging.writeImage(image, new File(fileName), IMAGE_FORMAT_PNG, null);
   } catch (ImageWriteException e) {
     e.printStackTrace(); // To change body of catch statement use File | Settings | File
     // Templates.
   } catch (IOException e) {
     e.printStackTrace(); // To change body of catch statement use File | Settings | File
     // Templates.
   }
 }
Esempio n. 5
0
 /**
  * Create an EditableImage from a given image file. Uses the Apache Commons Imaging library to
  * read into a BufferedImage.
  *
  * @param file The image file.
  */
 public EditableImage(File file) {
   try {
     this.image = Imaging.getBufferedImage(file);
     if (image == null) {
       throw new IOException();
     }
   } catch (IOException e) {
     System.out.println("Could not load image file " + file);
     e.printStackTrace();
   } catch (ImageReadException e) {
     System.out.println("Could not load image file " + file);
     e.printStackTrace();
   }
 }
Esempio n. 6
0
  public static void printMetaData(final File file)
      throws ImageReadException, IOException, IllegalArgumentException, IllegalAccessException {

    // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
    ImageMetadata metadata = Imaging.getMetadata(file);

    if (!(metadata instanceof JpegImageMetadata)) {
      throw new RuntimeException("Only support " + JpegImageMetadata.class.getSimpleName());
    }

    JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

    // JPEG EXIF metadata
    List<Field> exifTagfields = getTagInfoDefinedInClass(ExifTagConstants.class);
    System.out.println("=== " + ExifTagConstants.class.getSimpleName() + " ===");
    printTagInfos(exifTagfields, jpegMetadata);

    // Tiff metadata
    List<Field> tiffTagfields = getTagInfoDefinedInClass(TiffTagConstants.class);
    System.out.println("=== " + TiffTagConstants.class.getSimpleName() + " ===");
    printTagInfos(tiffTagfields, jpegMetadata);

    // Gps metadata
    List<Field> gpsTagfields = getTagInfoDefinedInClass(GpsTagConstants.class);
    System.out.println("=== " + GpsTagConstants.class.getSimpleName() + " ===");
    printTagInfos(gpsTagfields, jpegMetadata);

    // simple interface to EXIF jpegMetadata
    TiffImageMetadata exifMetadata = jpegMetadata.getExif();
    if (null != exifMetadata) {
      System.out.println("=== ExifMetadata ===");
      jpegMetadata.getExif().getAllFields().stream().forEach(System.out::println);
      System.out.println();
    }

    // All IImageMetadataItem
    List<ImageMetadataItem> items = jpegMetadata.getItems();
    printIImageMetadataItems(items);
  }
Esempio n. 7
0
  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.
      //
      // 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();
    }
  }