You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET MAUI中获取Android/iOS设备上的图片旋转方向?

.NET MAUI 获取Android/iOS图片旋转方向的解决方案

要读取图片的EXIF方向元数据,MAUI需要通过**依赖服务(Dependency Service)**实现分平台处理,以下是具体可行方案:

1. 定义共享接口

在MAUI项目的共享代码层创建统一接口,规范跨平台调用:

public interface IImageOrientationService
{
    // 返回EXIF方向值(1-8),对应不同旋转/翻转状态
    int GetImageOrientation(string imageFilePath);
}

2. Android平台实现

在Android项目中添加实现类,利用系统API读取EXIF元数据:

using Android.Media;
using Microsoft.Maui.Controls;

[assembly: Dependency(typeof(ImageOrientationService_Android))]
namespace YourAppNamespace.Droid.Services
{
    public class ImageOrientationService_Android : IImageOrientationService
    {
        public int GetImageOrientation(string imageFilePath)
        {
            try
            {
                using var exif = new ExifInterface(imageFilePath);
                // 获取EXIF方向标签,默认返回1(正常无旋转)
                return exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);
            }
            catch
            {
                // 读取失败返回默认正常方向
                return (int)Orientation.Normal;
            }
        }
    }
}

3. iOS平台实现

在iOS项目中添加实现类,通过ImageIO框架读取图片元数据:

using ImageIO;
using Foundation;
using Microsoft.Maui.Controls;

[assembly: Dependency(typeof(ImageOrientationService_iOS))]
namespace YourAppNamespace.iOS.Services
{
    public class ImageOrientationService_iOS : IImageOrientationService
    {
        public int GetImageOrientation(string imageFilePath)
        {
            try
            {
                var url = NSUrl.FromFilename(imageFilePath);
                using var source = CGImageSource.FromUrl(url);
                if (source == null) return 1;

                var properties = source.CopyPropertiesAtIndex(0, new NSDictionary());
                if (properties.TryGetValue(CGImageProperties.Orientation, out var orientationObj))
                {
                    return (int)(NSNumber)orientationObj;
                }
                return 1;
            }
            catch
            {
                return 1;
            }
        }
    }
}

4. 共享代码中调用(结合现有逻辑)

将方向读取与你已有的宽高获取逻辑结合,可进一步判断实际显示的旋转角度和宽高:

using (Stream stream = File.OpenRead("<path_to_image>"))
{
    var image = PlatformImage.FromStream(stream);
    if (image != null)
    {
        int width = Convert.ToInt32(image.Width);
        int height = Convert.ToInt32(image.Height);

        // 获取图片EXIF方向
        var orientationService = DependencyService.Get<IImageOrientationService>();
        int exifOrientation = orientationService.GetImageOrientation("<path_to_image>");

        // 根据方向映射旋转角度
        int rotationAngle = exifOrientation switch
        {
            1 => 0,          // 正常无旋转
            2 => 0,          // 水平翻转
            3 => 180,        // 顺时针180度
            4 => 180,        // 垂直翻转
            5 => 270,        // 顺时针270度+水平翻转
            6 => 90,         // 顺时针90度
            7 => 90,         // 顺时针90度+水平翻转
            8 => 270,        // 顺时针270度
            _ => 0
        };

        // 旋转90/270度时,实际显示宽高需互换
        bool swapDimensions = exifOrientation is 5 or 6 or 7 or 8;
        int actualDisplayWidth = swapDimensions ? height : width;
        int actualDisplayHeight = swapDimensions ? width : height;
    }
}

EXIF方向值对应状态说明

  • 1: 正常方向(0度旋转)
  • 2: 水平翻转
  • 3: 顺时针180度旋转
  • 4: 垂直翻转
  • 5: 顺时针270度旋转+水平翻转
  • 6: 顺时针90度旋转
  • 7: 顺时针90度旋转+水平翻转
  • 8: 顺时针270度旋转

内容的提问来源于stack exchange,提问作者Karen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 10:57:14