3

I am using the following code to blur an image in android, but it does not work. The final image that I get is with very distorted color and not a blur which I want. What am I doing wrong?

public Bitmap blurBitmap(Bitmap bmpOriginal)
{        
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();    

    Bitmap bmpBlurred = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    for (int i = 1; i < width - 1; i++) {
        for (int j = 1; j < height - 1; j++) {
            int color = (int) getAverageOfPixel(bmpOriginal, i, j);
            bmpBlurred.setPixel(i, j, Color.argb(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color)));
        }
    }
    return bmpBlurred;
}

private double getAverageOfPixel(Bitmap bitmap, int i, int j) {
     return (
    bitmap.getPixel(i-1, j-1) + bitmap.getPixel(i-1, j) + bitmap.getPixel(i-1, j+1) +
    bitmap.getPixel(i, j-1) + bitmap.getPixel(i, j) + bitmap.getPixel(i, j+1) + 
    bitmap.getPixel(i+1, j-1) + bitmap.getPixel(i+1, j) + bitmap.getPixel(i+1, j+1)) / 9;
}   
n1kh1lp
  • 14,247
  • 6
  • 29
  • 28
  • can you give your resolve method, i meet the same question,thank you.http://stackoverflow.com/questions/6728860/blur-and-emboss-an-image/6744104#6744104 – pengwang Jul 19 '11 at 08:21

1 Answers1

2

Your problem is that you are combining all the colour channels at once and they all spill into each other. You should apply your blur function to the red, green, and blue channels separately.

Might it be easier to create a SurfaceView and use the FX_SURFACE_BLUR flag?

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • thanks a lot! Separating color channels worked! But the image size grows by ten times after blurring. Do you know how I can compress it? I am converting from PNG (100 KB) to JPEG which becomes 950KB. And, I am setting the quality to zero in the above code: bmpBlurred.compress(Bitmap.CompressFormat.JPEG, 0, outputStream); – n1kh1lp Nov 26 '10 at 04:05
  • Also, to answer your second point, I'm trying to write my own algorithm. Thanks for the help! – n1kh1lp Nov 26 '10 at 04:47
  • What size is the image? 950KB does seem excessive... a 600x400 pixel image would need roughly that much space completely decompressed. – Reuben Scratton Nov 26 '10 at 09:28
  • Original image is 100 KB (1100 x 900). – n1kh1lp Nov 26 '10 at 09:58
  • I fixed it. Please ignore the question. Thanks a lot for your help! – n1kh1lp Nov 26 '10 at 10:22
  • 1
    @zero1 maybe you could show your solution to your problem, it might help others who encounters the same problem with yours. – blitzen12 Mar 25 '13 at 07:01
  • 1
    FX_SURFACE_BLUR was dropped in API14 http://developer.android.com/sdk/api_diff/14/changes/android.view.Surface.html#android.view.Surface.FX_SURFACE_BLUR – caller9 Mar 28 '13 at 16:46