fast blur a Bitmap - Android android.graphics

Android examples for android.graphics:Bitmap Operation

Description

fast blur a Bitmap

Demo Code

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

public class Main {

  public static final float DEFAULT_BLUR_RADIUS = 12f;

  public static Bitmap fastblur(Context context, Bitmap sentBitmap) {
    return fastblur(context, sentBitmap, DEFAULT_BLUR_RADIUS);
  }// www . java 2 s .co m

  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  public static Bitmap fastblur(Context context, Bitmap sentBitmap, float radius) {

    if (Build.VERSION.SDK_INT > 16 && (!Build.MODEL.contains("google_sdk") && !Build.MODEL.contains("Emulator")
        && !Build.MODEL.contains("Android SDK") && !Build.FINGERPRINT.contains("vbox86p"))) {
      Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

      final RenderScript rs = RenderScript.create(context);
      final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE,
          Allocation.USAGE_SCRIPT);
      final Allocation output = Allocation.createTyped(rs, input.getType());
      final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
      script.setRadius(radius /* e.g. 3.f */);
      script.setInput(input);
      script.forEach(output);
      output.copyTo(bitmap);
      return bitmap;
    }

    radius *= 0.1;
    return blurfast(sentBitmap, (int) radius);
  }

  static Bitmap blurfast(Bitmap bmp, int radius) {
    try {
      Bitmap bitmap = bmp.copy(bmp.getConfig(), true);
      int w = bmp.getWidth();
      int h = bmp.getHeight();
      int[] pix = new int[w * h];
      bmp.getPixels(pix, 0, w, 0, 0, w, h);

      for (int r = radius; r >= 1; r /= 2) {
        for (int i = r; i < h - r; i++) {
          for (int j = r; j < w - r; j++) {
            int tl = pix[(i - r) * w + j - r];
            int tr = pix[(i - r) * w + j + r];
            int tc = pix[(i - r) * w + j];
            int bl = pix[(i + r) * w + j - r];
            int br = pix[(i + r) * w + j + r];
            int bc = pix[(i + r) * w + j];
            int cl = pix[i * w + j - r];
            int cr = pix[i * w + j + r];

            pix[(i * w) + j] = 0xFF000000
                | (((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF) + (bl & 0xFF) + (br & 0xFF) + (bc & 0xFF) + (cl & 0xFF)
                    + (cr & 0xFF)) >> 3) & 0xFF
                | (((tl & 0xFF00) + (tr & 0xFF00) + (tc & 0xFF00) + (bl & 0xFF00) + (br & 0xFF00) + (bc & 0xFF00)
                    + (cl & 0xFF00) + (cr & 0xFF00)) >> 3) & 0xFF00
                | (((tl & 0xFF0000) + (tr & 0xFF0000) + (tc & 0xFF0000) + (bl & 0xFF0000) + (br & 0xFF0000)
                    + (bc & 0xFF0000) + (cl & 0xFF0000) + (cr & 0xFF0000)) >> 3) & 0xFF0000;
          }
        }
      }
      bitmap.setPixels(pix, 0, w, 0, 0, w, h);
      return bitmap;
    } catch (Exception e) {
      return bmp;
    }
  }


}

Related Tutorials