【问题标题】:How to resize image (Bitmap) to a given size? [duplicate]如何将图像(位图)调整为给定大小? [复制]
【发布时间】:2012-01-18 06:21:00
【问题描述】:

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

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

【问题讨论】:

标签: android bitmap


【解决方案1】:
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;
}

【讨论】:

  • 请注意,这种方法也会扩大规模。为了保证图片只会被缩小,检测结果比例是否小于1
  • 我收到一个错误Attempt to invoke virtual method 'void android.graphics.Bitmap.setHasAlpha(boolean)' on a null object reference
  • 这就是我要找的东西,谢谢你在不改变纵横比的情况下完美地缩小和放大位图到所需的大小。
  • 如果你想防止向上扩展,只需在newBitmap对象初始化之前添加if (ratio >= 1.0){ return realImage;}
  • 很好的解决方案...防止失真,这是我所需要的。
【解决方案2】:
 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);

【讨论】:

  • 我不明白为什么这不被接受为答案。无需手动实现,简单易用
  • 最佳答案
  • 最后一个布尔参数是干什么用的?
【解决方案3】:

在 MonoDroid 中是这样 (c#)

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

}

【讨论】:

  • 如果我有很多图像,它们在不同的活动中具有不同的大小。是在代码中调整它们的大小还是以小尺寸和大尺寸导入它们更好。
【解决方案4】:

关于“如何”调整它们的大小的其他答案是正确的,但我也会提出建议,首先获取您感兴趣的分辨率。大多数 Android 设备都提供一系列分辨率,您应该选择一个可以让您感到满意的文件大小。最大的原因是原生 Android 缩放算法(由 Jin35 和 Padma Kumar 详细介绍)产生了相当糟糕的结果。它不会为您提供 Photoshop 质量的大小调整,甚至缩小(更不用说放大了,我知道您不是在问这个问题,但这只是一个初学者)。

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

【讨论】:

    【解决方案5】:

    您可以使用 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;
        }
    

    【讨论】:

    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 2011-10-24
    • 2014-05-24
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2013-08-12
    • 2012-07-28
    相关资源
    最近更新 更多