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