Search in sources :

Example 11 with ImageProcessingException

use of com.drew.imaging.ImageProcessingException 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)

Example 12 with ImageProcessingException

use of com.drew.imaging.ImageProcessingException in project react-native-camera by react-native-community.

the class MutableImage method fixOrientation.

public void fixOrientation() throws ImageMutationFailedException {
    try {
        Metadata metadata = originalImageMetaData();
        ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
        if (exifIFD0Directory == null) {
            return;
        } else if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
            int exifOrientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
            if (exifOrientation != 1) {
                rotate(exifOrientation);
                exifIFD0Directory.setInt(ExifIFD0Directory.TAG_ORIENTATION, 1);
            }
        }
    } catch (ImageProcessingException | IOException | MetadataException e) {
        throw new ImageMutationFailedException("failed to fix orientation", e);
    }
}
Also used : ImageProcessingException(com.drew.imaging.ImageProcessingException) Metadata(com.drew.metadata.Metadata) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) IOException(java.io.IOException) MetadataException(com.drew.metadata.MetadataException)

Example 13 with ImageProcessingException

use of com.drew.imaging.ImageProcessingException in project react-native-camera by react-native-community.

the class MutableImage method writeDataToFile.

public void writeDataToFile(File file, ReadableMap options, int jpegQualityPercent) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(toJpeg(currentRepresentation, jpegQualityPercent));
    fos.close();
    try {
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        // copy original exif data to the output exif...
        for (Directory directory : originalImageMetaData().getDirectories()) {
            for (Tag tag : directory.getTags()) {
                int tagType = tag.getTagType();
                Object object = directory.getObject(tagType);
                exif.setAttribute(tag.getTagName(), object.toString());
            }
        }
        // Add missing exif data from a sub directory
        ExifSubIFDDirectory directory = originalImageMetaData().getFirstDirectoryOfType(ExifSubIFDDirectory.class);
        for (Tag tag : directory.getTags()) {
            int tagType = tag.getTagType();
            // As some of exif data does not follow naming of the ExifInterface the names need
            // to be transformed into Upper camel case format.
            String tagName = tag.getTagName().replaceAll(" ", "");
            Object object = directory.getObject(tagType);
            if (tagName.equals(ExifInterface.TAG_EXPOSURE_TIME)) {
                exif.setAttribute(tagName, convertExposureTimeToDoubleFormat(object.toString()));
            } else {
                exif.setAttribute(tagName, object.toString());
            }
        }
        writeLocationExifData(options, exif);
        if (hasBeenReoriented)
            rewriteOrientation(exif);
        exif.saveAttributes();
    } catch (ImageProcessingException | IOException e) {
        Log.e(TAG, "failed to save exif data", e);
    }
}
Also used : ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) ImageProcessingException(com.drew.imaging.ImageProcessingException) FileOutputStream(java.io.FileOutputStream) ExifInterface(androidx.exifinterface.media.ExifInterface) Tag(com.drew.metadata.Tag) IOException(java.io.IOException) ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) Directory(com.drew.metadata.Directory) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory)

Aggregations

ImageProcessingException (com.drew.imaging.ImageProcessingException)13 IOException (java.io.IOException)10 Metadata (com.drew.metadata.Metadata)8 ExifIFD0Directory (com.drew.metadata.exif.ExifIFD0Directory)6 MetadataException (com.drew.metadata.MetadataException)5 File (java.io.File)3 Date (java.util.Date)3 ExifInterface (androidx.exifinterface.media.ExifInterface)2 FileType (com.drew.imaging.FileType)2 Directory (com.drew.metadata.Directory)2 Tag (com.drew.metadata.Tag)2 ExifSubIFDDirectory (com.drew.metadata.exif.ExifSubIFDDirectory)2 LineString (de.micromata.opengis.kml.v_2_2_0.LineString)2 BufferedImage (java.awt.image.BufferedImage)2 FileOutputStream (java.io.FileOutputStream)2 IIOException (javax.imageio.IIOException)2 ParseException (net.pms.util.ParseException)2 UnknownFormatException (net.pms.util.UnknownFormatException)2 GeoLocation (com.drew.lang.GeoLocation)1 ArachneSystemRuntimeException (com.odysseusinc.arachne.portal.exception.ArachneSystemRuntimeException)1