Esempio n. 1
0
  public static Drawable createBlurredImageFromBitmap(
      Bitmap bitmap, Context context, int inSampleSize) {

    RenderScript rs = RenderScript.create(context);
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = inSampleSize;

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imageInByte = stream.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
    Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options);

    final android.support.v8.renderscript.Allocation input =
        android.support.v8.renderscript.Allocation.createFromBitmap(rs, blurTemplate);
    final android.support.v8.renderscript.Allocation output =
        android.support.v8.renderscript.Allocation.createTyped(rs, input.getType());
    final android.support.v8.renderscript.ScriptIntrinsicBlur script =
        android.support.v8.renderscript.ScriptIntrinsicBlur.create(
            rs, android.support.v8.renderscript.Element.U8_4(rs));
    script.setRadius(8f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(blurTemplate);
    Drawable dr = new BitmapDrawable(blurTemplate);

    return dr;
  }
Esempio n. 2
0
  @Override
  protected void createFilter(
      android.content.res.Resources res, float scaleFactor, int quality, Allocation in) {
    RenderScript rsCtx = getRenderScriptContext();

    Type.Builder tb_float = new Type.Builder(rsCtx, Element.F32_4(rsCtx));
    tb_float.setX(in.getType().getX());
    tb_float.setY(in.getType().getY());
    mScript = new ScriptC_grad(rsCtx, res, R.raw.grad);
  }
Esempio n. 3
0
  private void runSelectiveAdjust(Allocation in, Allocation out) {
    int width = in.getType().getX();
    int height = in.getType().getY();

    LaunchOptions options = new LaunchOptions();
    int ty;
    options.setX(0, width);

    for (ty = 0; ty < height; ty += STRIP_SIZE) {
      int endy = ty + STRIP_SIZE;
      if (endy > height) {
        endy = height;
      }
      options.setY(ty, endy);
      mScript.forEach_selectiveAdjust(in, out, options);
      if (checkStop()) {
        return;
      }
    }
  }
Esempio n. 4
0
  private static void blurRenderScript(Bitmap bitmap, float radius) {
    Allocation inAllocation =
        Allocation.createFromBitmap(
            renderScript, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation outAllocation = Allocation.createTyped(renderScript, inAllocation.getType());

    blurShader.setRadius(radius);
    blurShader.setInput(inAllocation);
    blurShader.forEach(outAllocation);

    outAllocation.copyTo(bitmap);
  }
Esempio n. 5
0
  public static Bitmap blurBitmap(Bitmap bitmapToBlur, Context context) {
    RenderScript rs = RenderScript.create(context);

    // use this constructor for best performance, because it uses USAGE_SHARED mode which reuses
    // memory
    final Allocation input = Allocation.createFromBitmap(rs, bitmapToBlur);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(16f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmapToBlur);
    return bitmapToBlur;
  }
Esempio n. 6
0
  @SuppressLint("NewApi")
  public static Bitmap blur(Context context, Bitmap sentBitmap, int radius) {
    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);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);
    return bitmap;
  }
Esempio n. 7
0
  protected boolean prepare() {
    final int width = mBlurredView.getWidth();
    final int height = mBlurredView.getHeight();

    if (mBlurringCanvas == null
        || mDownSampleFactorChanged
        || mBlurredViewWidth != width
        || mBlurredViewHeight != height) {
      mDownSampleFactorChanged = false;

      mBlurredViewWidth = width;
      mBlurredViewHeight = height;

      int scaledWidth = width / mDownSampleFactor;
      int scaledHeight = height / mDownSampleFactor;

      // The following manipulation is to avoid some RenderScript artifacts at the edge.
      scaledWidth = scaledWidth - scaledWidth % 4 + 4;
      scaledHeight = scaledHeight - scaledHeight % 4 + 4;

      if (mBlurredBitmap == null
          || mBlurredBitmap.getWidth() != scaledWidth
          || mBlurredBitmap.getHeight() != scaledHeight) {
        mBitmapToBlur = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
        if (mBitmapToBlur == null) {
          return false;
        }

        mBlurredBitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
        if (mBlurredBitmap == null) {
          return false;
        }
      }

      mBlurringCanvas = new Canvas(mBitmapToBlur);
      mBlurringCanvas.scale(1f / mDownSampleFactor, 1f / mDownSampleFactor);
      mBlurInput =
          Allocation.createFromBitmap(
              mRenderScript,
              mBitmapToBlur,
              Allocation.MipmapControl.MIPMAP_NONE,
              Allocation.USAGE_SCRIPT);
      mBlurOutput = Allocation.createTyped(mRenderScript, mBlurInput.getType());
    }
    return true;
  }