我有一个应用程序,用户可以使用FilePicker.PickMultipleAsync通过图库添加照片。此外,用户还可以使用CrossMedia插件拍摄这一功能。
如果用户以肖像格式拍摄照片(CrossMedia),则将照片转换为要显示的ImageSource。如果用户选择了图像,它就会执行对ImageSource的相同转换过程,但是在这种情况下,照片会自动旋转到右边。
这种情况发生在相机的本地应用程序拍摄的照片上。如果用户下载任何图像并从图库中选择它,问题就不会发生。这个问题是在将插件切换到FilePicker之后开始的。
我能做些什么来显示原始图像?
版本: Xamarin.Forms: 5.0.0.2401 Xamarin.Essentials: 1.7.3
插件代码
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)));
}

我从网上下载的左边的图片显示得很正确。右边的照片是我在相机前拍摄的,然后从画廊中选择了
发布于 2022-06-22 10:55:05
我用代码解决了这个问题
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
https://stackoverflow.com/questions/72662710
复制相似问题