Skip to content

Commit

Permalink
imageCaptureProcessing maintain EXIF
Browse files Browse the repository at this point in the history
  • Loading branch information
melmathari committed Nov 24, 2024
1 parent bbb7a96 commit 2fb86ba
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ private static Pair<File, String> moveAndScaleImage(File originalImage, boolean
if (currentWidget != null) {
int maxDimen = currentWidget.getMaxDimen();
if (maxDimen != -1) {
savedScaledImage = FileUtil.scaleAndSaveImage(originalImage, tempFilePathForScaledImage, maxDimen);
File tempFile = new File(tempFilePathForScaledImage);
savedScaledImage = FileUtil.scaleAndSaveImageWithExif(originalImage, tempFile, maxDimen);
}
}
}
Expand Down Expand Up @@ -92,7 +93,7 @@ private static File makeRawCopy(File originalImage, String instanceFolder, Strin
}
File rawImageFile = new File(rawDirPath + "/" + imageFilename);
try {
FileUtil.copyFile(originalImage, rawImageFile);
FileUtil.copyFileWithExifData(originalImage, rawImageFile);
} catch (Exception e) {
throw new IOException("Failed to rename " + originalImage.getAbsolutePath() +
" to " + rawImageFile.getAbsolutePath());
Expand Down
69 changes: 69 additions & 0 deletions app/src/org/commcare/utils/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -857,4 +858,72 @@ public static boolean isFileTooLargeToUpload(ContentResolver contentResolver, Ur
return returnCursor.getLong(sizeIndex) > FormUploadUtil.MAX_BYTES;
}
}

public static void copyFileWithExifData(File sourceFile, File destFile) throws IOException {
// First copy the file normally
copyFile(sourceFile, destFile);

// Then copy EXIF data
ExifInterface sourceExif = new ExifInterface(sourceFile.getAbsolutePath());
ExifInterface destExif = new ExifInterface(destFile.getAbsolutePath());

// Copy GPS data
String[] tagsToPreserve = {
ExifInterface.TAG_GPS_LATITUDE,
ExifInterface.TAG_GPS_LATITUDE_REF,
ExifInterface.TAG_GPS_LONGITUDE,
ExifInterface.TAG_GPS_LONGITUDE_REF,
ExifInterface.TAG_GPS_TIMESTAMP,
ExifInterface.TAG_GPS_DATESTAMP,
// Preserve other important EXIF data
ExifInterface.TAG_DATETIME,
ExifInterface.TAG_ORIENTATION
};

for (String tag : tagsToPreserve) {
String value = sourceExif.getAttribute(tag);
if (value != null) {
destExif.setAttribute(tag, value);
}
}

destExif.saveAttributes();
}

public static boolean scaleAndSaveImageWithExif(File sourceFile, File destFile, int maxDimen) throws IOException {
// First scale the image
boolean scaled = scaleAndSaveImage(sourceFile, destFile.getAbsolutePath(), maxDimen);

if (scaled) {
// Copy EXIF data from source to scaled image
ExifInterface sourceExif = new ExifInterface(sourceFile.getAbsolutePath());
ExifInterface destExif = new ExifInterface(destFile.getAbsolutePath());

// Copy GPS and other important EXIF data
copyExifData(sourceExif, destExif);
destExif.saveAttributes();
}

return scaled;
}

private static void copyExifData(ExifInterface source, ExifInterface dest) {
String[] tagsToPreserve = {
ExifInterface.TAG_GPS_LATITUDE,
ExifInterface.TAG_GPS_LATITUDE_REF,
ExifInterface.TAG_GPS_LONGITUDE,
ExifInterface.TAG_GPS_LONGITUDE_REF,
ExifInterface.TAG_GPS_TIMESTAMP,
ExifInterface.TAG_GPS_DATESTAMP,
ExifInterface.TAG_DATETIME,
ExifInterface.TAG_ORIENTATION
};

for (String tag : tagsToPreserve) {
String value = source.getAttribute(tag);
if (value != null) {
dest.setAttribute(tag, value);
}
}
}
}

0 comments on commit 2fb86ba

Please sign in to comment.