Skip to content

Commit

Permalink
Android EXIF orientation calculates proper size before downsampling #130
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-luberda committed Jan 26, 2016
1 parent 1279712 commit 7114cbc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
97 changes: 49 additions & 48 deletions source/FFImageLoading.Droid/Extensions/ExifExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,70 @@
using System;
using Android.Graphics;
using FFImageLoading.Work;
using Android.Media;

namespace FFImageLoading.Extensions
{
public static class ExifExtensions
{
public static Bitmap ToExifRotatedBitmap(this Bitmap sourceBitmap, ImageSource source, string identifier)
public static int GetExifRotationDegrees(this string filePath)
{
if (source == ImageSource.Filepath)
{
int rotation = 0;
var exifInt = new ExifInterface(identifier);
int exifRotation = exifInt.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);
int rotation = 0;
var exifInt = new ExifInterface(filePath);
int exifRotation = exifInt.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);

switch (exifRotation)
{
case (int) Orientation.Rotate270:
rotation = 270;
break;
case (int) Orientation.Rotate180:
rotation = 180;
break;
case (int) Orientation.Rotate90:
rotation = 90;
break;
}
switch (exifRotation)
{
case (int) Orientation.Rotate270:
rotation = 270;
break;
case (int) Orientation.Rotate180:
rotation = 180;
break;
case (int) Orientation.Rotate90:
rotation = 90;
break;
default:
return 0;
}

if (rotation == 0)
return sourceBitmap;
return rotation;
}

int width = sourceBitmap.Width;
int height = sourceBitmap.Height;
public static Bitmap ToRotatedBitmap(this Bitmap sourceBitmap, int rotationDegrees)
{
if (rotationDegrees == 0)
return sourceBitmap;

if (rotation == 90 || rotation == 270)
{
width = sourceBitmap.Height;
height = sourceBitmap.Width;
}
int width = sourceBitmap.Width;
int height = sourceBitmap.Height;

Bitmap bitmap = Bitmap.CreateBitmap(width, height, sourceBitmap.GetConfig());
using (Canvas canvas = new Canvas(bitmap))
using (Paint paint = new Paint())
using (BitmapShader shader = new BitmapShader(sourceBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
using (Matrix matrix = new Matrix())
{
// paint.AntiAlias = true;
// paint.Dither = true;
// paint.FilterBitmap = true;
if (rotationDegrees == 90 || rotationDegrees == 270)
{
width = sourceBitmap.Height;
height = sourceBitmap.Width;
}

matrix.PostRotate(rotation);
canvas.DrawBitmap(sourceBitmap, matrix, paint);
}
Bitmap bitmap = Bitmap.CreateBitmap(width, height, sourceBitmap.GetConfig());
using (Canvas canvas = new Canvas(bitmap))
using (Paint paint = new Paint())
using (BitmapShader shader = new BitmapShader(sourceBitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
using (Matrix matrix = new Matrix())
{
// paint.AntiAlias = true;
// paint.Dither = true;
// paint.FilterBitmap = true;

if (sourceBitmap != null && sourceBitmap.Handle != IntPtr.Zero && !sourceBitmap.IsRecycled)
{
sourceBitmap.Recycle();
sourceBitmap.Dispose();
}
matrix.PostRotate(rotationDegrees);
canvas.DrawBitmap(sourceBitmap, matrix, paint);
}

return bitmap;
if (sourceBitmap != null && sourceBitmap.Handle != IntPtr.Zero && !sourceBitmap.IsRecycled)
{
sourceBitmap.Recycle();
sourceBitmap.Dispose();
}
return sourceBitmap;

return bitmap;
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion source/FFImageLoading.Droid/Work/ImageLoaderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ protected virtual async Task<WithLoadingResult<SelfDisposingBitmapDrawable>> Get
options.InPreferredConfig = Bitmap.Config.Rgb565;
}
// CHECK IF BITMAP IS EXIF ROTATED
int exifRotation = 0;
if (source == ImageSource.Filepath)
{
exifRotation = path.GetExifRotationDegrees();
}
try
{
if (Parameters.DownSampleSize != null && (Parameters.DownSampleSize.Item1 > 0 || Parameters.DownSampleSize.Item2 > 0))
Expand All @@ -384,6 +391,13 @@ protected virtual async Task<WithLoadingResult<SelfDisposingBitmapDrawable>> Get
int downsampleWidth = Parameters.DownSampleSize.Item1;
int downsampleHeight = Parameters.DownSampleSize.Item2;
// if image is rotated, swap width/height
if (exifRotation == 90 || exifRotation == 270)
{
downsampleWidth = Parameters.DownSampleSize.Item2;
downsampleHeight = Parameters.DownSampleSize.Item1;
}
if (Parameters.DownSampleUseDipUnits)
{
downsampleWidth = downsampleWidth.DpToPixels();
Expand Down Expand Up @@ -440,7 +454,8 @@ protected virtual async Task<WithLoadingResult<SelfDisposingBitmapDrawable>> Get
return null;
// APPLY EXIF ORIENTATION IF NEEDED
bitmap = bitmap.ToExifRotatedBitmap(source, path);
if (exifRotation != 0)
bitmap = bitmap.ToRotatedBitmap(exifRotation);
bool transformPlaceholdersEnabled = Parameters.TransformPlaceholdersEnabled.HasValue ?
Parameters.TransformPlaceholdersEnabled.Value : ImageService.Config.TransformPlaceholders;
Expand Down

4 comments on commit 7114cbc

@molinch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Exif issue is it only when loaded from file? 7114cbc#diff-730a715a87b5e94e9dd2b2660d64a9d5R380

@daniel-luberda
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@molinch Yes, currently it rotates only local files (eg. pictures taken with camera app). Do you think we also need to rotate cached images (Uri source)?

@molinch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should ask that on github
If we can avoid it, it's better :)

@daniel-luberda
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked already, here: #130 (comment)

Please sign in to comment.