Reputation: 13
I'm trying to figure out how to get the image rotation on Android/iOS devices using .NET MAUI. I have the following code to get the Image Width and height. NOTE: I allow the user to select an image from their device. This is the <path_to_image>.
I've got the following code working on Android devices:
using (Stream stream = File.OpenRead(<path_to_image>))
{
image = PlatformImage.FromStream(stream);
if (image != null)
{
Int32 width = Convert.ToInt32(image.Width);
Int32 heigh = Convert.ToInt32(image.Height);
}
}
I can use this code to determine if the image is landscape or portrait. But now I need the Metadata information of the image to determine the Metadata orientation (similar to the following: Problem reading JPEG Metadata (Orientation) which only works on Windows).
Thoughts?
Upvotes: 1
Views: 355
Reputation: 8245
ExifInterface Class is a class for reading and writing Exif tags in various image file formats for Android.
Let's say we have an image, and we use ExifInterface
to get the orientation of this image.
#if ANDROID
ExifInterface exifInterface = new ExifInterface(imagePath);
// ExifInterface exifInterface = new ExifInterface(inputStream);
ori = exifInterface.GetAttribute(ExifInterface.TagOrientation);
#endif
Please note that we may get the value of 0,1,2,3.., which has different meanings of direction. You may refer to the android docs for more info, ExifInterface.
For iOS, UIImage has UIImage.Orientation Property, which you may get it using the following way.
#if IOS
UIImage image = new UIImage(imagePath);
var ori = image.Orientation;
//Console.WriteLine(image.Orientation);
#endif
For more info, you may also refer to the Apple;s doc, UIImageOrientation
As you can see, the above code use Conditional compilation to invoke platform code for Android and iOS.
Upvotes: 2