gpt4 book ai didi

android - 压缩后 Xamarin Forms Android 图像方向发生变化

转载 作者:行者123 更新时间:2023-11-29 01:28:31 25 4
gpt4 key购买 nike

我正在编写一个 Xamarin 表单 android 应用程序,我正在从图库中获取图像。我想将这些图像上传到服务器,所以我需要它的 byte[] 。我正在缩放和压缩这些图像,然后从中获取 byte[]。我的问题是当我压缩图像时,图像的方向从纵向变为横向。我试过使用“ExifInterface”类并更改图像方向,但它不起作用。以下是我的完整代码:-

protected override async void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
if (resultCode == Result.Canceled)
return;

try {
var mediafile = await data.GetMediaFileExtraAsync (Forms.Context);

byte[] data1 = ReadFully (mediafile.GetStream ());

byte[] resizedImage = ResizeImageAndroid (data1, 60, 60, mediafile);
var imageStream = new ByteArrayContent (resizedImage);
imageStream.Headers.ContentDisposition = new ContentDispositionHeaderValue ("attachment") {
FileName = Guid.NewGuid () + ".Png"
};

var multi = new MultipartContent ();
multi.Add (imageStream);
HealthcareProfessionalDataClass lDataClass = HealthcareProfessionalDataClass.Instance;
lDataClass.Thumbnail = multi;
App.mByteArrayOfImage = data1;

System.Diagnostics.Debug.WriteLine (mediafile.Path);

MessagingCenter.Send<IPictureTaker,string> (this, "picturetaken", mediafile.Path);
} catch (Java.Lang.Exception e) {
e.PrintStackTrace ();
}
}

public static byte[] ReadFully (System.IO.Stream input)
{
using (var ms = new MemoryStream ()) {
input.CopyTo (ms);
return ms.ToArray ();
}
}

public static byte[] ResizeImageAndroid (byte[] imageData, float width, float height, MediaFile file)
{
try {
// Load the bitmap

var options = new BitmapFactory.Options ();
options.InJustDecodeBounds = true;

// Calculate inSampleSize
options.InSampleSize = calculateInSampleSize (options, (int)width, (int)height);
// Decode bitmap with inSampleSize set
options.InJustDecodeBounds = false;

Bitmap originalImage = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length, options);
Bitmap resizedImage = Bitmap.CreateScaledBitmap (originalImage, (int)width, (int)height, false);


using (var ms = new MemoryStream ()) {

resizedImage.Compress (Bitmap.CompressFormat.Png, 0, ms);
resizedImage = changeOrientation (file, resizedImage);

return ms.ToArray ();
}
} catch (Java.Lang.Exception e) {
e.PrintStackTrace ();
return null;
}
}

public static int calculateInSampleSize (BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
int height = options.OutHeight;
int width = options.OutWidth;
int inSampleSize = 4;

if (height > reqHeight || width > reqWidth) {

int halfHeight = height / 2;
int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}

static Bitmap changeOrientation (MediaFile mediafile, Bitmap bitmap)
{
var exifInterface = new ExifInterface (mediafile.Path);
int orientation = exifInterface.GetAttributeInt (ExifInterface.TagOrientation, 0);
var matrix = new Matrix ();
switch (orientation) {
case 2:
matrix.SetScale (-1, 1);
break;
case 3:
matrix.SetRotate (180);
break;
case 4:
matrix.SetRotate (180);
matrix.PostScale (-1, 1);
break;
case 5:
matrix.SetRotate (90);
matrix.PostScale (-1, 1);
break;
case 6:
matrix.SetRotate (90);
break;
case 7:
matrix.SetRotate (-90);
matrix.PostScale (-1, 1);
break;
case 8:
matrix.SetRotate (-90);
break;
default:
return bitmap;
}

try {
Bitmap oriented = Bitmap.CreateBitmap (bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);
bitmap.Recycle ();
return oriented;
} catch (OutOfMemoryError e) {
e.PrintStackTrace ();
return bitmap;
} catch (System.Exception e) {
System.Diagnostics.Debug.WriteLine (e.Message);
return bitmap;
}
}

如果有人能解决我的问题,请告诉我。

最佳答案

我不确定我是否错过了它,但感觉矩阵没有分配给任何图像?因此,您对矩阵所做的任何更改都将永远看不到。

我的实现方式是:

    Matrix mtx = new Matrix();
ExifInterface exif = new ExifInterface(fileName);
string orientation = exif.GetAttribute(ExifInterface.TagOrientation);

switch (orientation)
{
case "6": // portrait
mtx.PreRotate(90);
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
mtx.Dispose();
mtx = null;
break;
case "1": // landscape
break;
case "8": // Selfie ORIENTATION_ROTATE_270 - might need to flip horizontally too...
mtx.PreRotate(270);
resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
mtx.Dispose();
mtx = null;
break;
}

这是矩阵 .preRotate 分配给 Bitmap.CreateBitmap(),然后返回这个调整大小的位图。我希望这会有所帮助:)

关于android - 压缩后 Xamarin Forms Android 图像方向发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32558943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com