我在SkiaSharp中使用Xamarin.Forms项目中的Xamarin.Forms()方法来调整图像的大小以供显示。我遇到的问题是,当我在iOS上拍照时,当一张照片是在肖像上拍摄时,图像就会显示在右边。在安卓系统上拍照,从安卓和iOS设备的照片库中导入,都保持了定位,但在iOS上拍照却不是这样。如果我不使用SkiaSharp调整图像的大小(只需显示图像而不进行任何调整大小),那么图像将以正确的方向显示。然而,这不是一个解决方案,因为图像需要调整大小。下面是我的密码-
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()只是获取存储照片的平台特定路径的助手。
发布于 2017-06-15 21:08:14
实际上,SkiaSharp并没有为我提供一种方法来操作和改变图像的方向。最后,当我使用特定于平台的代码捕获和保存图像时,我最终改变了方向。
发布于 2017-08-10 17:55:30
对于那些有同样问题的人,我做了以下工作,并愿意接受关于改进的意见。
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;
}
然后使用下面的方法得到原始的方向。
// 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;
}
}
.然后
SKBitmap orientedWExif = HandleOrientation(resized, orientation);
发布于 2020-05-13 12:44:20
谢谢你的回答,你的回答帮助我解决了方向旋转的问题。只是想更新您的答案,因为有一些不推荐的代码。
using (var inputStream = new SKManagedStream(await file.ReadAllStreamAsync()))
{
using (var codec = SKCodec.Create(inputStream))
{
orientation = codec.EncodedOrigin;
}
}
public static SKBitmap HandleOrientation(SKBitmap bitmap, SKEncodedOrigin orientation)
{
SKBitmap rotated;
switch (orientation)
{
case SKEncodedOrigin.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 SKEncodedOrigin.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 SKEncodedOrigin.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;
}
}
https://stackoverflow.com/questions/44181914
复制相似问题