Reformat exposure time in exif data.

Exposure time is returned from camera as a string like 1/11.
ExifInterface on expects exposure time to be formatted as a string but
the string should contain decimal formatted number instead of fraction
format.
This commit is contained in:
Lauri Matti Saarni 2018-03-12 15:45:17 +07:00
parent f1094302c7
commit 009da5a250
1 changed files with 18 additions and 2 deletions

View File

@ -190,14 +190,18 @@ public class MutableImage {
// Add missing exif data from a sub directory // Add missing exif data from a sub directory
ExifSubIFDDirectory directory = originalImageMetaData() ExifSubIFDDirectory directory = originalImageMetaData()
.getFirstDirectoryOfType(ExifSubIFDDirectory.class); .getFirstDirectoryOfType(ExifSubIFDDirectory.class);
for (Tag tag : directory.getTags()) { for (Tag tag : directory.getTags()) {
int tagType = tag.getTagType(); int tagType = tag.getTagType();
// As some of exif data does not follow naming of the ExifInterface the names need // As some of exif data does not follow naming of the ExifInterface the names need
// to be transformed into Upper camel case format. // to be transformed into Upper camel case format.
String tagName = tag.getTagName().replaceAll(" ", ""); String tagName = tag.getTagName().replaceAll(" ", "");
Object object = directory.getObject(tagType); Object object = directory.getObject(tagType);
exif.setAttribute(tagName, object.toString()); if (tagName.equals(ExifInterface.TAG_EXPOSURE_TIME)) {
exif.setAttribute(tagName, convertExposureTimeToDoubleFormat(object.toString()));
} else {
exif.setAttribute(tagName, object.toString());
}
} }
writeLocationExifData(options, exif); writeLocationExifData(options, exif);
@ -211,6 +215,18 @@ public class MutableImage {
} }
} }
// Reformats exposure time value to match ExifInterface format. Example 1/11 -> 0.0909
// Even the value is formatted as double it is returned as a String because exif.setAttribute requires it.
private String convertExposureTimeToDoubleFormat(String exposureTime) {
if(!exposureTime.contains("/"))
return;
String exposureFractions[]= exposureTime.split("/");
double divider = Double.parseDouble(exposureFractions[1]);
double exposureTimeAsDouble = 1.0f / divider;
return Double.toString(exposureTimeAsDouble);
}
private void rewriteOrientation(ExifInterface exif) { private void rewriteOrientation(ExifInterface exif) {
exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_NORMAL)); exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_NORMAL));
} }