MAUI:将图像保存为从相机 Microsoft.Maui.Media.MediaPicker 获取的 ByteArrayContent 时出现问题

问题描述 投票:0回答:1

在我的 MAUI 应用程序中,有一个个人资料页面,用户可以在其中查看和编辑他们的个人详细信息。此页面包含更新个人资料图像的功能。用户可以通过三个选项来添加个人资料图片:

  1. 使用相机拍照。
  2. 从图库中选择图像。

当用户尝试将其个人资料详细信息与图像一起保存时,图像不会被保存。从相机拍摄的图像尤其会出现此问题。为了保存个人资料详细信息,我使用 POST API 调用。该问题似乎不会影响从图库中选择的图像;这些图像已正确保存。

为了从相机和画廊拍照,我使用

Microsoft.Maui.Media.MediaPicker
包,为了组合路径,我使用
Microsoft.Maui.Storage.FileSystem

下面是相机拍照并梳理路径的代码:

FileResult photo;
photo = await MediaPicker.CapturePhotoAsync();  
if (photo != null)  
{  
    var imageFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);  
    using (var stream = await photo.OpenReadAsync())  
    using (var newStream = File.OpenWrite(imageFile))  
    await stream.CopyToAsync(newStream);  
    profilephoto.Source = imageFile;  
    isPicture = true;  
}  
else  
{  
    isPicture = false;  
}  

下面是从图库中获取图像并梳理路径的代码:

FileResult photo;
photo = await MediaPicker.PickPhotoAsync();  
if (photo != null)  
{  
    var imageFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);  
    using (var stream = await photo.OpenReadAsync())  
    using (var newStream = File.OpenWrite(imageFile))  
    await stream.CopyToAsync(newStream);  
    profilephoto.Source = imageFile; 
    isPicture = true;  
}  
else  
{  
    isPicture = false; 
} 

下面是将相机拍摄的图像发送到 POST API 的代码:(不起作用)

content.Add(new StreamContent(await photo.OpenReadAsync()), "\"file\"", $"\"{photo.FullPath}\"");

下面是将从图库中拍摄的图像发送到 POST API 的代码:(工作正常)

var fileBytes = File.ReadAllBytes(photo.FullPath);  
ByteArrayContent byteContent = new ByteArrayContent(fileBytes);  
content.Add(byteContent, "file", Path.GetFileName(photo.FullPath)); 

我正在寻找一种从相机发送图像的解决方案。根据上述代码,图库图像工作正常。

maui
1个回答
0
投票

我使用下面的代码修复了相机图像上传问题。我还使用了 SkiaSharp 包来压缩图像大小。

public async void CameraClick()
{
    if (MediaPicker.Default.IsCaptureSupported)
    {
        FileResult photo = await MediaPicker.Default.CapturePhotoAsync();

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

            // Open the photo as a stream
            using Stream sourceStream = await photo.OpenReadAsync();

            // Load the image using SkiaSharp
            using SKBitmap originalBitmap = SKBitmap.Decode(sourceStream);

            // Define the desired width and height
            int desiredWidth = 800; // change as needed
            int desiredHeight = 600; // change as needed

            // Resize the image
            using SKBitmap resizedBitmap = originalBitmap.Resize(new SKImageInfo(desiredWidth, desiredHeight), SKFilterQuality.High);

            // Encode the resized image to a byte array
            using SKImage image = SKImage.FromBitmap(resizedBitmap);
            using SKData encodedData = image.Encode(SKEncodedImageFormat.Jpeg, 75); // 75 is the quality, you can change it
            byte[] resizedImageBytes = encodedData.ToArray();

            // Save the resized image to local storage
            string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
            File.WriteAllBytes(localFilePath, resizedImageBytes);

            // Set the image source
            profilephoto.Source = localFilePath;

            // Update other variables
            cameraByteContents = new ByteArrayContent(resizedImageBytes);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.