pengwang
pengwang

Reputation: 19946

blur a image at android

i want blur a image,i used :

    public Bitmap mohu(Bitmap bmpOriginal,int hRadius,int vRadius) {
    int width, height, r,g, b, c,a, gry,c1,a1,r1,g1,b1,red,green,blue; 
    height = bmpOriginal.getHeight(); 
    width = bmpOriginal.getWidth(); 


     int iterations = 5;
     int[] inPixels = new int[width*height];
     int[] outPixels = new int[width*height];
       Bitmap bmpSephia = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bmpSephia);
        Paint paint = new Paint();
       int i=0;
       canvas.drawBitmap(bmpOriginal, 0, 0, null);
       for(int x=0; x < width; x++)  {
            for(int y=0; y< height; y++) {
                c = bmpOriginal.getPixel(x, y);
                inPixels[i]=c;
                i++;
            }
        }      

        for (int k = 0; k< iterations; k++ ) {
            blur( inPixels, outPixels, width, height, hRadius );
            blur( outPixels, inPixels, height, width,  vRadius );
        }
        bmpSephia.setPixels(outPixels, 0, width, 0, 0, width, height);
        return bmpSephia;
    }
public static void blur( int[] in, int[] out, int width, int height, int radius ) {
    int widthMinus1 = width-1;
    int tableSize = 2*radius+1;
    int divide[] = new int[256*tableSize];

    for ( int i = 0; i < 256*tableSize; i++ )
        divide[i] = i/tableSize;

    int inIndex = 0;

    for ( int y = 0; y < height; y++ ) {
        int outIndex = y;
        int ta = 0, tr = 0, tg = 0, tb = 0;

        for ( int i = -radius; i <= radius; i++ ) {
            int rgb = in[inIndex + ImageMath.clamp(i, 0, width-1)];
            ta += (rgb >> 24) & 0xff;
            tr += (rgb >> 16) & 0xff;
            tg += (rgb >> 8) & 0xff;
            tb += rgb & 0xff;
        }

        for ( int x = 0; x < width; x++ ) {
            out[ outIndex ] = (divide[ta] << 24) | (divide[tr] << 16) | (divide[tg] << 8) | divide[tb];

            int i1 = x+radius+1;
            if ( i1 > widthMinus1 )
                i1 = widthMinus1;
            int i2 = x-radius;
            if ( i2 < 0 )
                i2 = 0;
            int rgb1 = in[inIndex+i1];
            int rgb2 = in[inIndex+i2];

            ta += ((rgb1 >> 24) & 0xff)-((rgb2 >> 24) & 0xff);
            tr += ((rgb1 & 0xff0000)-(rgb2 & 0xff0000)) >> 16;
            tg += ((rgb1 & 0xff00)-(rgb2 & 0xff00)) >> 8;
            tb += (rgb1 & 0xff)-(rgb2 & 0xff);
            outIndex += height;
        }
        inIndex += width;

    }
}
 ///
  public static float clamp(float x, float a, float b) {
    return (x < a) ? a : (x > b) ? b : x;
}

the method for some images is good,for some images .the effect not well,ot looks very rude,can you give me some advice, i have readed http://www.jhlabs.com/ip/blurring.html and http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Image-enhance.doc.html#51172 ,but i cannot find good method for android

Upvotes: 3

Views: 2233

Answers (1)

koan
koan

Reputation: 3686

Your intermediate image is RGB565. That means 16 bits, 5 bits for R, 6 for G and 5 for B. If the original image is RGB888 then it would look bad after blurring. Can you not create an intermediate image in the same format as the original ?

Also, if the original image is RGB888, how is it converted to 565 ? Your code has:

c = bmpOriginal.getPixel(x, y);
inPixels[i]=c;

It looks like there is no controlled conversion.

Your blur function is for an ARGB image. As well as being inefficient since you've hard coded for 565, if the original image is ARGB8888 then your conversion to RGB565 is going to do strange things with the alpha channel.

If this answer is not enough, it would be helpful to see some "bad" images that this code creates.

Upvotes: 2

Related Questions