在android上模糊图像

时间:2011-07-19 07:58:56

标签: android graphics image-processing

我希望模糊图像,我用过:

    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;
}
某些图像的方法很好,对于某些图像。效果不好,看起来很粗鲁,你能给我一些建议吗,我已经写了http://www.jhlabs.com/ip/blurring.htmlhttp://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Image-enhance.doc.html#51172,但我找不到好的安卓方法

1 个答案:

答案 0 :(得分:2)

您的中间图像是RGB565。这意味着16位,R为5位,G为6,B为5。如果原始图像是RGB888,则模糊后看起来会很糟糕。你能不能用与原版相同的格式创建中间图像?

另外,如果原始图像是RGB888,它是如何转换为565的?您的代码有:

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

看起来没有受控转换。

您的模糊功能适用于ARGB图像。由于您的硬编码为565,效率低下,如果原始图像是ARGB8888,那么转换为RGB565将会对alpha通道做出奇怪的事情。

如果这个答案不够,那么看一下这段代码创建的“坏”图像会很有帮助。