use of com.drew.metadata.exif.ExifIFD0Directory in project structr by structr.
the class ImageHelper method getExifData.
public static JSONObject getExifData(final File originalImage) {
final JSONObject exifDataJson = new JSONObject();
try {
final Metadata metadata = getMetadata(originalImage);
final ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
final ExifSubIFDDirectory exifSubIFDDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
final GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
if (exifIFD0Directory != null) {
final JSONObject exifIFD0DataJson = new JSONObject();
exifIFD0Directory.getTags().forEach((tag) -> {
exifIFD0DataJson.put(tag.getTagName(), exifIFD0Directory.getDescription(tag.getTagType()));
});
originalImage.setProperty(StructrApp.key(Image.class, "exifIFD0Data"), exifIFD0DataJson.toString());
exifDataJson.putOnce("exifIFD0Data", exifIFD0DataJson);
}
if (exifSubIFDDirectory != null) {
final JSONObject exifSubIFDDataJson = new JSONObject();
exifSubIFDDirectory.getTags().forEach((tag) -> {
exifSubIFDDataJson.put(tag.getTagName(), exifSubIFDDirectory.getDescription(tag.getTagType()));
});
originalImage.setProperty(StructrApp.key(Image.class, "exifSubIFDData"), exifSubIFDDataJson.toString());
exifDataJson.putOnce("exifSubIFDData", exifSubIFDDataJson);
}
if (gpsDirectory != null) {
final JSONObject exifGpsDataJson = new JSONObject();
gpsDirectory.getTags().forEach((tag) -> {
exifGpsDataJson.put(tag.getTagName(), gpsDirectory.getDescription(tag.getTagType()));
});
originalImage.setProperty(StructrApp.key(Image.class, "gpsData"), exifGpsDataJson.toString());
exifDataJson.putOnce("gpsData", exifGpsDataJson);
}
return exifDataJson;
} catch (Exception ex) {
logger.warn("Unable to extract EXIF metadata.", ex);
}
return null;
}
use of com.drew.metadata.exif.ExifIFD0Directory in project drill by apache.
the class ImageDirectoryProcessor method processDirectory.
protected static void processDirectory(final MapColumnDefn writer, final Directory directory, final Metadata metadata, final ImageFormatConfig config) {
TimeZone timeZone = (config.getTimeZone() != null) ? TimeZone.getTimeZone(config.getTimeZone()) : TimeZone.getDefault();
for (Tag tag : directory.getTags()) {
try {
final int tagType = tag.getTagType();
Object value;
if (config.isDescriptive() || ImageMetadataUtils.isDescriptionTag(directory, tagType)) {
value = directory.getDescription(tagType);
if (directory instanceof PngDirectory) {
if (((PngDirectory) directory).getPngChunkType().areMultipleAllowed()) {
value = new String[] { (String) value };
}
}
} else {
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 = ((ExifSubIFDDirectory) directory).getDateOriginal(timeZone);
} else if (tagType == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED) {
value = ((ExifSubIFDDirectory) directory).getDateDigitized(timeZone);
}
} else if (directory instanceof GpsDirectory) {
if (tagType == GpsDirectory.TAG_LATITUDE) {
value = ((GpsDirectory) directory).getGeoLocation().getLatitude();
} else if (tagType == GpsDirectory.TAG_LONGITUDE) {
value = ((GpsDirectory) directory).getGeoLocation().getLongitude();
}
}
if (ImageMetadataUtils.isVersionTag(directory, tagType)) {
value = directory.getString(tagType, "US-ASCII");
} else if (ImageMetadataUtils.isDateTag(directory, tagType)) {
value = directory.getDate(tagType, timeZone);
}
}
processValue(writer, ImageMetadataUtils.formatName(tag.getTagName()), value);
} catch (Exception skipped) {
logger.warn("Error in processing image directory : {}", skipped.getMessage());
}
}
}
use of com.drew.metadata.exif.ExifIFD0Directory in project ArachneCentralAPI by OHDSI.
the class BaseUserServiceImpl method saveAvatar.
@Override
public void saveAvatar(U user, MultipartFile file) throws IOException, WrongFileFormatException, ImageProcessingException, MetadataException, IllegalAccessException, SolrServerException, NoSuchFieldException {
String fileExt = FilenameUtils.getExtension(file.getOriginalFilename());
BufferedImage img = ImageIO.read(file.getInputStream());
if (img == null) {
throw new WrongFileFormatException("file", "File format is not supported");
}
final File avatar = getUserAvatarFile(user);
Metadata metadata = ImageMetadataReader.readMetadata(file.getInputStream());
ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
int orientation = 1;
try {
orientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
} catch (Exception ignore) {
LOGGER.debug(ignore.getMessage(), ignore);
}
List<Scalr.Rotation> rotations = new LinkedList<>();
switch(orientation) {
case 1:
break;
case // Flip X
2:
rotations.add(Scalr.Rotation.FLIP_HORZ);
break;
case // PI rotation
3:
rotations.add(Scalr.Rotation.CW_180);
break;
case // Flip Y
4:
rotations.add(Scalr.Rotation.FLIP_VERT);
break;
case // - PI/2 and Flip X
5:
rotations.add(Scalr.Rotation.CW_90);
rotations.add(Scalr.Rotation.FLIP_HORZ);
break;
case // -PI/2 and -width
6:
rotations.add(Scalr.Rotation.CW_90);
break;
case // PI/2 and Flip
7:
rotations.add(Scalr.Rotation.CW_90);
rotations.add(Scalr.Rotation.FLIP_VERT);
break;
case // PI / 2
8:
rotations.add(Scalr.Rotation.CW_270);
break;
default:
break;
}
for (Scalr.Rotation rotation : rotations) {
img = Scalr.rotate(img, rotation);
}
BufferedImage thumbnail = Scalr.resize(img, Math.min(Math.max(img.getHeight(), img.getWidth()), 640), Scalr.OP_ANTIALIAS);
ImageIO.write(thumbnail, fileExt, avatar);
user.setUpdated(new Date());
U savedUser = rawUserRepository.save(user);
indexBySolr(savedUser);
}
Aggregations