Search in sources :

Example 11 with ExifIFD0Directory

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;
}
Also used : GpsDirectory(com.drew.metadata.exif.GpsDirectory) ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) JSONObject(org.json.JSONObject) Metadata(com.drew.metadata.Metadata) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) BufferedImage(java.awt.image.BufferedImage) Image(org.structr.web.entity.Image) JSONException(org.json.JSONException) FrameworkException(org.structr.common.error.FrameworkException) ImageProcessingException(com.drew.imaging.ImageProcessingException) MetadataException(com.drew.metadata.MetadataException) IOException(java.io.IOException)

Example 12 with ExifIFD0Directory

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());
        }
    }
}
Also used : GpsDirectory(com.drew.metadata.exif.GpsDirectory) ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) TimeZone(java.util.TimeZone) PngDirectory(com.drew.metadata.png.PngDirectory) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) Tag(com.drew.metadata.Tag) XMPException(com.adobe.internal.xmp.XMPException)

Example 13 with ExifIFD0Directory

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);
}
Also used : WrongFileFormatException(com.odysseusinc.arachne.portal.exception.WrongFileFormatException) Metadata(com.drew.metadata.Metadata) BufferedImage(java.awt.image.BufferedImage) SolrServerException(org.apache.solr.client.solrj.SolrServerException) UserNotFoundException(com.odysseusinc.arachne.portal.exception.UserNotFoundException) MetadataException(com.drew.metadata.MetadataException) WrongFileFormatException(com.odysseusinc.arachne.portal.exception.WrongFileFormatException) IOException(java.io.IOException) PasswordValidationException(com.odysseusinc.arachne.portal.exception.PasswordValidationException) ImageProcessingException(com.drew.imaging.ImageProcessingException) NotUniqueException(com.odysseusinc.arachne.portal.exception.NotUniqueException) ValidationException(com.odysseusinc.arachne.portal.exception.ValidationException) ArachneSystemRuntimeException(com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException) PermissionDeniedException(com.odysseusinc.arachne.portal.exception.PermissionDeniedException) NotExistException(com.odysseusinc.arachne.portal.exception.NotExistException) NotEmptyException(com.odysseusinc.arachne.portal.exception.NotEmptyException) LinkedList(java.util.LinkedList) Date(java.util.Date) Scalr(org.imgscalr.Scalr) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Aggregations

ExifIFD0Directory (com.drew.metadata.exif.ExifIFD0Directory)13 MetadataException (com.drew.metadata.MetadataException)8 ImageProcessingException (com.drew.imaging.ImageProcessingException)5 Metadata (com.drew.metadata.Metadata)5 ExifSubIFDDirectory (com.drew.metadata.exif.ExifSubIFDDirectory)5 IOException (java.io.IOException)5 Directory (com.drew.metadata.Directory)3 GpsDirectory (com.drew.metadata.exif.GpsDirectory)3 BufferedImage (java.awt.image.BufferedImage)3 EpsDirectory (com.drew.metadata.eps.EpsDirectory)2 PngDirectory (com.drew.metadata.png.PngDirectory)2 Date (java.util.Date)2 Metadata (org.apache.tika.metadata.Metadata)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 FrameworkException (org.structr.common.error.FrameworkException)2 Image (org.structr.web.entity.Image)2 XMPException (com.adobe.internal.xmp.XMPException)1 Tag (com.drew.metadata.Tag)1