Android: Resizing Bitmaps Without Losing Quality


Answer :

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: http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

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


Try below mentioned code for resizing bitmap.

 public Bitmap get_Resized_Bitmap(Bitmap bmp, int newHeight, int newWidth) {         int width = bmp.getWidth();         int height = bmp.getHeight();         float scaleWidth = ((float) newWidth) / width;         float scaleHeight = ((float) newHeight) / height;         // CREATE A MATRIX FOR THE MANIPULATION         Matrix matrix = new Matrix();         // RESIZE THE BIT MAP         matrix.postScale(scaleWidth, scaleHeight);          // "RECREATE" THE NEW BITMAP         Bitmap newBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false);         return newBitmap ;     } 

I used this code to downsize my bitmap, and its quality, well.. , was acceptable.

Hope this helps.


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?