如何将图像(位图)调整为给定大小? [英] How to resize image (Bitmap) to a given size?

查看:25
本文介绍了如何将图像(位图)调整为给定大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式将图像(位图)调整为例如 800*480?我在我的应用程序中检索到了一张大约 1MB 的图片,我需要将其缩小到 800*480我已经加载了那张图片并对其进行了压缩,但是我该如何使它变小:

How to resize image (Bitmap) to for example 800*480 programatically ? I have retrieved a picture in my app which is ~1MB and I need to scale it down to 800*480 I have loaded that picture and compressed it but how do I do to make it smaller :

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;
}

这篇关于如何将图像(位图)调整为给定大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