简体   繁体   English

android中的图像模糊(出现错误)

[英]image blur in android (getting error)

hi im trying to implement image blur in android i fond so many example for it but im trying with this code 您好,我尝试在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;
    }

but whent i try to run this is display FATAL EXCEPTION like 但是当我尝试运行时这会显示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)

i don't know what wrong or what i miss, can any one help me with this i referred this Example 我不知道自己做错了什么或者我错过了什么,任何一个可以帮我这个我提到这个例子

In docs you can see that setPixel throws IllegalStateException when Bitmap is immutable. 文档中,您可以看到当Bitmap不可变时, setPixel会引发IllegalStateException You need a mutable bitmap. 您需要一个可变的位图。 Simple way (one of many) to obtain one is: 获得一个的简单方法(众多方法之一)是:

bitmap = bitmap.copy(bitmap.getConfig(), true);
 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..... 我希望它能正常工作.....

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM