0

嗨,我正在尝试在 android 中实现图像模糊,我喜欢它的很多示例,但我正在尝试使用此代码

private Bitmap getBlurBitmap(Bitmap bitmap, int radius)
    {
        int w,h,total;

        if(bitmap == null){
            System.err.println(" <== BitMap is Null ==> ");
            return null;
        }

        w=bitmap.getWidth();
        h=bitmap.getHeight();

         for (int y = 0; y < h; ++y) {
             for (int x = 0; x < w; ++x) {
                 total = 0;
                 for (int ky = -radius; ky <= radius; ++ky){
                     for (int kx = -radius; kx <= radius; ++kx){
                        // total += source(x + kx, y + ky);

                         int _tempx=x + kx;
                         int _tempy=y + ky;

                         if(_tempx < 0 )
                             _tempx=0;
                         if(_tempx > w )
                            _tempx = w - kx;

                         if(_tempy < 0 )
                             _tempy=0;
                         if(_tempy > h )
                             _tempy = h - ky;

                         total += bitmap.getPixel(_tempx, _tempy);
                     }
                 }
                 bitmap.setPixel(x, y,(int)( total / (radius * 2 + 1) ^ 2));
             }
         }
        return bitmap;
    }

但是当我尝试运行时,它的显示FATAL EXCEPTION就像

03-18 04:41:54.296: E/AndroidRuntime(16347): FATAL EXCEPTION: main
03-18 04:41:54.296: E/AndroidRuntime(16347): java.lang.IllegalStateException
03-18 04:41:54.296: E/AndroidRuntime(16347):    at android.graphics.Bitmap.setPixel(Bitmap.java:856)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at com.org.PhotoAppSimpleBlureActivity.getBlurBitmap(PhotoAppSimpleBlureActivity.java:81)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at com.org.PhotoAppSimpleBlureActivity.onClick(PhotoAppSimpleBlureActivity.java:93)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at android.view.View.performClick(View.java:2485)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at android.view.View$PerformClick.run(View.java:9080)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at android.os.Handler.handleCallback(Handler.java:587)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at android.os.Looper.loop(Looper.java:130)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at java.lang.reflect.Method.invoke(Method.java:507)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
03-18 04:41:54.296: E/AndroidRuntime(16347):    at dalvik.system.NativeStart.main(Native Method)

我不知道有什么问题或我想念什么,有人可以帮我解决这个问题吗?我参考了这个例子

4

3 回答 3

3

文档中,您可以看到setPixelthrows IllegalStateExceptionwhenBitmap是不可变的。您需要一个可变位图。获得一个的简单方法(许多之一)是:

bitmap = bitmap.copy(bitmap.getConfig(), true);
于 2012-10-25T10:52:55.157 回答
0
 bitmap.setPixel(x, y,(int)( total / (radius * 2 + 1) ^ 2));

把上面的代码这样

try
        {
             bitmap.setPixel(x, y,(int)( total / (radius * 2 + 1) ^ 2));
        }

        catch (IllegalStateException e) {
            // TODO: handle exception
        }
        catch (Exception e) {
            // TODO: handle exception
        }

我希望它会正常工作......

于 2012-10-25T10:49:49.923 回答
0

试试这个

private Bitmap getBlurBitmap(Bitmap src) {

    final int widthKernal = 5;
    final int heightKernal = 5;

    int w = src.getWidth();
    int h = src.getHeight();

    Bitmap blurBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {

            int r = 0;
            int g = 0;
            int b = 0;
            int a = 0;

            for (int xk = 0; xk < widthKernal; xk++) {
                for (int yk = 0; yk < heightKernal; yk++) {
                    int px = x + xk - 2;
                    int py = y + yk - 2;

                    if (px < 0) {
                        px = 0;
                    } else if (px >= w) {
                        px = w - 1;
                    }

                    if (py < 0) {
                        py = 0;
                    } else if (py >= h) {
                        py = h - 1;
                    }
                    int intColor = src.getPixel(px, py);
                    r += Color.red(intColor);
                    g += Color.green(intColor);
                    b += Color.blue(intColor);
                    a += Color.alpha(intColor);
                }
            }
            blurBitmap.setPixel(x, y,
                    Color.argb(a / 25, r / 25, g / 25, b / 25));
        }
    }
    return blurBitmap;
}
于 2014-07-31T09:57:46.587 回答