image - 使用 SkiaSharp 调整大小时 iPhone 图像方向错误

标签 image image-processing xamarin xamarin.forms skiasharp

我在 Xamarin.Forms 项目上的 SkiaSharp 中使用 SKBitmap.Resize() 方法来调整图像大小以进行显示。我遇到的问题是在 iOS 上拍照时,当纵向拍照时,图像右侧朝上显示。在 Android 上拍照,从 Android 和 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() 只是获取照片存储位置的特定于平台的路径的助手。

最佳答案

对于那些有同样问题的人,我做了以下工作,并很乐意接受改进的意见。

        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);

关于image - 使用 SkiaSharp 调整大小时 iPhone 图像方向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44181914/

相关文章:

CSS Sprite 图像和响应式设计。它是否同时加载全尺寸和响应尺寸?

python - 如何使用 imageio 调整图像大小以获得正确的 ICO 文件?

c++ - OpenCV 中的轮廓?

image-processing - 人脸认证

c# - 如何从 Controller 方法返回多个图像?

visual-studio-2010 - 在 Visual Studio 中使用 Xamarin Android Designer 不会更改布局

firebase - 如何处理 Firebase 通知,即 Android 中的通知消息和数据消息

图像替换上的 jQuery 动画

javascript - 如何检查文本覆盖图像的百分比?

java - Xamarin KeyStore 密码保护字段