214

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?

clahey
  • 4,795
  • 3
  • 27
  • 20

5 Answers5

259

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.

Kjeld Schmidt
  • 771
  • 8
  • 19
teedyay
  • 23,293
  • 19
  • 66
  • 73
  • 6
    @Mirko - nah, it's OK: this should earn me the Populist badge any minute now. :) – teedyay Nov 14 '12 at 14:41
  • @ElYeante - do you have an example? Some input and output images would be great. Thanks. – teedyay Oct 21 '13 at 21:36
  • Wow. I stand corrected. I'm curious as to why this is so apparent with your images, but I've not seen it with mine. Is it because you're downscaling by so much, perhaps? – teedyay Oct 24 '13 at 19:32
  • 2
    this flag is misleading. how can we downsample with smoothing prior to resampling to prevent anti-aliasing? – Sam Jul 10 '14 at 13:50
  • 1
    The link has broken @teedyay – Sagar Feb 25 '18 at 12:49
  • @teedyay You should inline the image because links often die on the internet. – Maarten Feb 18 '20 at 15:53
89

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

beekeeper
  • 2,516
  • 17
  • 13
  • 5
    What does it do if you pass it false? Nearest neighbor? – clahey Nov 05 '10 at 15:34
  • Also, do you know if it's doing area averaging if you pass filter = true? – clahey Nov 05 '10 at 15:36
  • You should add an image to get a feel for what the difference is, instead of referencing external sources. Links die, sources vanish. – Maarten Feb 18 '20 at 15:52
  • Haha, more than years 10 later @clahey: `If this is false then nearest-neighbor scaling is used instead which have worse image quality but is faster. ` is in the javadoc for `createScaledBitmap` – Ben Butterworth Nov 24 '20 at 15:37
4

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

Maarten
  • 6,894
  • 7
  • 55
  • 90
3

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.

Karan
  • 12,724
  • 6
  • 40
  • 33
1

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

Krzysztof K
  • 11
  • 1
  • 1