@Nullable public static Geolocation getGeolocation(@Nullable final byte[] imageBytes) throws ImageProcessingException, IOException { if (imageBytes != null && imageBytes.length > 0) { final Metadata metadata = ImageMetadataReader.readMetadata(new BufferedInputStream(new ByteArrayInputStream(imageBytes)), false); if (metadata != null) { final GpsDirectory gpsDirectory = metadata.getDirectory(GpsDirectory.class); if (gpsDirectory != null) { return new GeolocationImpl(gpsDirectory); } } } return null; }
@Override public void initialize(String file) throws ServiceException, IOException { try { File jpegFile = new File(file); Metadata metadata = ImageMetadataReader.readMetadata(jpegFile); directory = metadata.getDirectory(ExifSubIFDDirectory.class); } catch (Throwable e) { throw new ServiceException("Could not read EXIF data", e); } }
@Override public void initialize(String file) throws ServiceException, IOException { try { File jpegFile = new File(file); Metadata metadata = ImageMetadataReader.readMetadata(jpegFile); directory = metadata.getDirectory(ExifSubIFDDirectory.class); } catch (Throwable e) { throw new ServiceException("Could not read EXIF data", e); } }
/** * Tries to read the orientation data from the given image {@link Metadata}. Returns <code>null</code> if not * found or if an error occurs. */ @Nullable public static ImageOrientation getOrientation(@Nullable final Metadata metadata) { if (metadata != null) { final Directory exifIfd0Directory = metadata.getDirectory(ExifIFD0Directory.class); if (exifIfd0Directory != null) { try { return findById(exifIfd0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION)); } catch (Exception e) { LOG.info("ImageOrientation.getOrientation(): Exception while trying to read the orientation data from the EXIF. Ignoring and just returning null"); } } } return null; }
/*************************************************************************** * * **************************************************************************/ private static Directory getIPTCDirectory (final Node node) { if (node.getNodeName().equals("unknown")) { if (Integer.parseInt(node.getAttributes().getNamedItem("MarkerTag").getNodeValue()) == IPTC) { final byte[] data = (byte[])((IIOMetadataNode)node).getUserObject(); final Metadata metadata = new Metadata(); new ExifReader().extract(data, metadata); return metadata.getDirectory(IptcDirectory.class); } } Node child = node.getFirstChild(); while (child != null) { final Directory directory = getIPTCDirectory(child); if (directory != null) { return directory; } child = child.getNextSibling(); } return null; } }
/*************************************************************************** * * **************************************************************************/ private static Directory getEXIFDirectory (final Node node) { if (node.getNodeName().equals("unknown")) { if (Integer.parseInt(node.getAttributes().getNamedItem("MarkerTag").getNodeValue()) == EXIF) { final byte[] data = (byte[])((IIOMetadataNode)node).getUserObject(); final Metadata metadata = new Metadata(); new ExifReader().extract(data, metadata); return metadata.getDirectory(ExifSubIFDDirectory.class); } } Node child = node.getFirstChild(); while (child != null) { final Directory directory = getEXIFDirectory(child); if (directory != null) { return directory; } child = child.getNextSibling(); } return null; }
ExifSubIFDDirectory exifSubIFDDirectory = fileMetadata.getDirectory(ExifSubIFDDirectory.class); ExifIFD0Directory exifIFD0Directory = fileMetadata.getDirectory(ExifIFD0Directory.class);
ExifSubIFDDirectory exifSubIFDDirectory = fileMetadata.getDirectory(ExifSubIFDDirectory.class); ExifIFD0Directory exifIFD0Directory = fileMetadata.getDirectory(ExifIFD0Directory.class);
/** * From the extracted metadata object, retrieve information about the file copyright and put it into a Copyright. * <p/> * Field being read is: {@code Copyright} * * {@link AttributeSet} * @param fileMetadata * @return An {@link AttributeSet} containing copyright attribute * @return {@code null} when no copyright information was found */ protected AttributeSet extractCopyrightMetadata(Metadata fileMetadata) { AttributeSet copyrightAttributeSet = null; ExifIFD0Directory exifIFD0Directory = fileMetadata.getDirectory(ExifIFD0Directory.class); String copyrightStatement = exifIFD0Directory.getDescription(ExifIFD0Directory.TAG_COPYRIGHT); if (copyrightStatement != null) { Attribute copyrightAttribute = new MetadataAttribute(MetadataAttributeName.COPY_RIGHT_NOTE, MetadataAttributeType.STRING, copyrightStatement); copyrightAttributeSet = new MetadataAttributeSet(MetadataAttributeSetName.COPY_RIGHT_METADATA); copyrightAttributeSet.getAttributes().add(copyrightAttribute); } return copyrightAttributeSet; }
/** * From the extracted metadata object, retrieve information about the file copyright and put it into a Copyright. * <p/> * Field being read is: {@code Copyright} * * {@link AttributeSet} * @param fileMetadata * @return An {@link AttributeSet} containing copyright attribute * @return {@code null} when no copyright information was found */ protected AttributeSet extractCopyrightMetadata(Metadata fileMetadata) { AttributeSet copyrightAttributeSet = null; ExifIFD0Directory exifIFD0Directory = fileMetadata.getDirectory(ExifIFD0Directory.class); String copyrightStatement = exifIFD0Directory.getDescription(ExifIFD0Directory.TAG_COPYRIGHT); if (copyrightStatement != null) { Attribute copyrightAttribute = new MetadataAttribute(MetadataAttributeName.COPY_RIGHT_NOTE, MetadataAttributeType.STRING, copyrightStatement); copyrightAttributeSet = new MetadataAttributeSet(MetadataAttributeSetName.COPY_RIGHT_METADATA); copyrightAttributeSet.getAttributes().add(copyrightAttribute); } return copyrightAttributeSet; }
final DirectoryDrewAdapter exifAdatpter = new DirectoryDrewAdapter(metadata.getDirectory(ExifSubIFDDirectory.class)); exif.loadFromAdapter(exifAdatpter); final DirectoryDrewAdapter iptcAdatpter = new DirectoryDrewAdapter(metadata.getDirectory(IptcDirectory.class)); iptc.loadFromAdapter(iptcAdatpter); final XmpDirectory xmpDirectory = metadata.getDirectory(XmpDirectory.class); final DirectoryDrewAdapter xmpAdatpter = new DirectoryDrewAdapter(xmpDirectory); xmp.loadFromAdapter(xmpAdatpter);
ExifSubIFDDirectory exifSubIFDDirectory = fileMetadata.getDirectory(ExifSubIFDDirectory.class); ExifIFD0Directory exifIFD0Directory = fileMetadata.getDirectory(ExifIFD0Directory.class); GpsDirectory gpsDirectory = fileMetadata.getDirectory(GpsDirectory.class);
ExifSubIFDDirectory exifSubIFDDirectory = fileMetadata.getDirectory(ExifSubIFDDirectory.class); ExifIFD0Directory exifIFD0Directory = fileMetadata.getDirectory(ExifIFD0Directory.class); GpsDirectory gpsDirectory = fileMetadata.getDirectory(GpsDirectory.class);
GpsDirectory gpsDirectory = fileMetadata.getDirectory(GpsDirectory.class); AttributeSet attributeSet = new MetadataAttributeSet(MetadataAttributeSetName.SPATIAL_METADATA); Attribute spatialAttribute;
GpsDirectory gpsDirectory = fileMetadata.getDirectory(GpsDirectory.class); AttributeSet attributeSet = new MetadataAttributeSet(MetadataAttributeSetName.SPATIAL_METADATA); Attribute spatialAttribute;
private boolean processUsingAdvancedMetadata( Node imageNode, Binary binaryValue, boolean imageParsedUsingDefaultMetadata ) throws Exception { try (InputStream stream = binaryValue.getStream()) { Metadata advancedMetadata = ImageMetadataReader.readMetadata(new BufferedInputStream(stream), false); ExifIFD0Directory exifIFD0Directory = advancedMetadata.getDirectory(ExifIFD0Directory.class); if (exifIFD0Directory == null || !hasTags(exifIFD0Directory, EXIF_TAGS)) { if (!imageParsedUsingDefaultMetadata) { getLogger().info("Neither default nor advanced metadata parser can resolve image. Ignoring sequencing"); } getLogger().debug("No relevant IFD0 information found, ignoring EXIF node."); return imageParsedUsingDefaultMetadata; } if (!imageParsedUsingDefaultMetadata) { //it's a format not supported by the default metadata and since we're reading the IFD0 descriptor, mark the image //as a TIFF getLogger().info( "Image has IFD0 block information but is not one of the standard image types, marking it as TIFF"); imageNode.setProperty(ImageMetadataLexicon.FORMAT_NAME, TIFF_FORMAT); imageNode.setProperty(JcrConstants.JCR_MIME_TYPE, IMAGE_TIFF_DEFAULT_MIME_TYPE); } addEXIFNode(imageNode, exifIFD0Directory); return true; } catch (Exception e) { getLogger().debug(e, "Cannot process image for advanced metadata"); return imageParsedUsingDefaultMetadata; } }
private boolean processUsingAdvancedMetadata( Node imageNode, Binary binaryValue, boolean imageParsedUsingDefaultMetadata ) throws Exception { try (InputStream stream = binaryValue.getStream()) { Metadata advancedMetadata = ImageMetadataReader.readMetadata(new BufferedInputStream(stream), false); ExifIFD0Directory exifIFD0Directory = advancedMetadata.getDirectory(ExifIFD0Directory.class); if (exifIFD0Directory == null || !hasTags(exifIFD0Directory, EXIF_TAGS)) { if (!imageParsedUsingDefaultMetadata) { getLogger().info("Neither default nor advanced metadata parser can resolve image. Ignoring sequencing"); } getLogger().debug("No relevant IFD0 information found, ignoring EXIF node."); return imageParsedUsingDefaultMetadata; } if (!imageParsedUsingDefaultMetadata) { //it's a format not supported by the default metadata and since we're reading the IFD0 descriptor, mark the image //as a TIFF getLogger().info( "Image has IFD0 block information but is not one of the standard image types, marking it as TIFF"); imageNode.setProperty(ImageMetadataLexicon.FORMAT_NAME, TIFF_FORMAT); imageNode.setProperty(JcrConstants.JCR_MIME_TYPE, IMAGE_TIFF_DEFAULT_MIME_TYPE); } addEXIFNode(imageNode, exifIFD0Directory); return true; } catch (Exception e) { getLogger().debug(e, "Cannot process image for advanced metadata"); return imageParsedUsingDefaultMetadata; } }