Android examples for Graphics:Bitmap Blur
get Blurred Bitmap
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Shader.TileMode; import android.graphics.YuvImage; import android.graphics.drawable.Drawable; public class Main{ /*from w w w . j a va 2s . co m*/ private static final String TAG = ImgUtil.class.getSimpleName(); public static Bitmap getBlurredBitmap(Bitmap src, int ratio, boolean recycleSrc) { Bitmap aged = blurBitmap(src.copy(Config.RGB_565, true), ratio); if (recycleSrc && aged != src) { src.recycle(); } return aged; } public static Bitmap blurBitmap(Bitmap src, int ratio) { if (!src.isMutable()) { LogUtil.w(TAG, "blurImage", "source bitmap is not mutable"); return src; } // int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 }; int width = src.getWidth(); int height = src.getHeight(); int pixR = 0; int pixG = 0; int pixB = 0; int pixColor = 0; int newR = 0; int newG = 0; int newB = 0; int idx = 0; int[] pixels = new int[width * height]; src.getPixels(pixels, 0, width, 0, 0, width, height); for (int i = 1, length = height - 1; i < length; i++) { for (int k = 1, len = width - 1; k < len; k++) { idx = 0; for (int m = -1; m <= 1; m++) { for (int n = -1; n <= 1; n++) { pixColor = pixels[(i + m) * width + k + n]; pixR = Color.red(pixColor); pixG = Color.green(pixColor); pixB = Color.blue(pixColor); newR = newR + (int) (pixR * gauss[idx]); newG = newG + (int) (pixG * gauss[idx]); newB = newB + (int) (pixB * gauss[idx]); idx++; } } newR /= ratio; newG /= ratio; newB /= ratio; newR = Math.min(255, Math.max(0, newR)); newG = Math.min(255, Math.max(0, newG)); newB = Math.min(255, Math.max(0, newB)); pixels[i * width + k] = Color.argb(255, newR, newG, newB); newR = 0; newG = 0; newB = 0; } } src.setPixels(pixels, 0, width, 0, 0, width, height); return src; } }