Android createScaledBitmap 的过滤器参数有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2895065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:46:29  来源:igfitidea点击:

What does the filter parameter to createScaledBitmap do?

androidscaling

提问by clahey

The declaration of android.graphics.Bitmap.createScaledBitmapis

的声明android.graphics.Bitmap.createScaledBitmap

public static Bitmap createScaledBitmap
  (Bitmap src, int dstWidth, int dstHeight, boolean filter)

However, the documentation doesn't explain any of the parameters. All of them are pretty obvious except for boolean filter. Does anyone know what it does?

但是,文档没有解释任何参数。除了boolean filter. 有谁知道它是做什么的?

采纳答案by beekeeper

A quick dig through the SKIA source-code indicates that (at least by default) the FILTER flag causes it to do a straightforward bilinear interpolation. Check Wikipedia or your favorite graphics reference to see what the expected consequences are. Traditionally, you want to do bilinear or bicubic interpolation when upsizing images, and area averaging when downsizing images. I get the impression (though I'm glad to be corrected) that Android/Skia does simple subsampling when downsizing without filtering, so you are likely to get better results from filtering even when downsizing. (There's an alternate method for getting high quality downsizing with interpolation, involving doing a series of 50% scale reductions. See http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.htmlfor details.)

快速浏览 SKIA 源代码表明(至少在默认情况下)FILTER 标志使其执行简单的双线性插值。检查维基百科或您最喜欢的图形参考,看看预期的结果是什么。传统上,您希望在放大图像时进行双线性或双三次插值,并在缩小图像时进行区域平均。我的印象是(尽管我很高兴得到纠正)Android/Skia 在不进行过滤的情况下缩小规模时会进行简单的子采样,因此即使在缩小规模时,您也可能通过过滤获得更好的结果。(有一种通过插值获得高质量缩小规模的替代方法,包括进行一系列 50% 的规模缩小。参见http://today.java.net/pub/a/today/2007/04/03/perils-of-图像-getscaledinstance.html详情。)

回答by teedyay

To expand on Karan's answer: As a general rule you won't see any difference if you're scaling your image down, but you will if you're scaling it up.

为了扩大Karan的答案:作为一般规则,你将不会看到任何区别,如果你缩放图像下来,但是你会如果你缩放来

Passing filter = falsewill result in a blocky, pixellated image.

通过filter = false将导致块状像素化图像。

Passing filter = truewill give you smoother edges.

通过filter = true会给你更平滑的边缘。

However, as EIYeante pointed out in the comments, you might still see a difference. This is their example image.

但是,正如 EIYeante 在评论中指出的那样,您可能仍然会看到差异。这是他们的示例图像。

回答by Karan

Filter will set the FILTER_BITMAP_FLAGfor painting which affects the sampling of bitmaps when they are transformed based on the value that you provide.

过滤器将为绘画设置FILTER_BITMAP_FLAG,这会在根据您提供的值转换位图时影响位图的采样。

回答by Maarten

A bit late to the party, but I thought some sample images might clarify the issue.

聚会有点晚了,但我认为一些示例图像可能会澄清问题。

Android's API does not specify what kind of filter would be applied, so I guess the question is: do you want your pixels to remain as they are (as you would want in 8-bit art) or is it okay to apply a transformation to make the image more palatable (as you would want in photographs).

Android 的 API 没有指定将应用哪种过滤器,所以我想问题是:您希望像素保持原样(就像您在 8 位艺术中希望的那样)还是可以将转换应用于使图像更可口(就像您在照片中想要的那样)。

There is a more general question about whether and how to filter on superuser.

关于是否以及如何过滤superuser有一个更普遍的问题。

Says Jeff Atwood:

杰夫阿特伍德说:

In general you want a mild sharpening effect when making a larger image into a smaller one, and a mild blurring effect when making a smaller image into a larger one.

一般来说,将较大的图像制作成较小的图像时,您需要温和的锐化效果,而将较小的图像制作成较大的图像时,您需要温和的模糊效果。

image filters compared

图像过滤器比较