嗨,我试图在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)
我不知道错过了什么或错过了什么, 任何人都可以帮我这个我提到这个Example
答案 0 :(得分:3)
在docs中,当setPixel
不可变时,您会看到IllegalStateException
抛出Bitmap
。
你需要一个可变的位图。获得一个的简单方法(众多之一)是:
bitmap = bitmap.copy(bitmap.getConfig(), true);
答案 1 :(得分: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
}
我希望它能正常工作.....
答案 2 :(得分: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;
}