image blur in android (getting error)

╄→гoц情女王★ 提交于 2019-12-02 17:40:26

问题


hi i m trying to implement image blur in android i fond so many example for it but i m trying with this code

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;
    }

but whent i try to run this is display FATAL EXCEPTION like

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)

i don't know what wrong or what i miss, can any one help me with this i referred this Example


回答1:


In docs you can see that setPixel throws IllegalStateException when Bitmap is immutable. You need a mutable bitmap. Simple way (one of many) to obtain one is:

bitmap = bitmap.copy(bitmap.getConfig(), true);



回答2:


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

put the above code like this

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

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

I hope it will work fine.....




回答3:


try this

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;
}


来源:https://stackoverflow.com/questions/13066842/image-blur-in-android-getting-error

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