What does the filter parameter to createScaledBitmap do?

AndroidScaling

Android Problem Overview


The declaration of android.graphics.Bitmap.createScaledBitmap is

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?

Android Solutions


Solution 1 - Android

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.

Passing filter = false will result in a blocky, pixellated image.

Passing filter = true will give you smoother edges.

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

Solution 2 - Android

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.html for details.)

Solution 3 - Android

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

image filters compared

There is a more general question about whether and how to filter on 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.

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).

Solution 4 - Android

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

Solution 5 - Android

During my research about used algorithm when filter=false (I needed to be 100% sure what output is produced) as I was using the same tensorflow lite model on android and python and found that it uses NEAREST (tested on android 7.1) when filter=false. I've compared embeddings which were identical on android and on python when used PIL resample=Image.NEAREST

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionclaheyView Question on Stackoverflow
Solution 1 - AndroidteedyayView Answer on Stackoverflow
Solution 2 - AndroidbeekeeperView Answer on Stackoverflow
Solution 3 - AndroidMaartenView Answer on Stackoverflow
Solution 4 - AndroidKaranView Answer on Stackoverflow
Solution 5 - AndroidKrzysztof KView Answer on Stackoverflow