Search in sources :

Example 1 with TiffOutputSet

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;
}
Also used : TiffOutputSet(org.apache.sanselan.formats.tiff.write.TiffOutputSet) TiffImageMetadata(org.apache.sanselan.formats.tiff.TiffImageMetadata) JpegImageMetadata(org.apache.sanselan.formats.jpeg.JpegImageMetadata) IImageMetadata(org.apache.sanselan.common.IImageMetadata)

Example 2 with TiffOutputSet

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;
}
Also used : TiffOutputSet(org.apache.sanselan.formats.tiff.write.TiffOutputSet) TiffImageMetadata(org.apache.sanselan.formats.tiff.TiffImageMetadata) JpegImageMetadata(org.apache.sanselan.formats.jpeg.JpegImageMetadata) IImageMetadata(org.apache.sanselan.common.IImageMetadata)

Example 3 with TiffOutputSet

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

TiffOutputSet (org.apache.sanselan.formats.tiff.write.TiffOutputSet)3 IImageMetadata (org.apache.sanselan.common.IImageMetadata)2 JpegImageMetadata (org.apache.sanselan.formats.jpeg.JpegImageMetadata)2 TiffImageMetadata (org.apache.sanselan.formats.tiff.TiffImageMetadata)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 ImageWriteException (org.apache.sanselan.ImageWriteException)1 ExifRewriter (org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter)1 TiffOutputDirectory (org.apache.sanselan.formats.tiff.write.TiffOutputDirectory)1 TiffOutputField (org.apache.sanselan.formats.tiff.write.TiffOutputField)1