use of org.apache.sanselan.formats.tiff.write.TiffOutputDirectory in project iNaturalistAndroid by inaturalist.
the class ImageUtils method getOrCreateExifDirectory.
private static TiffOutputDirectory getOrCreateExifDirectory(TiffOutputSet outputSet, TiffOutputDirectory outputDirectory) {
TiffOutputDirectory result = outputSet.findDirectory(outputDirectory.type);
if (result != null)
return result;
result = new TiffOutputDirectory(outputDirectory.type);
try {
outputSet.addDirectory(result);
} catch (ImageWriteException e) {
return null;
}
return result;
}
use of org.apache.sanselan.formats.tiff.write.TiffOutputDirectory in project iNaturalistAndroid by inaturalist.
the class ImageUtils method copyExifData.
// EXIF-copying code taken from: https://bricolsoftconsulting.com/copying-exif-metadata-using-sanselan/
private static boolean copyExifData(InputStream sourceFileStream, File destFile, List<TagInfo> excludedFields) {
String tempFileName = destFile.getAbsolutePath() + ".tmp";
File tempFile = null;
OutputStream tempStream = null;
try {
tempFile = new File(tempFileName);
TiffOutputSet sourceSet = getSanselanOutputSet(sourceFileStream, TiffConstants.DEFAULT_TIFF_BYTE_ORDER);
TiffOutputSet destSet = getSanselanOutputSet(destFile, sourceSet.byteOrder);
// of this implementation
if (sourceSet.byteOrder != destSet.byteOrder)
return false;
destSet.getOrCreateExifDirectory();
// Go through the source directories
List<?> sourceDirectories = sourceSet.getDirectories();
for (int i = 0; i < sourceDirectories.size(); i++) {
TiffOutputDirectory sourceDirectory = (TiffOutputDirectory) sourceDirectories.get(i);
TiffOutputDirectory destinationDirectory = getOrCreateExifDirectory(destSet, sourceDirectory);
// failed to create
if (destinationDirectory == null)
continue;
// Loop the fields
List<?> sourceFields = sourceDirectory.getFields();
for (int j = 0; j < sourceFields.size(); j++) {
// Get the source field
TiffOutputField sourceField = (TiffOutputField) sourceFields.get(j);
// Check exclusion list
if (excludedFields != null && excludedFields.contains(sourceField.tagInfo)) {
destinationDirectory.removeField(sourceField.tagInfo);
continue;
}
// Remove any existing field
destinationDirectory.removeField(sourceField.tagInfo);
// Add field
destinationDirectory.add(sourceField);
}
}
// Save data to destination
tempStream = new BufferedOutputStream(new FileOutputStream(tempFile));
new ExifRewriter().updateExifMetadataLossless(destFile, tempStream, destSet);
tempStream.close();
// Replace file
if (destFile.delete()) {
tempFile.renameTo(destFile);
}
return true;
} catch (ImageReadException exception) {
exception.printStackTrace();
} catch (ImageWriteException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
} finally {
if (tempStream != null) {
try {
tempStream.close();
} catch (IOException e) {
}
}
if (tempFile != null) {
if (tempFile.exists())
tempFile.delete();
}
}
return false;
}
Aggregations