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);
}
Aggregations