2015/09/30

Read the image's EXIF using Sanselan Android

Output result


Sample code:
/*
path is String, for example, /storage/emultaed/0/DCIM/abc.jpg
*/
IImageMetadata metadata = Sanselan.getMetadata(new File(path));
if (metadata instanceof JpegImageMetadata)
{
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
TiffImageMetadata exifMetadata = jpegMetadata.getExif();
TiffField field,imgWidth,imgHeight;
// Show Date
field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_MODIFY_DATE);
if (field != null)
{
    String v = field.getStringValue();
    txtDate.setText(v);
}
// Show Resolution
imgWidth = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_EXIF_IMAGE_WIDTH);
imgHeight = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_EXIF_IMAGE_LENGTH);
if ((imgWidth != null) && (imgHeight != null))
{
    int w = imgWidth.getIntValue();
    int h = imgHeight.getIntValue();
    txtRes.setText(Integer.toString(w)+" x "+Integer.toString(h));
}
// Show Camera info
field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_MODEL);
if (field != null)
{
    String v = field.getStringValue();
    txtCam.setText(v);
}
// Show ISO
field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_ISO);
if (field != null)
{
    int v = field.getIntValue();
    txtIso.setText(Integer.toString(v));
}
// Show Exposure time
field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_EXPOSURE_TIME);
if (field != null)
{
    double v = field.getDoubleValue();
    txtExp.setText(String.format("%.3f", v));
}
// Show Aperture
field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
if (field != null)
{
    double v = field.getDoubleValue();
    txtApe.setText(String.format("%.1f", v));
}
// Show Focal length
field = jpegMetadata.findEXIFValue(ExifTagConstants.EXIF_TAG_FOCAL_LENGTH);
if (field != null)
{
    double v = field.getDoubleValue();
    txtFoc.setText(Double.toString(v));
}          
}