Blur image in android

I want to blur the 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 is not very good, it looks very rude, can you give me some tips, I read 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 can not find a good method for android

+3
source share
1 answer

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

Also, if the original image is RGB888, then how is it converted to 565? In your code:

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

There seems to be no controlled transformation.

The blur function is for ARGB image. Besides the fact that you are inefficient, since you are hard-coded for 565, if the source image is ARGB8888, then your conversion to RGB565 will do strange things with the alpha channel.

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

+2
source

Source: https://habr.com/ru/post/906817/


All Articles