首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >茜素FilePicker ImageSource

茜素FilePicker ImageSource
EN

Stack Overflow用户
提问于 2022-06-17 17:09:07
回答 1查看 151关注 0票数 0

我有一个应用程序,用户可以使用FilePicker.PickMultipleAsync通过图库添加照片。此外,用户还可以使用CrossMedia插件拍摄这一功能。

如果用户以肖像格式拍摄照片(CrossMedia),则将照片转换为要显示的ImageSource。如果用户选择了图像,它就会执行对ImageSource的相同转换过程,但是在这种情况下,照片会自动旋转到右边。

这种情况发生在相机的本地应用程序拍摄的照片上。如果用户下载任何图像并从图库中选择它,问题就不会发生。这个问题是在将插件切换到FilePicker之后开始的。

我能做些什么来显示原始图像?

版本: Xamarin.Forms: 5.0.0.2401 Xamarin.Essentials: 1.7.3

插件代码

代码语言:javascript
复制
var result = await FilePicker.PickMultipleAsync(new PickOptions
{
    PickerTitle = "Choose Images",
    FileTypes = FilePickerFileType.Images
});

if (result != null)
{
    foreach (var item in result)
    {
        AttachedFiles.Add(new AttachmentFiles(item));
    }
}

public AttachmentFiles(FileResult imgSource)
{
    var imageAsBytes = ImageHelper.ConvertStreamToByteArray(Task.Run(async () => await imgSource.OpenReadAsync()).Result);
    var resizer = DependencyService.Get<IImageResize>();

    this.ImageId = Guid.NewGuid();
    this.Source = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
    this.SourceT = ImageSource.FromStream(() => new MemoryStream(resizer.ResizeImage(imageAsBytes, 70, 70)));
}

我从网上下载的左边的图片显示得很正确。右边的照片是我在相机前拍摄的,然后从画廊中选择了

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-22 10:55:05

我用代码解决了这个问题

代码语言:javascript
复制
 public class PhotoPickerService : IPhotoPickerService
    {
        public async Task<byte[]> ImageToArrayAsync(string path)
        {
            try
            {
                var exif = new Android.Media.ExifInterface(path);
                string orientation = exif.GetAttribute(Android.Media.ExifInterface.TagOrientation);

                //Get the bitmap.
                var originalImage = BitmapFactory.DecodeFile(path);

                //Set imageSize and imageCompression parameters.
                var imageSize = .40;
                var imageCompression = 45;

                //Resize it and then compress it to Jpeg.
                var width = originalImage.Width * imageSize;
                var height = originalImage.Height * imageSize;
                var scaledImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, true);

                var matrix = new Matrix();

                switch (orientation)
                {
                    case "1": // landscape
                        break;
                    case "3":
                        matrix.PreRotate(180);
                        scaledImage = Bitmap.CreateBitmap(scaledImage, 0, 0, scaledImage.Width, scaledImage.Height, matrix, true);
                        matrix.Dispose();
                        matrix = null;
                        break;
                    case "4":
                        matrix.PreRotate(180);
                        scaledImage = Bitmap.CreateBitmap(scaledImage, 0, 0, scaledImage.Width, scaledImage.Height, matrix, true);
                        matrix.Dispose();
                        matrix = null;
                        break;
                    case "5":
                        matrix.PreRotate(90);
                        scaledImage = Bitmap.CreateBitmap(scaledImage, 0, 0, scaledImage.Width, scaledImage.Height, matrix, true);
                        matrix.Dispose();
                        matrix = null;
                        break;
                    case "6": // portrait
                        matrix.PreRotate(90);
                        scaledImage = Bitmap.CreateBitmap(scaledImage, 0, 0, scaledImage.Width, scaledImage.Height, matrix, true);
                        matrix.Dispose();
                        matrix = null;
                        break;
                    case "7":
                        matrix.PreRotate(-90);
                        scaledImage = Bitmap.CreateBitmap(scaledImage, 0, 0, scaledImage.Width, scaledImage.Height, matrix, true);
                        matrix.Dispose();
                        matrix = null;
                        break;
                    case "8":
                        matrix.PreRotate(-90);
                        scaledImage = Bitmap.CreateBitmap(scaledImage, 0, 0, scaledImage.Width, scaledImage.Height, matrix, true);
                        matrix.Dispose();
                        matrix = null;
                        break;
                }

                byte[] imageBytes;

                using (MemoryStream ms = new MemoryStream())
                {
                    scaledImage.Compress(Bitmap.CompressFormat.Jpeg, imageCompression, ms);
                    imageBytes = ms.ToArray();
                    await File.WriteAllBytesAsync(path, imageBytes);
                }

                originalImage.Recycle();
                scaledImage.Recycle();
                originalImage.Dispose();
                scaledImage.Dispose();

                return imageBytes;
            }

            catch (IOException ex)
            {
                _ = ex.Message;
                return null;
            }
        }
    }

来源:https://github.com/xamarin/Essentials/issues/1514#issuecomment-922233449

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72662710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档