我有一个位图采取的Base64字符串从我的远程数据库,(encodedImage是字符串表示图像与Base64):
profileImage = (ImageView)findViewById(R.id.profileImage);
byte[] imageAsBytes=null;
try {
imageAsBytes = Base64.decode(encodedImage.getBytes());
} catch (IOException e) {e.printStackTrace();}
profileImage.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
profileImage是我的ImageView
但在显示在布局的ImageView中之前,我需要调整图像的大小。我得把它调整到120x120。
有人能告诉我调整大小的代码吗?
我发现的例子不能应用于base64字符串获得位图。
public static Bitmap resizeBitmapByScale(
Bitmap bitmap, float scale, boolean recycle) {
int width = Math.round(bitmap.getWidth() * scale);
int height = Math.round(bitmap.getHeight() * scale);
if (width == bitmap.getWidth()
&& height == bitmap.getHeight()) return bitmap;
Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
Canvas canvas = new Canvas(target);
canvas.scale(scale, scale);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
canvas.drawBitmap(bitmap, 0, 0, paint);
if (recycle) bitmap.recycle();
return target;
}
private static Bitmap.Config getConfig(Bitmap bitmap) {
Bitmap.Config config = bitmap.getConfig();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
return config;
}
虽然前面的答案确实缩放了图像并考虑了纵横比,但重新采样本身应该这样做,这样就不会出现混叠。注意尺度是一个正确修正参数的问题。有许多关于标准缩放调用输出图像质量的评论。为了保持图像的质量,应该使用标准调用:
位图resizedBitmap =位图。createScaledBitmap(originalBitmap, newWidth, newHeight, true);
最后一个参数设置为true,因为它将对重采样进行双线性过滤,以防止混叠。在这里阅读更多关于别名的信息:https://en.wikipedia.org/wiki/Aliasing
来自android文档:
https://developer.android.com/reference/android/graphics/Bitmap createScaledBitmap (android.graphics.Bitmap % 20 int, int % 20, % 20布尔)
public static Bitmap createScaledBitmap (Bitmap src,
int dstWidth,
int dstHeight,
boolean filter)
filter: boolean,当缩放位图时是否应该使用双线性过滤。如果这是真的,那么在缩放时将使用双线性滤波,它以更差的性能为代价,具有更好的图像质量。如果这是假的,那么使用最近邻缩放,这将有较差的图像质量,但更快。推荐的默认值是将filter设置为'true',因为双线性过滤的成本通常是最小的,并且图像质量的改善是显著的。
public static Bitmap resizeBitmapByScale(
Bitmap bitmap, float scale, boolean recycle) {
int width = Math.round(bitmap.getWidth() * scale);
int height = Math.round(bitmap.getHeight() * scale);
if (width == bitmap.getWidth()
&& height == bitmap.getHeight()) return bitmap;
Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
Canvas canvas = new Canvas(target);
canvas.scale(scale, scale);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
canvas.drawBitmap(bitmap, 0, 0, paint);
if (recycle) bitmap.recycle();
return target;
}
private static Bitmap.Config getConfig(Bitmap bitmap) {
Bitmap.Config config = bitmap.getConfig();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
return config;
}
虽然接受的答案是正确的,但它不会通过保持相同的纵横比来调整位图的大小。如果你正在寻找一种方法来调整位图的大小保持相同的纵横比,你可以使用下面的实用函数。该功能的使用细节和解释在此链接中。
public static Bitmap resizeBitmap(Bitmap source, int maxLength) {
try {
if (source.getHeight() >= source.getWidth()) {
int targetHeight = maxLength;
if (source.getHeight() <= targetHeight) { // if image already smaller than the required height
return source;
}
double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
int targetWidth = (int) (targetHeight * aspectRatio);
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
}
return result;
} else {
int targetWidth = maxLength;
if (source.getWidth() <= targetWidth) { // if image already smaller than the required height
return source;
}
double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
int targetHeight = (int) (targetWidth * aspectRatio);
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
}
return result;
}
}
catch (Exception e)
{
return source;
}
}