我有一個應用程式,用戶可以使用 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)));
}

我從互聯網上下載了左側的影像,它顯示正確。我用相機拍攝并從圖庫中選擇的右側影像
uj5u.com熱心網友回復:
我用代碼解決了這個問題
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://www.uj5u.com/qukuanlian/495571.html
標籤:xamarin xamarin.forms 文件选择器 xamarin 必需品
上一篇:Xamarin,CompilationFailedException:編譯未能完成,來源:classes.jar:ApkLibraryInstaller$ZipFileInZipEntry.class
下一篇:Xamarin中的scoolview出現問題,沒有列印資料。當我在應用程式作業時重新輸入ItemsSource(Persons)時它作業
