如何以编程方式将图像(位图)的大小调整为例如 800*480?我在我的应用程序中检索到一张大约 1MB 的图片,我需要将其缩小到 800*480 我已经加载了该图片并对其进行了压缩,但我该如何使其更小:
ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
photo.compress(CompressFormat.JPEG, 25, bos);
最佳答案
Bitmap scaledBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true);
缩小方法:
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
boolean filter) {
float ratio = Math.min(
(float) maxImageSize / realImage.getWidth(),
(float) maxImageSize / realImage.getHeight());
int width = Math.round((float) ratio * realImage.getWidth());
int height = Math.round((float) ratio * realImage.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
height, filter);
return newBitmap;
}
关于android - 如何将图像(位图)调整为给定大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8471226/