Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.Sanselan;
import org.apache.sanselan.common.IImageMetadata;
import org.apache.sanselan.common.ImageMetadata.Item;
import org.apache.sanselan.common.RationalNumber;
import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
import org.apache.sanselan.formats.tiff.TiffField;
import org.apache.sanselan.formats.tiff.TiffImageMetadata;
import org.apache.sanselan.formats.tiff.constants.ExifTagConstants;
import org.apache.sanselan.formats.tiff.constants.GPSTagConstants;
import org.apache.sanselan.formats.tiff.constants.TiffTagConstants;
import org.apache.sanselan.formats.tiff.constants.TagInfo;
class ImageDataContainer{
private static HashMap<String, TagInfo> Tags = null;
public static String getFileTag(File file, TagInfo ti){
final IImageMetadata metadata;
try {
metadata = Sanselan.getMetadata(file);
if (metadata instanceof JpegImageMetadata) {
final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(ti);
if(field == null) return "NoTagVal";
if(ti == GPSTagConstants.GPS_TAG_GPS_LATITUDE || ti == GPSTagConstants.GPS_TAG_GPS_LONGITUDE){
RationalNumber gpsValue[] = (RationalNumber[]) field.getValue();
RationalNumber gpsLatitudeDegrees = gpsValue[0];
RationalNumber gpsLatitudeMinutes = gpsValue[1];
RationalNumber gpsLatitudeSeconds = gpsValue[2];
return (String) gpsLatitudeDegrees.toDisplayString() + " degs, " + gpsLatitudeMinutes.toDisplayString() + " mins, " + gpsLatitudeSeconds.toDisplayString() + " secs";
}else if(ti == TiffTagConstants.TIFF_TAG_XRESOLUTION || ti == TiffTagConstants.TIFF_TAG_YRESOLUTION) {
return String.valueOf((RationalNumber) field.getValue());
}else{
return (String) field.getValue();
}
}
} catch (ImageReadException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String keyval = "CANT FIND NAME";
for (Object o : Tags.keySet()) {
if (Tags.get(o).equals(ti)) {
keyval = (String) o;
}
}
Log.d("NoTagVal", "No tag for " + keyval);
return "NoTagVal";
}
public static HashMap<String, TagInfo> initializeTags(){
if (Tags != null) return Tags;
Tags = new HashMap<>();
Tags.put("description", TiffTagConstants.TIFF_TAG_IMAGE_DESCRIPTION);
Tags.put("x_resolution", TiffTagConstants.TIFF_TAG_XRESOLUTION);
Tags.put("y_resolution", TiffTagConstants.TIFF_TAG_YRESOLUTION);
Tags.put("date", TiffTagConstants.TIFF_TAG_DATE_TIME);
Tags.put("name", TiffTagConstants.TIFF_TAG_DOCUMENT_NAME);
Tags.put("gps_lat_ref",GPSTagConstants.GPS_TAG_GPS_LATITUDE_REF);
Tags.put("gps_lon_ref",GPSTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
Tags.put("gps_lat",GPSTagConstants.GPS_TAG_GPS_LATITUDE);
Tags.put("gps_lon",GPSTagConstants.GPS_TAG_GPS_LONGITUDE);
//TODO: Add Focal length Tag.
return Tags;
}
private static void printTagValue(final JpegImageMetadata jpegMetadata,
final TagInfo tagInfo) {
final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tagInfo);
if (field == null) {
System.out.println(tagInfo.name + ": " + "Not Found.");
} else {
System.out.println(tagInfo.name + ": "
+ field.getValueDescription());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment