com.drew.metadata.exif
Class ExifReader

java.lang.Object
  extended by com.drew.metadata.exif.ExifReader
All Implemented Interfaces:
MetadataReader

public class ExifReader
extends Object
implements MetadataReader

Decodes Exif binary data, populating a Metadata object with tag values in ExifDirectory, GpsDirectory and one of the many camera makernote directories.

Author:
Drew Noakes http://drewnoakes.com

Field Summary
private  byte[] _data
          The Exif segment as an array of bytes.
private  ExifDirectory _exifDirectory
          The Exif directory used (loaded lazily)
private  boolean _isMotorollaByteOrder
          Represents the native byte ordering used in the JPEG segment.
private  Metadata _metadata
          Bean instance to store information about the image and camera/scanner/capture device.
private static int[] BYTES_PER_FORMAT
          The number of bytes used per format descriptor.
private static int FMT_BYTE
           
private static int FMT_DOUBLE
           
private static int FMT_SBYTE
           
private static int FMT_SINGLE
           
private static int FMT_SLONG
           
private static int FMT_SRATIONAL
           
private static int FMT_SSHORT
           
private static int FMT_STRING
           
private static int FMT_ULONG
           
private static int FMT_UNDEFINED
           
private static int FMT_URATIONAL
           
private static int FMT_USHORT
           
private static int MAX_FORMAT_CODE
          The number of formats known.
static int TAG_EXIF_OFFSET
           
static int TAG_GPS_INFO_OFFSET
           
static int TAG_INTEROP_OFFSET
           
static int TAG_MAKER_NOTE
           
static int TIFF_HEADER_START_OFFSET
           
 
Constructor Summary
ExifReader(byte[] data)
          Creates an ExifReader for the given Exif data segment.
ExifReader(File jpegFile)
          Deprecated. Not all files will be Jpegs! Use a constructor that provides the exif segment in isolation.
ExifReader(InputStream jpegInputStream)
          Deprecated. Not all files will be Jpegs! Use a constructor that provides the exif segment in isolation.
ExifReader(JpegSegmentData segmentData)
          Deprecated. Not all files will be Jpegs! This overload doesn't offer much convenience to the caller.
 
Method Summary
private  int calculateTagOffset(int dirStartOffset, int entryNumber)
          Determine the offset at which a given InteropArray entry begins within the specified IFD.
private  int calculateTagValueOffset(int byteCount, int dirEntryOffset, int tiffHeaderOffset)
           
 Metadata extract()
          Performs the Exif data extraction, returning a new instance of Metadata.
 Metadata extract(Metadata metadata)
          Performs the Exif data extraction, adding found values to the specified instance of Metadata.
private  Metadata extractIFD(Metadata metadata, int tiffHeaderOffset)
           
 Metadata extractTiff(Metadata metadata)
          Performs the Exif data extraction on a Tiff/Raw, adding found values to the specified instance of Metadata.
private  int get16Bits(int offset)
          Get a 16 bit value from file's native byte order.
private  int get32Bits(int offset)
          Get a 32 bit value from file's native byte order.
private  ExifDirectory getExifDirectory()
           
private  boolean isDirectoryLengthValid(int dirStartOffset, int tiffHeaderOffset)
           
private  void processDirectory(Directory directory, HashMap processedDirectoryOffsets, int dirStartOffset, int tiffHeaderOffset)
          Process one of the nested Tiff IFD directories.
private  void processMakerNote(int subdirOffset, HashMap processedDirectoryOffsets, int tiffHeaderOffset)
           
private  void processTag(Directory directory, int tagType, int tagValueOffset, int componentCount, int formatCode)
           
private  String readString(int offset, int maxLength)
          Creates a String from the _data buffer starting at the specified offset, and ending where byte=='\0' or where length==maxLength.
private  boolean setByteOrder(String byteOrderIdentifier)
          Sets the _isMotorollaByteOrder flag to true or false, depending upon the file's byte order string.
private  void storeThumbnailBytes(ExifDirectory exifDirectory, int tiffHeaderOffset)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BYTES_PER_FORMAT

private static final int[] BYTES_PER_FORMAT
The number of bytes used per format descriptor.


MAX_FORMAT_CODE

private static final int MAX_FORMAT_CODE
The number of formats known.

See Also:
Constant Field Values

FMT_BYTE

private static final int FMT_BYTE
See Also:
Constant Field Values

FMT_STRING

private static final int FMT_STRING
See Also:
Constant Field Values

FMT_USHORT

private static final int FMT_USHORT
See Also:
Constant Field Values

FMT_ULONG

private static final int FMT_ULONG
See Also:
Constant Field Values

FMT_URATIONAL

private static final int FMT_URATIONAL
See Also:
Constant Field Values

FMT_SBYTE

private static final int FMT_SBYTE
See Also:
Constant Field Values

FMT_UNDEFINED

private static final int FMT_UNDEFINED
See Also:
Constant Field Values

FMT_SSHORT

private static final int FMT_SSHORT
See Also:
Constant Field Values

FMT_SLONG

private static final int FMT_SLONG
See Also:
Constant Field Values

FMT_SRATIONAL

