use of org.apache.sanselan.formats.tiff.write.TiffOutputSet in project iNaturalistAndroid by inaturalist.
the class ImageUtils method getSanselanOutputSet.
private static TiffOutputSet getSanselanOutputSet(File jpegImageFile, int defaultByteOrder) throws IOException, ImageReadException, ImageWriteException {
TiffImageMetadata exif = null;
TiffOutputSet outputSet = null;
IImageMetadata metadata = Sanselan.getMetadata(jpegImageFile);
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
if (jpegMetadata != null) {
exif = jpegMetadata.getExif();
if (exif != null) {
outputSet = exif.getOutputSet();
}
}
// keep all other existing tags
if (outputSet == null)
outputSet = new TiffOutputSet(exif == null ? defaultByteOrder : exif.contents.header.byteOrder);
return outputSet;
}
use of org.apache.sanselan.formats.tiff.write.TiffOutputSet in project iNaturalistAndroid by inaturalist.
the class ImageUtils method getSanselanOutputSet.
private static TiffOutputSet getSanselanOutputSet(InputStream stream, int defaultByteOrder) throws IOException, ImageReadException, ImageWriteException {
TiffImageMetadata exif = null;
TiffOutputSet outputSet = null;
IImageMetadata metadata = Sanselan.getMetadata(stream, null);
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
if (jpegMetadata != null) {
exif = jpegMetadata.getExif();
if (exif != null) {
outputSet = exif.getOutputSet();
}
}
// keep all other existing tags
if (outputSet == null)
outputSet = new TiffOutputSet(exif == null ? defaultByteOrder : exif.contents.header.byteOrder);
return outputSet;
}
use of org.apache.sanselan.formats.tiff.write.TiffOutputSet 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