Skip to content Skip to sidebar Skip to footer

Android: Resizing Bitmaps Without Losing Quality

i've really searched through over the entire web before posting. My problem is that i cannot resize bitmap without losing the quality of the image (the quality is really bad and pi

Solution 1:

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/

Solution 2:

Try below mentioned code for resizing bitmap.

public Bitmap get_Resized_Bitmap(Bitmap bmp, int newHeight, int newWidth) {
        intwidth= bmp.getWidth();
        intheight= bmp.getHeight();
        floatscaleWidth= ((float) newWidth) / width;
        floatscaleHeight= ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATIONMatrixmatrix=newMatrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        // "RECREATE" THE NEW BITMAPBitmapnewBitmap= 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.

Solution 3:

Try Bitmap.createScaledBitmap. It also has an option to filter the source.

Solution 4:

the best rescaling method I have come across the method uses createScaledBitmap whereby the height and width are calculated based on the bitmap height and width and a scale ratio hence quality is not lost

public Bitmap resize(Bitmap imaged, int maxWidth, int maxHeight) {
        Bitmapimage= imaged;

        if (maxHeight > 0 && maxWidth > 0) {
            intwidth= image.getWidth();
            intheight= image.getHeight();
            floatratioBitmap= (float) width / (float) height;
            floatratioMax= (float) maxWidth / (float) maxHeight;
            intfinalWidth= maxWidth;
            intfinalHeight= maxHeight;
            if (ratioMax > 1) {
                finalWidth = Math.round(((float) maxHeight * ratioBitmap));
            } else {
                finalHeight = Math.round(((float) maxWidth / ratioBitmap));
            }
            returnimage= Bitmap.createScaledBitmap(image, finalWidth, finalHeight, false);
        }
        return image;
    }

how to use:

BitmapresizedBitmap= resize(scrBitMap,640,640);

Post a Comment for "Android: Resizing Bitmaps Without Losing Quality"