blur a image at android

。_饼干妹妹 提交于 2019-12-12 07:27:42

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6743969/blur-a-image-at-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!