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

.NET MAUI Android15相机打开后拍照镜像翻转问题求助

.NET MAUI Android 15相机拍照后图片镜像翻转问题

问题概述

我的.NET MAUI应用通过MediaPicker.CapturePhotoAsync()调用相机功能,在其他Android版本运行正常,但在Android 15上出现两个问题:

  • 最初无法打开相机,添加Manifest配置后相机可正常启动
  • 拍照完成后图片出现镜像翻转,该问题仅在Android 15设备上存在

已尝试的代码实现

初始实现代码

try
{
    photo = await MediaPicker.CapturePhotoAsync();
    if (photo != null)
    {
        var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
        using (var stream = await photo.OpenReadAsync())
        using (var newStream = File.OpenWrite(newFile))
            await stream.CopyToAsync(newStream);

        profileImage.Source = newFile;
        isPicture = true;
    }
}
catch (Exception ex)
{
    Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
}

第二种实现(使用SkiaSharp处理图片)

try
{
    if (MediaPicker.Default.IsCaptureSupported)
    {
        FileResult photo = await MediaPicker.Default.CapturePhotoAsync();

        if (photo != null)
        {
            picPath = photo.FullPath;

            // 打开图片流
            using Stream sourceStream = await photo.OpenReadAsync();

            // 使用SkiaSharp加载图片
            using SKBitmap originalBitmap = SKBitmap.Decode(sourceStream);

            // 定义目标尺寸
            int desiredWidth = 800; // 可按需调整
            int desiredHeight = 600; // 可按需调整

            // 缩放图片
            using SKBitmap resizedBitmap = originalBitmap.Resize(new SKImageInfo(desiredWidth, desiredHeight), SKFilterQuality.High);

            // 将缩放后的图片编码为字节数组
            using SKImage image = SKImage.FromBitmap(resizedBitmap);
            using SKData encodedData = image.Encode(SKEncodedImageFormat.Jpeg, 75); // 75为图片质量,可调整
            byte[] resizedImageBytes = encodedData.ToArray();

            // 将图片保存到本地缓存
            string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
            File.WriteAllBytes(localFilePath, resizedImageBytes);

            // 设置图片源
            profileImage.Source = localFilePath;
            isPicture = true;
            isCamera = true;

            // 更新其他变量
            imageByteContents = new ByteArrayContent(resizedImageBytes);
        }
        else
        {
            isPicture = true;
            isCamera = true;
        }
        isGallery = false;
    }
}
catch (Exception exception)
{
    Console.WriteLine($"CapturePhotoAsync THREW: {exception}");
}

解决相机无法启动的Manifest配置

添加以下配置到AndroidManifest.xml后,相机可正常打开:

<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
</queries>

图片对比

  • 拍照原图:拍照原图
  • UI显示的翻转图片:UI显示的翻转图片

解决方案建议

Android 15对相机拍摄的图片元数据处理可能有调整,镜像问题通常是图片的EXIF方向信息未被正确解析导致的,可通过以下方式修复:

1. 解析并应用EXIF方向

使用SkiaSharp读取图片的EXIF方向信息,根据信息对图片进行翻转/旋转处理:

using var stream = await photo.OpenReadAsync();
using var codec = SKCodec.Create(stream);
var exifOrientation = codec.EncodedOrigin;

SKBitmap adjustedBitmap = originalBitmap;
switch (exifOrientation)
{
    case SKEncodedOrigin.TopRight:
        // 水平翻转
        adjustedBitmap.RotateFlip(SKRotateFlipType.RotateNoneFlipX);
        break;
    case SKEncodedOrigin.BottomLeft:
        // 180度旋转
        adjustedBitmap.RotateFlip(SKRotateFlipType.Rotate180FlipNone);
        break;
    // 可根据实际需求补充其他方向的处理逻辑
}

2. 确保EXIF数据被正确保留

在复制或处理图片时,不要丢失EXIF元数据。使用ImageSource.FromFile而非直接设置路径,让MAUI自动处理方向信息:

profileImage.Source = ImageSource.FromFile(newFile);

3. 尝试平台特定代码

如果MediaPicker的封装存在兼容性问题,可通过MAUI的平台特定代码调用Android原生相机API,直接获取带正确方向的图片。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 20:18:31