Android 如何将图像(位图)调整为给定大小?

Android 如何将图像(位图)调整为给定大小?,android,bitmap,Android,Bitmap,如何以编程方式将图像(位图)调整为例如800*480?我在我的应用程序中检索到一张约1MB的图片,我需要将其缩小到800*480 我已加载该图片并对其进行压缩,但如何使其变小: ByteArrayOutputStream bos = new ByteArrayOutputStream(8192); photo.compress(CompressFormat.JPEG, 25, bos); 您可以通过使用canvas.drawBitmap和提供矩阵来缩放位图,例如: public static

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

ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
photo.compress(CompressFormat.JPEG, 25, bos); 

您可以通过使用canvas.drawBitmap和提供矩阵来缩放位图,例如:

public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
        Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        Matrix m = new Matrix();
        m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
        canvas.drawBitmap(bitmap, m, new Paint());

        return output;
    }
缩小方法:

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缩放算法(由Jin35和Padma Kumar详细介绍)产生了非常糟糕的结果。它不会给你Photoshop质量的尺寸调整,甚至是缩小尺寸(更不用说扩大尺寸了,我知道你不是在问这个问题,但这只是一个开始)

所以,你应该尝试他们的解决方案,如果你对结果感到满意,那就太好了。但如果没有,我会编写一个函数,提供一个您满意的宽度范围,并在设备的可用图片大小数组中查找该尺寸(或任何最接近的尺寸),然后设置并使用它。

在MonoDroid中,以下是方法(c#)

//
///用于调整图像大小的图形支持
/// 
公共静态类图形
{
公共静态位图缩放向下位图(位图原始图像、浮点最大值大小、布尔过滤器)
{
浮动比率=数学最小值((浮动)最大尺寸/原始图像宽度,(浮动)最大尺寸/原始图像高度);
整数宽度=(整数)数学圆整(比率*(浮点)原始图像宽度);
整数高度=(整数)数学圆整(比率*(浮点)原始图像高度);
位图newBitmap=Bitmap.CreateScaledBitmap(原始图像、宽度、高度、过滤器);
返回newBitmap;
}
公共静态位图比例图(位图原始图像、int wantedWidth、int wantedHeight)
{
位图输出=Bitmap.CreateBitmap(wantedWidth、wantedHeight、Bitmap.Config.argb888);
画布=新画布(输出);
矩阵m=新矩阵();
m、 设置刻度((浮动)wantedWidth/originalImage.Width,(浮动)wantedHeight/originalImage.Height);
canvas.DrawBitmap(originalImage,m,new Paint());
返回输出;
}
}
或:


请注意,此方法也将按比例放大。为了确保图像仅被缩小,请检测结果比率是否小于1I获取错误
尝试调用虚拟方法'void android.graphics.Bitmap.setHasAlpha(布尔值)''在一个空对象引用上
这就是我要找的,谢谢你在不改变纵横比的情况下将位图完美地缩小和放大到所需大小。如果你想防止放大,只需在对象初始化之前添加
if(ratio>=1.0){return realmeage;}
。。。防止失真,这正是我所需要的。向绘制添加标志以提高下采样质量:new paint(6)6=paint.FILTER_BITMAP_FLAG | paint.DITHER_FLAG如果我有很多图像,它们在不同的活动中具有不同的大小。我已经在这里回答了这个问题,我不明白为什么这个答案不被接受。无需手动实现,简单易用最佳答案最后一个布尔参数是什么?
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;
}
/// <summary>
/// Graphics support for resizing images
/// </summary>
public static class Graphics
{
    public static Bitmap ScaleDownBitmap(Bitmap originalImage, float maxImageSize, bool filter)
    {
        float ratio = Math.Min((float)maxImageSize / originalImage.Width, (float)maxImageSize / originalImage.Height);
        int width = (int)Math.Round(ratio * (float)originalImage.Width);
        int height =(int) Math.Round(ratio * (float)originalImage.Height);

        Bitmap newBitmap = Bitmap.CreateScaledBitmap(originalImage, width, height, filter);
        return newBitmap;
    }

    public static Bitmap ScaleBitmap(Bitmap originalImage, int wantedWidth, int wantedHeight)
    {
        Bitmap output = Bitmap.CreateBitmap(wantedWidth, wantedHeight, Bitmap.Config.Argb8888);
        Canvas canvas = new Canvas(output);
        Matrix m = new Matrix();
        m.SetScale((float)wantedWidth / originalImage.Width, (float)wantedHeight / originalImage.Height);
        canvas.DrawBitmap(originalImage, m, new Paint());
        return output;
    }

}
 Bitmap yourBitmap;
 Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
 resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);