Android 调整位图大小而不丢失质量
我正在将图片从sd卡加载到位图。文件是jpeg格式的。我是这样做的:Android 调整位图大小而不丢失质量,android,Android,我正在将图片从sd卡加载到位图。文件是jpeg格式的。我是这样做的: Bitmap b = BitmapFactory.decodeFile("/sdcard/DinEgen/"+name.get(position), null); Bitmap img = Bitmap.createScaledBitmap( b, width, height, true); b.recycle(); i.setImageBitmap(img); 但当我缩放
Bitmap b = BitmapFactory.decodeFile("/sdcard/DinEgen/"+name.get(position), null);
Bitmap img = Bitmap.createScaledBitmap( b, width, height, true);
b.recycle();
i.setImageBitmap(img);
但当我缩放位图时,我会失去质量。当我只使用“位图b”时,质量很好。因此,当我使用
createScaledBitmap
时,我会失去质感。如何将位图b从hight
和width
变量缩放到大小?我已经为它编写了一个类
public class BitmapSizeHelper
{
public static enum ScalingLogic
{
CROP, FIT
}
public static Bitmap getBitmapFromResources(Resources res, int resId, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, dstHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options);
return unscaledBitmap;
}
public static Bitmap getBitmapFromPath(int targetW, int targetH, String photopath, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photopath, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, dstHeight, scalingLogic);
Bitmap bitmap = BitmapFactory.decodeFile(photopath, options);
return bitmap;
}
public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
if (scalingLogic == ScalingLogic.FIT)
{
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect)
{
return srcWidth / dstWidth;
}
else
{
return srcHeight / dstHeight;
}
}
else
{
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect)
{
return srcHeight / dstHeight;
}
else
{
return srcWidth / dstWidth;
}
}
}
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
if (scalingLogic == ScalingLogic.CROP)
{
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect)
{
final int srcRectWidth = (int) (srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
}
else
{
final int srcRectHeight = (int) (srcWidth / dstAspect);
final int scrRectTop = (int) (srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
}
}
else
{
return new Rect(0, 0, srcWidth, srcHeight);
}
}
public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
if (scalingLogic == ScalingLogic.FIT)
{
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect)
{
return new Rect(0, 0, dstWidth, (int) (dstWidth / srcAspect));
}
else
{
return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight);
}
}
else
{
return new Rect(0, 0, dstWidth, dstHeight);
}
}
}`
您无法调整图像的大小而不降低图像质量,除非它们是。不过,可能有一些方法可以将质量损失降至最低。在解码JPEG时,请使用
位图选项
明确指示不要缩放等
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap b = BitmapFactory.decodeFile("/sdcard/DinEgen/"+name.get(position), options);
图像质量失真/不稳定的原因是图像的纵横比没有根据源宽度和高度以及目标宽度和高度来保持。因此,您需要决定使用裁剪或拟合的缩放逻辑。只需感谢。。。你救了我一天。。。真棒,谢谢你。工作很有魅力…………谢谢