3

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

3
  • 1
    Have you seen this ? stackoverflow.com/questions/4276967/… Commented Jul 19, 2011 at 8:10
  • yes,i read it ,but i donot know how to modify it Commented Jul 19, 2011 at 8:27
  • @pengwang can you please tell me where i can find "ImageMath" . Commented Sep 24, 2014 at 8:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your answer,i think you are right,can you give a other method so that it can deal with kinds of images?
I don't know Android APIs but I think that this code can be improved. For example, change to Bitmap bmpSephia = Bitmap.createBitmap(width, height, bmpOriginal.GetConfig()); or something like that. Also, consider whether you want to blur the alpha channel or not, if it exists.
thank you,Config only four,i test every one,but the result not well,i also thank you.i need to find other method,thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.