如何解决转换为位图后,图像变得太像素化-Xamarin.forms
我正在尝试在我的 Xamarin.Forms Android 应用中查找相机拍摄的图像的旋转度。目的是使图像始终为纵向。它工作正常。旋转后,图像将转换为位图。
我面临的问题是在 Vivo 设备中:图像变得太像素化。它变成7KB左右的大小。我希望减小图像尺寸,但要保持质量。
我应该做些什么改变,以保持尺寸更小而不损失质量。
旋转检查并转换为位图
Android.Graphics.Bitmap loadAndResizeBitmap(string filePath)
{
Android.Graphics.BitmapFactory.Options options = new Android.Graphics.BitmapFactory.Options { InJustDecodeBounds = true };
Android.Graphics.BitmapFactory.DecodeFile(filePath,options);
int required_SIZE = 100;
int width_tmp = options.OutWidth,height_tmp = options.OutHeight;
int scale = 4;
while (true)
{
if (width_tmp / 2 < required_SIZE || height_tmp / 2 < required_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
options.InSampleSize = scale;
options.InJustDecodeBounds = false;
Android.Graphics.Bitmap resizedBitmap = Android.Graphics.BitmapFactory.DecodeFile(filePath,options);
ExifInterface exif = null;
try
{
exif = new ExifInterface(filePath);
string orientation = exif.GetAttribute(ExifInterface.TagOrientation);
Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
switch (orientation)
{
case "1": // landscape
break;
case "3":
matrix.PreRotate(180);
resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,resizedBitmap.Width,resizedBitmap.Height,matrix,false);
matrix.dispose();
matrix = null;
break;
case "4":
matrix.PreRotate(180);
resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
matrix.dispose();
matrix = null;
break;
case "5":
matrix.PreRotate(90);
resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
matrix.dispose();
matrix = null;
break;
case "6": // portrait
matrix.PreRotate(90);
resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
matrix.dispose();
matrix = null;
break;
case "7":
matrix.PreRotate(-90);
resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
matrix.dispose();
matrix = null;
break;
case "8":
matrix.PreRotate(-90);
resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
matrix.dispose();
matrix = null;
break;
}
return resizedBitmap;
}
}
解决方法
对于options.InSampleSize
,如果将其设置为值> 1,则请求解码器对原始图像进行二次采样,返回较小的图像以节省内存。这样您将得到一个7KB大小的图像。
https://developer.android.com/reference/android/graphics/BitmapFactory.Options#inSampleSize
您可以尝试使用matrix.postScale
以指定比例调整矩阵大小。 https://developer.android.com/reference/android/graphics/Matrix#preScale(float,%20float)
public Bitmap getResizedBitmap(Bitmap bm,int newHeight,int newWidth)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth,scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm,width,height,matrix,false);
return resizedBitmap;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。