Search code examples
androidpixeleffects

How to pixelize a bitmap in android


I have been searching but i did not find a question about how to pixelize a bitmap in android

Example

A

Note that i dont mean blur


Solution

  • You could just try and resize that image to a smaller version (and then back up if you need it at the same size as the original for some reason)

    {
        Bitmap original = ...;
        //Calculate proportional size, or make the method accept just a factor of scale.
        Bitmap small = getResigetResizedBitmap(original, smallWidth, smallHeight);
        Bitmap pixelated = getResigetResizedBitmap(small, normalWidth, normalHeight);
       //Recycle small, recycle original if no longer needed.
    }
    
    public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
        int width = bm.getWidth();
        int height = bm.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 resizedBitmap = Bitmap.createBitmap(
            bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    }
    

    The code is from here