Search in sources :

Example 1 with ImageWriteException

use of org.apache.sanselan.ImageWriteException 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;
}
Also used : TiffOutputDirectory(org.apache.sanselan.formats.tiff.write.TiffOutputDirectory) ImageWriteException(org.apache.sanselan.ImageWriteException)

Example 2 with ImageWriteException

use of org.apache.sanselan.ImageWriteException 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;
}
Also used : ExifRewriter(org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter) TiffOutputDirectory(org.apache.sanselan.formats.tiff.write.TiffOutputDirectory) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ImageWriteException(org.apache.sanselan.ImageWriteException) IOException(java.io.IOException) Paint(android.graphics.Paint) TiffOutputSet(org.apache.sanselan.formats.tiff.write.TiffOutputSet) TiffOutputField(org.apache.sanselan.formats.tiff.write.TiffOutputField) FileOutputStream(java.io.FileOutputStream) ImageReadException(org.apache.sanselan.ImageReadException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

ImageWriteException (org.apache.sanselan.ImageWriteException)2 TiffOutputDirectory (org.apache.sanselan.formats.tiff.write.TiffOutputDirectory)2 Paint (android.graphics.Paint)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ImageReadException (org.apache.sanselan.ImageReadException)1 ExifRewriter (org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter)1 TiffOutputField (org.apache.sanselan.formats.tiff.write.TiffOutputField)1 TiffOutputSet (org.apache.sanselan.formats.tiff.write.TiffOutputSet)1