Skip to content

Commit

Permalink
#1363 / #1293 - rotation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
budowski committed Sep 29, 2024
1 parent 1f78b3d commit 616d6ae
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public void onTimezone(String timezoneName) {
taxonSuggestions = getTaxonSuggestions(obsUrl, latitude, longitude, observedOn, suggestionSource, placeId, taxonId, placeLat, placeLng, limit, page);
} else {
// Local photo - Resize photo to 640x640 max, not using Lanczos
String resizedPhotoFilename = ImageUtils.resizeImage(mContext, obsFilename, null, 640, true);
String resizedPhotoFilename = ImageUtils.resizeImage(mContext, obsFilename, null, 640, true, false);

if (resizedPhotoFilename != null) {
taxonSuggestions = getTaxonSuggestions(resizedPhotoFilename, latitude, longitude, observedOn, suggestionSource, placeId, taxonId, placeLat, placeLng, limit, page);
Expand Down
23 changes: 8 additions & 15 deletions iNaturalist/src/main/java/org/inaturalist/android/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,19 +353,21 @@ public static Bitmap rotateImage(Bitmap bitmapImage, int orientation) {



public static String resizeImage(Context context, String path, Uri photoUri, int maxDimensions) {
return resizeImage(context, path, photoUri, maxDimensions, false);
public static String resizeImage(Context context, String path, Uri photoUri, int maxDimensions, boolean isCameraPhoto) {
return resizeImage(context, path, photoUri, maxDimensions, false, isCameraPhoto);
}
/**
* Resizes an image to max size
* @param path the path to the image filename (optional)
* @param photoUri the original Uri of the image
* @param noLanczos if True, will not use Lanczos to resize image (but rather bilinear resampling)
* @param isCameraPhoto if True, will not rotate photo orientation according to EXIF (photo taken by iNat camera itself)
* @return the resized image - or original image if smaller than 2048x2048
*/
public static String resizeImage(Context context, String path, Uri photoUri, int maxDimensions, boolean noLanczos) {
public static String resizeImage(Context context, String path, Uri photoUri, int maxDimensions, boolean noLanczos, boolean isCameraPhoto) {
InputStream is = null;
BitmapFactory.Options options = new BitmapFactory.Options();
Logger.tag(TAG).info("resizeImage: " + path + "/" + photoUri + ":" + isCameraPhoto);

try {
if (photoUri != null) {
Expand Down Expand Up @@ -398,9 +400,11 @@ public static String resizeImage(Context context, String path, Uri photoUri, int

int rotationDegrees = 0;

if (path != null && path.toLowerCase().endsWith("heic")) {

if (path != null) {
androidx.exifinterface.media.ExifInterface exif = new androidx.exifinterface.media.ExifInterface(path);
rotationDegrees = exif.getRotationDegrees();
Logger.tag(TAG).error("resizeImage: degrees: " + rotationDegrees);
}

if (photoUri != null) {
Expand Down Expand Up @@ -471,17 +475,6 @@ public static String resizeImage(Context context, String path, Uri photoUri, int
// BitmapFactory.decodeStream moves the reading cursor
is.close();

if (photoUri != null) {
is = context.getContentResolver().openInputStream(photoUri);
} else {
is = new FileInputStream(new File(path));
}

// Copy all EXIF data from original image into resized image
copyExifData(is, new File(imageFile.getAbsolutePath()), null);

is.close();

return imageFile.getAbsolutePath();

} catch (OutOfMemoryError e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public class ObservationEditor extends Fragment {
@State public boolean mIsCaptive = false;
@State public boolean mChoseNewPhoto = false;
@State public boolean mChoseNewSound = false;
@State public boolean mCameraPhotoTaken = false;
private List<Uri> mSharePhotos = null;

@State public HashMap<Integer, Integer> mOriginalPhotoPositions = null;
Expand Down Expand Up @@ -1456,6 +1457,7 @@ public void onPermissionDenied() {

// In case a new/existing photo was taken - make sure we won't retake it in case the activity pauses/resumes.
mPictureTaken = true;
mCameraPhotoTaken = true;
}

private void chooseSound() {
Expand Down Expand Up @@ -1504,6 +1506,7 @@ public void onPermissionDenied() {

private void choosePhoto() {
Logger.tag(TAG).debug("choosePhoto");
mCameraPhotoTaken = false;

if (!mApp.isExternalStoragePermissionGranted()) {
Logger.tag(TAG).debug("choosePhoto - no storage permissions");
Expand Down Expand Up @@ -3581,7 +3584,7 @@ private Uri createObservationSoundForSound(Uri soundUri) {
}

private Uri createObservationPhotoForPhoto(Uri photoUri, int position, boolean isDuplicated) {
Logger.tag(TAG).debug("createObservationPhotoForPhoto: " + photoUri + ":" + position + ":" + isDuplicated);
Logger.tag(TAG).debug("createObservationPhotoForPhoto: " + photoUri + ":" + position + ":" + isDuplicated + ":" + mCameraPhotoTaken);

mPhotosChanged = true;

Expand Down Expand Up @@ -3613,7 +3616,7 @@ private Uri createObservationPhotoForPhoto(Uri photoUri, int position, boolean i
}

// Resize photo to 2048x2048 max
String resizedPhoto = ImageUtils.resizeImage(getActivity(), path, isDuplicated ? null : photoUri, 2048);
String resizedPhoto = ImageUtils.resizeImage(getActivity(), path, isDuplicated ? null : photoUri, 2048, mCameraPhotoTaken);

if (resizedPhoto == null) {
return null;
Expand All @@ -3623,7 +3626,7 @@ private Uri createObservationPhotoForPhoto(Uri photoUri, int position, boolean i
}

// Save original-sized copy of the photo (so when cropping, we'll crop from the original sized photo)
String originalSizePhoto = ImageUtils.resizeImage(getActivity(), path, isDuplicated ? null : photoUri, Integer.MAX_VALUE);
String originalSizePhoto = ImageUtils.resizeImage(getActivity(), path, isDuplicated ? null : photoUri, Integer.MAX_VALUE, mCameraPhotoTaken);

ObservationPhoto op = new ObservationPhoto();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3795,7 +3795,7 @@ private Uri createObservationPhotoForPhoto(Uri photoUri, int position, boolean i
}

// Resize photo to 2048x2048 max
String resizedPhoto = ImageUtils.resizeImage(getActivity(), path, photoUri, 2048);
String resizedPhoto = ImageUtils.resizeImage(getActivity(), path, photoUri, 2048, false);

if (resizedPhoto == null) {
return null;
Expand Down

0 comments on commit 616d6ae

Please sign in to comment.