private Map<String, String> getTags(Integer max, Metadata metadata) { Map<String, String> results = new HashMap<>(); int i =0; for (Directory directory : metadata.getDirectories()) { for (Tag tag : directory.getTags()) { results.put(directory.getName() + "." + tag.getTagName(), tag.getDescription()); if(max!=null) { i++; if (i >= max) { return results; } } } } return results; } }
directory.setString(tagIdentifier, ""); return; directory.setString(tagIdentifier, charsetName); return; case IptcDirectory.TAG_ENVELOPE_RECORD_VERSION: int shortValue = reader.getUInt16(); reader.skip(tagByteCount - 2); directory.setInt(tagIdentifier, shortValue); return; directory.setInt(tagIdentifier, reader.getUInt8()); reader.skip(tagByteCount - 1); return; String charSetName = directory.getString(IptcDirectory.TAG_CODED_CHARACTER_SET); Charset charset = null; try { if (directory.containsTag(tagIdentifier)) { StringValue[] oldStrings = directory.getStringValueArray(tagIdentifier); StringValue[] newStrings; if (oldStrings == null) { directory.setStringValueArray(tagIdentifier, newStrings); } else { directory.setStringValue(tagIdentifier, string);
/** Returns the specified tag's value as a long, if possible. */ public long getLong(int tagType) throws MetadataException { Long value = getLongObject(tagType); if (value != null) return value; Object o = getObject(tagType); if (o == null) throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first"); throw new MetadataException("Tag '" + tagType + "' cannot be converted to a long. It is of type '" + o.getClass() + "'."); }
/** Returns the specified tag's value as a boolean, if possible. */ public boolean getBoolean(int tagType) throws MetadataException { Boolean value = getBooleanObject(tagType); if (value != null) return value; Object o = getObject(tagType); if (o == null) throw new MetadataException("Tag '" + getTagName(tagType) + "' has not been set -- check using containsTag() first"); throw new MetadataException("Tag '" + tagType + "' cannot be converted to a boolean. It is of type '" + o.getClass() + "'."); }
String fileName = file.getName(); String urlName = StringUtil.urlEncode(filePath); ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); String make = exifIFD0Directory == null ? "" : exifIFD0Directory.getString(ExifIFD0Directory.TAG_MAKE); String model = exifIFD0Directory == null ? "" : exifIFD0Directory.getString(ExifIFD0Directory.TAG_MODEL); for (Directory directory : metadata.getDirectories()) { String directoryName = directory.getName(); for (Tag tag : directory.getTags()) { String tagName = tag.getTagName(); String description = tag.getDescription(); System.out.printf("%s|0x%s|%s|%s%n", directoryName, Integer.toHexString(tag.getTagType()), tagName, description); for (String error : directory.getErrors()) System.err.println("ERROR: " + error);
if (metadata.hasErrors()) { for (Directory directory : metadata.getDirectories()) { if (!directory.hasErrors()) continue; for (String error : directory.getErrors()) writer.format("[ERROR: %s] %s%s", directory.getName(), error, NEW_LINE); for (Directory directory : metadata.getDirectories()) { String directoryName = directory.getName(); for (Tag tag : directory.getTags()) { String tagName = tag.getTagName(); String description; try { description = tag.getDescription(); } catch (Exception ex) { description = "ERROR: " + ex.getMessage(); description = ""; if (directory instanceof FileSystemDirectory && tag.getTagType() == FileSystemDirectory.TAG_FILE_MODIFIED_DATE) description = "<omitted for regression testing as checkout dependent>"; writer.format("[%s - %s] %s = %s%s", directoryName, tag.getTagTypeHex(), tagName, description, NEW_LINE); if (directory.getTagCount() != 0) writer.write(NEW_LINE);
if (metadata.hasErrors()) { System.err.println(url); for (Directory directory : metadata.getDirectories()) { if (!directory.hasErrors()) continue; for (String error : directory.getErrors()) { System.err.printf("\t[%s] %s%n", directory.getName(), error); for (Directory directory : metadata.getDirectories()) { for (Tag tag : directory.getTags()) { String tagName = tag.getTagName(); String directoryName = directory.getName(); String description = tag.getDescription();
if (directory.containsTag(ExifSubIFDDirectory.TAG_EXPOSURE_TIME)) { Object exposure = directory.getObject(ExifSubIFDDirectory.TAG_EXPOSURE_TIME); if (exposure instanceof Rational) { metadata.set(Metadata.EXPOSURE_TIME, ((Rational) exposure).doubleValue()); } else { metadata.set(Metadata.EXPOSURE_TIME, directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_TIME)); if (directory.containsTag(ExifSubIFDDirectory.TAG_FLASH)) { String flash = directory.getDescription(ExifSubIFDDirectory.TAG_FLASH); if (flash != null) { if (flash.contains("Flash fired")) { if (directory.containsTag(ExifSubIFDDirectory.TAG_FNUMBER)) { Object fnumber = directory.getObject(ExifSubIFDDirectory.TAG_FNUMBER); if (fnumber instanceof Rational) { metadata.set(Metadata.F_NUMBER, ((Rational) fnumber).doubleValue()); } else { metadata.set(Metadata.F_NUMBER, directory.getString(ExifSubIFDDirectory.TAG_FNUMBER)); if (directory.containsTag(ExifSubIFDDirectory.TAG_FOCAL_LENGTH)) { Object length = directory.getObject(ExifSubIFDDirectory.TAG_FOCAL_LENGTH); if (length instanceof Rational) { metadata.set(Metadata.FOCAL_LENGTH, ((Rational) length).doubleValue()); } else { metadata.set(Metadata.FOCAL_LENGTH, directory.getString(ExifSubIFDDirectory.TAG_FOCAL_LENGTH)); if (directory.containsTag(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT)) { metadata.set(Metadata.ISO_SPEED_RATINGS, directory.getString(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT));
public void handle(Directory directory, Metadata metadata) throws MetadataException { if (directory.getTags() != null) { for (Tag tag : directory.getTags()) { metadata.set(tag.getTagName(), tag.getDescription()); } } } }
private void processDirectory(final MapWriter writer, final Directory directory, final Metadata metadata) { for (Tag tag : directory.getTags()) { try { final int tagType = tag.getTagType(); Object value; if (descriptive || isDescriptionTag(directory, tagType)) { value = directory.getDescription(tagType); if (directory instanceof PngDirectory) { if (((PngDirectory) directory).getPngChunkType().areMultipleAllowed()) { value = directory.getObject(tagType); if (directory instanceof ExifIFD0Directory && tagType == ExifIFD0Directory.TAG_DATETIME) { ExifSubIFDDirectory exifSubIFDDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); String subsecond = null; if (exifSubIFDDir != null) { subsecond = exifSubIFDDir.getString(ExifSubIFDDirectory.TAG_SUBSECOND_TIME); value = directory.getDate(tagType, subsecond, timeZone); } else if (directory instanceof ExifSubIFDDirectory) { if (tagType == ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL) { value = directory.getString(tagType, "US-ASCII"); } else if (isDateTag(directory, tagType)) { value = directory.getDate(tagType, timeZone); writeValue(writer, formatName(tag.getTagName()), value); } catch (Exception e) {
/** * A basic representation of the tag's type and value. EG: <code>[Exif IFD0] FNumber - f/2.8</code>. * * @return the tag's type and value */ @Override @NotNull public String toString() { String description = getDescription(); if (description == null) description = _directory.getString(getTagType()) + " (unable to formulate description)"; return "[" + _directory.getName() + "] " + getTagName() + " - " + description; } }
private void populateMetadata(Metadata metadata) { for (Iterator<?> di = metadata.getDirectories().iterator(); di.hasNext();) { Directory directory = (Directory) di.next(); Map<String, String> tags = new HashMap<String, String>(); put(directory.getName(), tags); for (Iterator<?> ti = directory.getTags().iterator(); ti.hasNext();) { Tag tag = (Tag) ti.next(); try { tags.put(tag.getTagName(), directory.getDescription(tag.getTagType())); } catch (Exception error) { errors.add(error); } } } }
@Override public void extractMetadata(byte[] bytes, Map<String, String> md) { BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(bytes)); try { Metadata metadata = ImageMetadataReader.readMetadata(bis, false); for (Directory directory : metadata.getDirectories()) { for (Tag tag : directory.getTags()) { String key = tag.getTagName(); if (md.containsKey(key)) { key = String.format("%s (%s)", tag.getTagName(), tag.getDirectoryName()); } String value = directory.getDescription(tag.getTagType()); if (StringUtils.isNotEmpty(value) && value.startsWith("[") && value.endsWith("bytes]")) { byte[] tagBytes = directory.getByteArray(tag.getTagType()); value = Base64.encodeBase64String(tagBytes); } md.put(key, value); } } } catch (Exception ex) { ex.printStackTrace(); } } }
@Override public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log) { super.onExtractionSuccess(file, metadata, relativePath, log); for (Directory directory : metadata.getDirectories()) { for (Tag tag : directory.getTags()) { // Only interested in unknown tags (those without names) if (tag.hasTagName()) continue; HashMap<Integer, Integer> occurrenceCountByTag = _occurrenceCountByTagByDirectory.get(directory.getName()); if (occurrenceCountByTag == null) { occurrenceCountByTag = new HashMap<Integer, Integer>(); _occurrenceCountByTagByDirectory.put(directory.getName(), occurrenceCountByTag); } Integer count = occurrenceCountByTag.get(tag.getTagType()); if (count == null) { count = 0; occurrenceCountByTag.put(tag.getTagType(), 0); } occurrenceCountByTag.put(tag.getTagType(), count + 1); } } }
Metadata metadata = new Metadata(); for (JpegSegmentType segmentType : exifReader.getSegmentTypes()) { exifReader.readJpegSegments(segmentData.getSegments(segmentType), metadata, segmentType); Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); if (directory != null) { for (Tag tag : directory.getTags()) { if (tag.getTagType() == ExifDirectoryBase.TAG_DATETIME) { dateTime = tag.getDescription();
try { // Extract metadata. Metadata metadata = ImageMetadataReader.readMetadata(new BufferedInputStream(new ByteArrayInputStream(imageData)), imageData.length); // Log each directory. for(Directory directory : metadata.getDirectories()) { Log.d("LOG", "Directory: " + directory.getName()); // Log all errors. for(String error : directory.getErrors()) { Log.d("LOG", "> error: " + error); } // Log all tags. for(Tag tag : directory.getTags()) { Log.d("LOG", "> tag: " + tag.getTagName() + " = " + tag.getDescription()); } } } catch(Exception e) { // TODO: handle exception }
public void handle(Directory directory, Metadata metadata) throws MetadataException { if (directory.containsTag(JpegCommentDirectory.TAG_COMMENT)) { metadata.add(TikaCoreProperties.COMMENTS, directory.getString(JpegCommentDirectory.TAG_COMMENT)); } } }
File pic = new File("C:/myPicture.jpg"); Metadata metadata = ImageMetadataReader.readMetadata(pic); for (Directory directory : metadata.getDirectories()) { for (Tag tag : directory.getTags()) { System.out.println(tag + " - " +tag.getTagType()); } }
this.relativePath = relativePath; ExifIFD0Directory ifd0Dir = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); ExifSubIFDDirectory subIfdDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class); ExifThumbnailDirectory thumbDir = metadata.getFirstDirectoryOfType(ExifThumbnailDirectory.class); if (ifd0Dir != null) { manufacturer = ifd0Dir.getDescription(ExifIFD0Directory.TAG_MAKE); for (Directory directory : metadata.getDirectories()) { if (directory.getClass().getName().contains("Makernote")) { makernote = directory.getName().replace("Makernote", "").trim(); break;