I'm using the SKBitmap.Resize() method in SkiaSharp on a Xamarin.Forms project to resize images for display. The problem I'm encountering is when taking a photo on iOS, when a photo is taken in portrait, the image is displayed with the right side up. Taking a photo on Android, importing from the photo gallery on both an Android and iOS device maintains orientation, but taking a photo in iOS does not. If I don't resize the image using SkiaSharp (just display the image without any resizing), then the image displays with the proper orientation. However that is not a solution as the images need to be resized. Below is my code -
private byte[] GetResizedImageData(string imageName)
{
float resizeFactor = 0.5f;
var filePath = PathUtil.GetImagePath(imageName);
var ogBitmap = SKBitmap.Decode(filePath);
float fWidth = ogBitmap.Width * resizeFactor;
int width = (int) Math.Round(fWidth);
float fHeight = ogBitmap.Height * resizeFactor;
int height = (int) Math.Round(fHeight);
if (height >= 4096 || width >= 4096)
{
width = width * (int)resizeFactor;
height = height * (int)resizeFactor;
}
var scaledBitmap = ogBitmap.Resize(new SKImageInfo( width, height), SKBitmapResizeMethod.Box);
var image = SKImage.FromBitmap(scaledBitmap);
var data = image.Encode(SKEncodedImageFormat.Jpeg, 100);
return data.ToArray();
}
PathUtil.GetImagePath() is just a helper to get platform-specific paths for where the photos are being stored.
For those with the same issue I did the following and would gladly accept input on improvements.
public static SKBitmap HandleOrientation(SKBitmap bitmap, SKCodecOrigin orientation)
{
SKBitmap rotated;
switch (orientation)
{
case SKCodecOrigin.BottomRight:
using (var surface = new SKCanvas(bitmap))
{
surface.RotateDegrees(180, bitmap.Width / 2, bitmap.Height / 2);
surface.DrawBitmap(bitmap.Copy(), 0, 0);
}
return bitmap;
case SKCodecOrigin.RightTop:
rotated = new SKBitmap(bitmap.Height, bitmap.Width);
using (var surface = new SKCanvas(rotated))
{
surface.Translate(rotated.Width, 0);
surface.RotateDegrees(90);
surface.DrawBitmap(bitmap, 0, 0);
}
return rotated;
case SKCodecOrigin.LeftBottom:
rotated = new SKBitmap(bitmap.Height, bitmap.Width);
using (var surface = new SKCanvas(rotated))
{
surface.Translate(0, rotated.Height);
surface.RotateDegrees(270);
surface.DrawBitmap(bitmap, 0, 0);
}
return rotated;
default:
return bitmap;
}
And then use the following to get the original orientation.
// TODO: Improve this.. I do not know how to "reset"
// the inputStream, so new it up twice. :/
using (var inputStream = new SKManagedStream(imageIn))
{
using (var codec = SKCodec.Create(inputStream))
{
orientation = codec.Origin;
}
}
....... Then
SKBitmap orientedWExif = HandleOrientation(resized, orientation);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With