How to draw and scale a bitmap on a canvas using bicubic interpolation in Android?
Asked Answered
W

2

18

I want to draw a bitmap on a canvas with bigger size than it is. I can use canvas.drawBitmap(bitmap, null, destRect, null); but that gives a poor quality, as the result is pixelated, if the source image is sightly smaller than the destination rectangle.

How can i draw my bitmap using bilinear or bicubic resampling?

Any help would be appreciated, thanx.

Whoosh answered 25/3, 2010 at 17:0 Comment(0)
C
38

You need to set the FILTER_BITMAP_FLAG flag in your paint.

canvas.drawBitmap(bitmap, matrix, null); //ugly jaggies unless scale is 1:1, but fast

or

Paint paint = new Paint();
paint.setFilterBitmap();
canvas.drawBitmap(bitmap, matrix, paint); // pretty but slower (not too slow, usually)

The same applies to other (syntactic sugar) forms of the drawBitmap method.

There is no documentation anywhere for most of the Android drawing options and neither is there documentation for the underlying native Skia library that does the rendering. I'd be happier if there were. You can search the Skia source code on Google Code Search. Search for Skia and dig into the raw source; that's the only documentation.

Carrasquillo answered 25/3, 2010 at 19:57 Comment(4)
Had to deal with this now, thanks for pointing out setFilterBitmap! Just to add a little more info from my findings, the interpolation for filter bitmap flag false is nearest neighbor and true is bilinear. Was hoping bicubic was available but doesn't look like it.Lannie
paint.setFilterBitmap(); is not too slow, because most of the device/api level is using gpu to do the interpolation.Collings
Exact same behavior, bit more condensed: canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));Eachern
that was not enough for me, I had to use createScaledBitmap and apply those filters tooConventioner
O
1

FILTER_BITMAP_FLAG doesn't work for downscaling in most of the cases.

Good downscaling algorithm (not nearest neighbor like) consists of just 2 steps (plus calculation of the exact Rect for input/output images crop):

  1. downscale using BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() as close as possible to the resolution that you need but not less than it
  2. get to the exact resolution by downscaling a little bit using Canvas::drawBitmap()

Here is detailed explanation how SonyMobile resolved this task: https://web.archive.org/web/20140228024414/http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

Here is the source code of SonyMobile scale utils: https://web.archive.org/web/20140227233706/http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/

Offence answered 21/4, 2014 at 2:13 Comment(1)
Those links seem to be down, unfortunately.Castleberry

© 2022 - 2024 — McMap. All rights reserved.