Search in sources :

Example 11 with MetadataException

use of com.drew.metadata.MetadataException 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

MetadataException (com.drew.metadata.MetadataException)11 ExifIFD0Directory (com.drew.metadata.exif.ExifIFD0Directory)7 IOException (java.io.IOException)5 ImageProcessingException (com.drew.imaging.ImageProcessingException)4 Metadata (com.drew.metadata.Metadata)4 TikaException (org.apache.tika.exception.TikaException)4 ExifSubIFDDirectory (com.drew.metadata.exif.ExifSubIFDDirectory)2 BufferedImage (java.awt.image.BufferedImage)2 Date (java.util.Date)2 XMPMetadata (org.apache.jempbox.xmp.XMPMetadata)2 Metadata (org.apache.tika.metadata.Metadata)2 JpegProcessingException (com.drew.imaging.jpeg.JpegProcessingException)1 RiffProcessingException (com.drew.imaging.riff.RiffProcessingException)1 TiffProcessingException (com.drew.imaging.tiff.TiffProcessingException)1 ByteArrayReader (com.drew.lang.ByteArrayReader)1 Directory (com.drew.metadata.Directory)1 AviDirectory (com.drew.metadata.avi.AviDirectory)1 BmpHeaderDirectory (com.drew.metadata.bmp.BmpHeaderDirectory)1 EpsDirectory (com.drew.metadata.eps.EpsDirectory)1 ExifReader (com.drew.metadata.exif.ExifReader)1