private static final int FMT_SRATIONAL
See Also:
Constant Field Values

FMT_SINGLE

private static final int FMT_SINGLE
See Also:
Constant Field Values

FMT_DOUBLE

private static final int FMT_DOUBLE
See Also:
Constant Field Values

TAG_EXIF_OFFSET

public static final int TAG_EXIF_OFFSET
See Also:
Constant Field Values

TAG_INTEROP_OFFSET

public static final int TAG_INTEROP_OFFSET
See Also:
Constant Field Values

TAG_GPS_INFO_OFFSET

public static final int TAG_GPS_INFO_OFFSET
See Also:
Constant Field Values

TAG_MAKER_NOTE

public static final int TAG_MAKER_NOTE
See Also:
Constant Field Values

TIFF_HEADER_START_OFFSET

public static final int TIFF_HEADER_START_OFFSET
See Also:
Constant Field Values

_data

private final byte[] _data
The Exif segment as an array of bytes.


_isMotorollaByteOrder

private boolean _isMotorollaByteOrder
Represents the native byte ordering used in the JPEG segment. If true, then we're using Motorolla ordering (Big endian), else we're using Intel ordering (Little endian).


_metadata

private Metadata _metadata
Bean instance to store information about the image and camera/scanner/capture device.


_exifDirectory

private ExifDirectory _exifDirectory
The Exif directory used (loaded lazily)

Constructor Detail

ExifReader

public ExifReader(JpegSegmentData segmentData)
Deprecated. Not all files will be Jpegs! This overload doesn't offer much convenience to the caller.

Creates an ExifReader for a JpegSegmentData object.

Parameters:
segmentData -

ExifReader

public ExifReader(File jpegFile)
           throws JpegProcessingException
Deprecated. Not all files will be Jpegs! Use a constructor that provides the exif segment in isolation.

Creates an ExifReader for a Jpeg jpegFile.

Parameters:
jpegFile -
Throws:
JpegProcessingException

ExifReader

public ExifReader(InputStream jpegInputStream)
           throws JpegProcessingException
Deprecated. Not all files will be Jpegs! Use a constructor that provides the exif segment in isolation.

Creates an ExifReader for a Jpeg stream.

Parameters:
jpegInputStream - JPEG stream. Stream will be closed.
Throws:
JpegProcessingException

ExifReader

public ExifReader(byte[] data)
Creates an ExifReader for the given Exif data segment.

Method Detail

extract

public Metadata extract()
Performs the Exif data extraction, returning a new instance of Metadata.

Specified by:
extract in interface MetadataReader
Returns:
The populated Metadata object.

extract

public Metadata extract(Metadata metadata)
Performs the Exif data extraction, adding found values to the specified instance of Metadata.

Specified by:
extract in interface MetadataReader
Returns:
The updated Metadata object.

getExifDirectory

private ExifDirectory getExifDirectory()

extractTiff

public Metadata extractTiff(Metadata metadata)
Performs the Exif data extraction on a Tiff/Raw, adding found values to the specified instance of Metadata.


extractIFD

private Metadata extractIFD(Metadata metadata,
                            int tiffHeaderOffset)

storeThumbnailBytes

private void storeThumbnailBytes(ExifDirectory exifDirectory,
                                 int tiffHeaderOffset)

setByteOrder

private boolean setByteOrder(String byteOrderIdentifier)
Sets the _isMotorollaByteOrder flag to true or false, depending upon the file's byte order string. If the string cannot be interpreted, false is returned.

Parameters:
byteOrderIdentifier - a two-character string; either "MM" for Motorolla or "II" for Intel.
Returns:
true if successful, otherwise false.

processDirectory

private void processDirectory(Directory directory,
                              HashMap processedDirectoryOffsets,
                              int dirStartOffset,
                              int tiffHeaderOffset)
Process one of the nested Tiff IFD directories. 2 bytes: number of tags for each tag 2 bytes: tag type 2 bytes: format code 4 bytes: component count


processMakerNote

private void processMakerNote(int subdirOffset,
                              HashMap processedDirectoryOffsets,
                              int tiffHeaderOffset)

isDirectoryLengthValid

private boolean isDirectoryLengthValid(int dirStartOffset,
                                       int tiffHeaderOffset)

processTag

private void processTag(Directory directory,
                        int tagType,
                        int tagValueOffset,
                        int componentCount,
                        int formatCode)

calculateTagValueOffset

private int calculateTagValueOffset(int byteCount,
                                    int dirEntryOffset,
                                    int tiffHeaderOffset)

readString

private String readString(int offset,
                          int maxLength)
Creates a String from the _data buffer starting at the specified offset, and ending where byte=='\0' or where length==maxLength.


calculateTagOffset

private int calculateTagOffset(int dirStartOffset,
                               int entryNumber)
Determine the offset at which a given InteropArray entry begins within the specified IFD.

Parameters:
dirStartOffset - the offset at which the IFD starts
entryNumber - the zero-based entry number

get16Bits

private int get16Bits(int offset)
Get a 16 bit value from file's native byte order. Between 0x0000 and 0xFFFF.


get32Bits

private int get32Bits(int offset)
Get a 32 bit value from file's native byte order.