Class ExifInterface

  • All Implemented Interfaces:

    
    public class ExifInterface
    
                        

    This class provides methods and constants for reading and writing jpeg file metadata. It contains a collection of ExifTags, and a collection of definitions for creating valid ExifTags. The collection of ExifTags can be updated by: reading new ones from a file, deleting or adding existing ones, or building new ExifTags from a tag definition. These ExifTags can be written to a valid jpeg image as exif metadata.

    Each ExifTag has a tag ID (TID) and is stored in a specific image file directory (IFD) as specified by the exif standard. A tag definition can be looked up with a constant that is a combination of TID and IFD. This definition has information about the type, number of components, and valid IFDs for a tag.

    • Constructor Detail

      • ExifInterface

        ExifInterface()
    • Method Detail

      • defineTag

         static int defineTag(int ifdId, short tagId)

        Returns the constant representing a tag with a given TID and default IFD.

      • getTrueTagKey

         static short getTrueTagKey(int tag)

        Returns the TID for a tag constant.

      • getTrueIfd

         static int getTrueIfd(int tag)

        Returns the default IFD for a tag constant.

      • readExif

         void readExif(Array<byte> jpeg)

        Reads the exif tags from a byte array, clearing this ExifInterface object's existing exif tags.

        Parameters:
        jpeg - a byte array containing a jpeg compressed image.
      • readExif

         void readExif(InputStream inStream)

        Reads the exif tags from an InputStream, clearing this ExifInterface object's existing exif tags.

        Parameters:
        inStream - an InputStream containing a jpeg compressed image.
      • readExif

         void readExif(String inFileName)

        Reads the exif tags from a file, clearing this ExifInterface object's existing exif tags.

        Parameters:
        inFileName - a string representing the filepath to jpeg file.
      • setExif

         void setExif(Collection<ExifTag> tags)

        Sets the exif tags, clearing this ExifInterface object's existing exif tags.

        Parameters:
        tags - a collection of exif tags to set.
      • clearExif

         void clearExif()

        Clears this ExifInterface object's existing exif tags.

      • writeExif

         void writeExif(Array<byte> jpeg, OutputStream exifOutStream)

        Writes the tags from this ExifInterface object into a jpeg image, removing prior exif tags.

        Parameters:
        jpeg - a byte array containing a jpeg compressed image.
        exifOutStream - an OutputStream to which the jpeg image with added exif tags will be written.
      • writeExif

         void writeExif(Bitmap bmap, OutputStream exifOutStream)

        Writes the tags from this ExifInterface object into a jpeg compressed bitmap, removing prior exif tags.

        Parameters:
        bmap - a bitmap to compress and write exif into.
        exifOutStream - the OutputStream to which the jpeg image with added exif tags will be written.
      • writeExif

         void writeExif(InputStream jpegStream, OutputStream exifOutStream)

        Writes the tags from this ExifInterface object into a jpeg stream, removing prior exif tags.

        Parameters:
        jpegStream - an InputStream containing a jpeg compressed image.
        exifOutStream - an OutputStream to which the jpeg image with added exif tags will be written.
      • writeExif

         void writeExif(Array<byte> jpeg, String exifOutFileName)

        Writes the tags from this ExifInterface object into a jpeg image, removing prior exif tags.

        Parameters:
        jpeg - a byte array containing a jpeg compressed image.
        exifOutFileName - a String containing the filepath to which the jpeg image with added exif tags will be written.
      • writeExif

         void writeExif(Bitmap bmap, String exifOutFileName)

        Writes the tags from this ExifInterface object into a jpeg compressed bitmap, removing prior exif tags.

        Parameters:
        bmap - a bitmap to compress and write exif into.
        exifOutFileName - a String containing the filepath to which the jpeg image with added exif tags will be written.
      • writeExif

         void writeExif(InputStream jpegStream, String exifOutFileName)

        Writes the tags from this ExifInterface object into a jpeg stream, removing prior exif tags.

        Parameters:
        jpegStream - an InputStream containing a jpeg compressed image.
        exifOutFileName - a String containing the filepath to which the jpeg image with added exif tags will be written.
      • writeExif

         void writeExif(String jpegFileName, String exifOutFileName)

        Writes the tags from this ExifInterface object into a jpeg file, removing prior exif tags.

        Parameters:
        jpegFileName - a String containing the filepath for a jpeg file.
        exifOutFileName - a String containing the filepath to which the jpeg image with added exif tags will be written.
      • getExifWriterStream

         OutputStream getExifWriterStream(OutputStream outStream)

        Wraps an OutputStream object with an ExifOutputStream. Exif tags in this ExifInterface object will be added to a jpeg image written to this stream, removing prior exif tags. Other methods of this ExifInterface object should not be called until the returned OutputStream has been closed.

        Parameters:
        outStream - an OutputStream to wrap.
      • getExifWriterStream

         OutputStream getExifWriterStream(String exifOutFileName)

        Returns an OutputStream object that writes to a file. Exif tags in this ExifInterface object will be added to a jpeg image written to this stream, removing prior exif tags. Other methods of this ExifInterface object should not be called until the returned OutputStream has been closed.

        Parameters:
        exifOutFileName - an String containing a filepath for a jpeg file.
      • rewriteExif

         boolean rewriteExif(String filename, Collection<ExifTag> tags)

        Attempts to do an in-place rewrite the exif metadata in a file for the given tags. If tags do not exist or do not have the same size as the existing exif tags, this method will fail.

        Parameters:
        filename - a String containing a filepath for a jpeg file with exif tags to rewrite.
        tags - tags that will be written into the jpeg file over existing tags if possible.
      • rewriteExif

         boolean rewriteExif(ByteBuffer buf, Collection<ExifTag> tags)

        Attempts to do an in-place rewrite the exif metadata in a ByteBuffer for the given tags. If tags do not exist or do not have the same size as the existing exif tags, this method will fail.

        Parameters:
        buf - a ByteBuffer containing a jpeg file with existing exif tags to rewrite.
        tags - tags that will be written into the jpeg ByteBuffer over existing tags if possible.
      • forceRewriteExif

         void forceRewriteExif(String filename, Collection<ExifTag> tags)

        Attempts to do an in-place rewrite of the exif metadata. If this fails, fall back to overwriting file. This preserves tags that are not being rewritten.

        Parameters:
        filename - a String containing a filepath for a jpeg file.
        tags - tags that will be written into the jpeg file over existing tags if possible.
      • forceRewriteExif

         void forceRewriteExif(String filename)

        Attempts to do an in-place rewrite of the exif metadata using the tags in this ExifInterface object. If this fails, fall back to overwriting file. This preserves tags that are not being rewritten.

        Parameters:
        filename - a String containing a filepath for a jpeg file.
      • getAllTags

         List<ExifTag> getAllTags()

        Get the exif tags in this ExifInterface object or null if none exist.

      • getTagsForTagId

         List<ExifTag> getTagsForTagId(short tagId)

        Returns a list of ExifTags that share a TID (which can be obtained by calling getTrueTagKey on a defined tag constant) or null if none exist.

        Parameters:
        tagId - a TID as defined in the exif standard (or with defineTag).
      • getTagsForIfdId

         List<ExifTag> getTagsForIfdId(int ifdId)

        Returns a list of ExifTags that share an IFD (which can be obtained by calling getTrueIFD on a defined tag constant) or null if none exist.

        Parameters:
        ifdId - an IFD as defined in the exif standard (or with defineTag).
      • getTag

         ExifTag getTag(int tagId, int ifdId)

        Gets an ExifTag for an IFD other than the tag's default.

      • getTag

         ExifTag getTag(int tagId)

        Returns the ExifTag in that tag's default IFD for a defined tag constant or null if none exists.

        Parameters:
        tagId - a defined tag constant, e.g.
      • getTagValue

         Object getTagValue(int tagId, int ifdId)

        Gets a tag value for an IFD other than the tag's default.

      • getTagValue

         Object getTagValue(int tagId)

        Returns the value of the ExifTag in that tag's default IFD for a defined tag constant or null if none exists or the value could not be cast into the return type.

        Parameters:
        tagId - a defined tag constant, e.g.
      • isTagCountDefined

         boolean isTagCountDefined(int tagId)

        Checks whether a tag has a defined number of elements.

        Parameters:
        tagId - a defined tag constant, e.g.
      • getDefinedTagCount

         int getDefinedTagCount(int tagId)

        Gets the defined number of elements for a tag.

        Parameters:
        tagId - a defined tag constant, e.g.
      • getActualTagCount

         int getActualTagCount(int tagId, int ifdId)

        Gets the number of elements for an ExifTag in a given IFD.

        Parameters:
        tagId - a defined tag constant, e.g.
        ifdId - the IFD containing the ExifTag to check.
      • getDefinedTagDefaultIfd

         int getDefinedTagDefaultIfd(int tagId)

        Gets the default IFD for a tag.

        Parameters:
        tagId - a defined tag constant, e.g.
      • getDefinedTagType

         short getDefinedTagType(int tagId)

        Gets the defined type for a tag.

        Parameters:
        tagId - a defined tag constant, e.g.
      • buildTag

         ExifTag buildTag(int tagId, int ifdId, Object val)

        Creates a tag for a defined tag constant in a given IFD if that IFD is allowed for the tag. This method will fail anytime the appropriate setValue for this tag's datatype would fail.

        Parameters:
        tagId - a tag constant, e.g.
        ifdId - the IFD that the tag should be in.
        val - the value of the tag to set.
      • buildTag

         ExifTag buildTag(int tagId, Object val)

        Creates a tag for a defined tag constant in the tag's default IFD.

        Parameters:
        tagId - a tag constant, e.g.
        val - the tag's value.
      • setTagValue

         boolean setTagValue(int tagId, int ifdId, Object val)

        Sets the value of an ExifTag if it exists in the given IFD. The value must be the correct type and length for that ExifTag.

        Parameters:
        tagId - a tag constant, e.g.
        ifdId - the IFD that the ExifTag is in.
        val - the value to set.
      • setTagValue

         boolean setTagValue(int tagId, Object val)

        Sets the value of an ExifTag if it exists it's default IFD. The value must be the correct type and length for that ExifTag.

        Parameters:
        tagId - a tag constant, e.g.
        val - the value to set.
      • setTag

         ExifTag setTag(ExifTag tag)

        Puts an ExifTag into this ExifInterface object's tags, removing a previous ExifTag with the same TID and IFD. The IFD it is put into will be the one the tag was created with in buildTag.

        Parameters:
        tag - an ExifTag to put into this ExifInterface's tags.
      • setTags

         void setTags(Collection<ExifTag> tags)

        Puts a collection of ExifTags into this ExifInterface objects's tags. Any previous ExifTags with the same TID and IFDs will be removed.

        Parameters:
        tags - a Collection of ExifTags.
      • deleteTag

         void deleteTag(int tagId, int ifdId)

        Removes the ExifTag for a tag constant from the given IFD.

        Parameters:
        tagId - a tag constant, e.g.
        ifdId - the IFD of the ExifTag to remove.
      • deleteTag

         void deleteTag(int tagId)

        Removes the ExifTag for a tag constant from that tag's default IFD.

        Parameters:
        tagId - a tag constant, e.g.
      • setTagDefinition

         int setTagDefinition(short tagId, int defaultIfd, short tagType, short defaultComponentCount, Array<int> allowedIfds)

        Creates a new tag definition in this ExifInterface object for a given TID and default IFD. Creating a definition with the same TID and default IFD as a previous definition will override it.

        Parameters:
        tagId - the TID for the tag.
        defaultIfd - the default IFD for the tag.
        tagType - the type of the tag (see getDataType).
        defaultComponentCount - the number of elements of this tag's type in the tags value.
        allowedIfds - the IFD's this tag is allowed to be put in.
      • removeTagDefinition

         void removeTagDefinition(int tagId)

        Removes a tag definition for given defined tag constant.

        Parameters:
        tagId - a defined tag constant, e.g.
      • getThumbnailBytes

         Array<byte> getThumbnailBytes()

        Returns the thumbnail from IFD1 as a byte array, or null if none exists. The bytes may either be an uncompressed strip as specified in the exif standard or a jpeg compressed image.

      • getThumbnail

         Array<byte> getThumbnail()

        Returns the thumbnail if it is jpeg compressed, or null if none exists.

      • hasThumbnail

         boolean hasThumbnail()

        Check if thumbnail exists.

      • setCompressedThumbnail

         boolean setCompressedThumbnail(Array<byte> thumb)

        Sets the thumbnail to be a jpeg compressed image. Clears any prior thumbnail.

        Parameters:
        thumb - a byte array containing a jpeg compressed image.
      • setCompressedThumbnail

         boolean setCompressedThumbnail(Bitmap thumb)

        Sets the thumbnail to be a jpeg compressed bitmap. Clears any prior thumbnail.

        Parameters:
        thumb - a bitmap to compress to a jpeg thumbnail.
      • getUserComment

         String getUserComment()

        Decodes the user comment tag into string as specified in the EXIF standard. Returns null if decoding failed.

      • getOrientationValueForRotation

         static short getOrientationValueForRotation(int degrees)

        Returns the Orientation ExifTag value for a given number of degrees.

        Parameters:
        degrees - the amount an image is rotated in degrees.
      • getRotationForOrientationValue

         static int getRotationForOrientationValue(short orientation)

        Returns the rotation degrees corresponding to an ExifTag Orientation value.

        Parameters:
        orientation - the ExifTag Orientation value.
      • convertLatOrLongToDouble

         static double convertLatOrLongToDouble(Array<Rational> coordinate, String reference)

        Gets the double representation of the GPS latitude or longitude coordinate.

        Parameters:
        coordinate - an array of 3 Rationals representing the degrees, minutes, and seconds of the GPS location as defined in the exif specification.
        reference - a GPS reference reperesented by a String containing "N", "S", "E", or "W".
      • getLatLongAsDoubles

         Array<double> getLatLongAsDoubles()

        Gets the GPS latitude and longitude as a pair of doubles from this ExifInterface object's tags, or null if the necessary tags do not exist.

      • addGpsTags

         boolean addGpsTags(double latitude, double longitude)

        Creates and sets all to the GPS tags for a give latitude and longitude.

        Parameters:
        latitude - a GPS latitude coordinate.
        longitude - a GPS longitude coordinate.
      • addGpsDateTimeStampTag

         boolean addGpsDateTimeStampTag(long timestamp)

        Creates and sets the GPS timestamp tag.

        Parameters:
        timestamp - a GPS timestamp.