Java Code Examples for android.support.v8.renderscript.ScriptIntrinsicBlur#create()

The following examples show how to use android.support.v8.renderscript.ScriptIntrinsicBlur#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From renderscript-samples with Apache License 2.0 6 votes vote down vote up
private void createScript() {
    mRS = RenderScript.create(this);

    mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn);

    mOutAllocations = new Allocation[NUM_BITMAPS];
    for (int i = 0; i < NUM_BITMAPS; ++i) {
        mOutAllocations[i] = Allocation.createFromBitmap(mRS, mBitmapsOut[i]);
    }

    // Create intrinsics.
    // RenderScript has built-in features such as blur, convolve filter etc.
    // These intrinsics are handy for specific operations without writing RenderScript kernel.
    // In the sample, it's creating blur, convolve and matrix intrinsics.

    mScriptBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    mScriptConvolve = ScriptIntrinsicConvolve5x5.create(mRS,
            Element.U8_4(mRS));
    mScriptMatrix = ScriptIntrinsicColorMatrix.create(mRS,
            Element.U8_4(mRS));
}
 
Example 2
Source File: BlurBuilder.java    From react-native-simple-shadow-view with MIT License 6 votes vote down vote up
public static Bitmap blur(Context context, Bitmap image, float blurRadius, float scale) {
    int width = Math.round(image.getWidth() * scale);
    int height = Math.round(image.getHeight() * scale);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);

    ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

    intrinsicBlur.setRadius(blurRadius);
    intrinsicBlur.setInput(tmpIn);
    intrinsicBlur.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example 3
Source File: BlurTransformation.java    From android-tutorials-glide with MIT License 6 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(10);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    toTransform.recycle();

    return blurredBitmap;
}
 
Example 4
Source File: Utils.java    From BlurZoomGallery with MIT License 5 votes vote down vote up
public static void blurImage(RenderScript renderScript, Bitmap bmp, float radius) {
    final Allocation input = Allocation.createFromBitmap(renderScript, bmp);
    final Allocation output = Allocation.createTyped(renderScript, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bmp);
}
 
Example 5
Source File: RenderScriptBlurHelper.java    From BlurDialogFragment with Apache License 2.0 5 votes vote down vote up
/**
 * blur a given bitmap
 *
 * @param sentBitmap       bitmap to blur
 * @param radius           blur radius
 * @param canReuseInBitmap true if bitmap must be reused without blur
 * @param context          used by RenderScript, can be null if RenderScript disabled
 * @return blurred bitmap
 */
public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap, Context context) {
    Bitmap bitmap;

    if (canReuseInBitmap) {
        bitmap = sentBitmap;
    } else {
        bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    }

    if (bitmap.getConfig() == Bitmap.Config.RGB_565) {
        // RenderScript hates RGB_565 so we convert it to ARGB_8888
        // (see http://stackoverflow.com/questions/21563299/
        // defect-of-image-with-scriptintrinsicblur-from-support-library)
        bitmap = convertRGB565toARGB888(bitmap);
    }

    try {
        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs, bitmap, 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;
    } catch (RSRuntimeException e) {
        Log.e(TAG, "RenderScript known error : https://code.google.com/p/android/issues/detail?id=71347 "
            + "continue with the FastBlur approach.");
    }

    return null;
}
 
Example 6
Source File: Blur.java    From AllAngleExpandableButton with Apache License 2.0 5 votes vote down vote up
@WorkerThread
private Bitmap getBlurBitmap(Context context, Bitmap inBitmap, float radius) {
    if (context == null || inBitmap == null) {
        throw new IllegalArgumentException("have not called setParams() before call execute()");
    }

    int width = Math.round(inBitmap.getWidth() * SCALE);
    int height = Math.round(inBitmap.getHeight() * SCALE);

    Bitmap in = Bitmap.createScaledBitmap(inBitmap, width, height, false);
    Bitmap out = Bitmap.createBitmap(in);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    Allocation allocationIn = Allocation.createFromBitmap(rs, in);
    Allocation allocationOut = Allocation.createFromBitmap(rs, out);

    blurScript.setRadius(radius);
    blurScript.setInput(allocationIn);
    blurScript.forEach(allocationOut);
    allocationOut.copyTo(out);

    allocationIn.destroy();
    allocationOut.destroy();
    blurScript.destroy();
    rs.destroy();

    return out;
}
 
Example 7
Source File: RenderScriptGaussianBlur.java    From AndroidMaterialDesign with Apache License 2.0 5 votes vote down vote up
public Bitmap blur(int radius, Bitmap bitmapOriginal) {
    final Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal);
    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(bitmapOriginal);
    return bitmapOriginal;
}
 
Example 8
Source File: RSGaussianBlur.java    From BlurView with Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap blur(int radius, Bitmap bitmapOriginal) {
    radius = Math.min(radius,25);

    final Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal);
    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(bitmapOriginal);
    return bitmapOriginal;
}
 
Example 9
Source File: MizLib.java    From Mizuu with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a blurred bitmap. It uses a RenderScript to blur the bitmap very fast.
 * @param context
 * @param originalBitmap
 * @param radius
 * @return
 */
public static Bitmap fastBlur(Context context, Bitmap originalBitmap, int radius) {
    final RenderScript rs = RenderScript.create(context);

    final Allocation input = Allocation.createFromBitmap(rs, originalBitmap);
    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(originalBitmap);

    return originalBitmap;
}
 
Example 10
Source File: BlurBuilder.java    From talk-android with MIT License 5 votes vote down vote up
public static void RsBlur(RenderScript rs, Bitmap original) {
    // use this constructor for best performance, because it uses USAGE_SHARED mode which
    // reuses memory
    final Allocation input = Allocation.createFromBitmap(rs, original);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(8f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(original);
    rs.destroy();
}
 
Example 11
Source File: BlurTransformation.java    From Dota2Helper with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();
    int scaledWidth = width / mSampling;
    int scaledHeight = height / mSampling;

    Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(source, 0, 0, paint);

    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
            Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    blur.setInput(input);
    blur.setRadius(mRadius);
    blur.forEach(output);
    output.copyTo(bitmap);

    rs.destroy();

    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 12
Source File: BlurTransformation.java    From Sky31Radio with Apache License 2.0 5 votes vote down vote up
@Override
    public Bitmap transform(Bitmap source) {
        Timber.i("start bitmap transform");
        try {
            float radius = 10f;
            Bitmap outputBitmap;
//            if(Build.VERSION.SDK_INT >= 17){
                outputBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
                RenderScript rs = RenderScript.create(ctx);
                ScriptIntrinsicBlur sib = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
                Allocation tmpIn = Allocation.createFromBitmap(rs, source);
                Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
                sib.setRadius(radius);
                sib.setInput(tmpIn);
                sib.forEach(tmpOut);
                tmpOut.copyTo(outputBitmap);
                source.recycle();
//            }else{
//                outputBitmap = FastBlur.doBlur(source, (int) radius, true);
//            }
            Timber.d("blur bitmap success");
            return outputBitmap;
        } catch (Exception e) {
            Timber.e(e, "occur an error during blurring bitmap");
            return source;
        }
    }
 
Example 13
Source File: Blur.java    From easyrs with MIT License 5 votes vote down vote up
@Override
public void runScript(RSToolboxContext rsToolboxContext, Allocation aout, BlurParams scriptParams) {
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(
            rsToolboxContext.rs, rsToolboxContext.ain.getElement());
    blurScript.setInput(rsToolboxContext.ain);
    blurScript.setRadius(scriptParams.radius);
    blurScript.forEach(aout);
}
 
Example 14
Source File: BlurBitmapUtil.java    From Recyclerview-Gallery with Apache License 2.0 5 votes vote down vote up
/**
 * 模糊图片的具体方法
 *
 * @param context 上下文对象
 * @param image   需要模糊的图片
 * @return 模糊处理后的图片
 */
public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
    // 计算图片缩小后的长宽
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    // 将缩小后的图片做为预渲染的图片
    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    // 创建一张渲染后的输出图片
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    // 创建RenderScript内核对象
    RenderScript rs = RenderScript.create(context);
    // 创建一个模糊效果的RenderScript的工具对象
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间
    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

    // 设置渲染的模糊程度, 25f是最大模糊度
    blurScript.setRadius(blurRadius);
    // 设置blurScript对象的输入内存
    blurScript.setInput(tmpIn);
    // 将输出数据保存到输出内存中
    blurScript.forEach(tmpOut);

    // 将数据填充到Allocation中
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example 15
Source File: FastStyleModel.java    From style-transfer with Apache License 2.0 5 votes vote down vote up
public Bitmap processImage(Bitmap bitmap) {
    if (!mLoaded) {
        try {
            loadModel();
        } catch (IOException e) {

        }
    }
    // convert the bitmap to RGB * (h * w);
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    // Crop the image.
    Bitmap outImgBig = Bitmap.createBitmap(bitmap, (width - MAX_IMG_SIZE) / 2,
            (height - MAX_IMG_SIZE) / 2, MAX_IMG_SIZE, MAX_IMG_SIZE);
    // Process the cropped image through the neural net.
    Allocation outImgBigAlloc = processImgChunk(outImgBig);

    // Blur the output image a bit.
    Allocation blurredAlloc = Allocation.createFromBitmap(mRS, outImgBig);
    ScriptIntrinsicBlur mBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    mBlur.setInput(outImgBigAlloc);
    mBlur.setRadius(1.5f);
    mBlur.forEach(blurredAlloc);
    blurredAlloc.copyTo(outImgBig);

    logBenchmarkResult();
    return outImgBig;
}
 
Example 16
Source File: GlassView.java    From GlassView with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    mRenderScript = RenderScript.create(getContext());
    mBlur = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));
    if (isPostHoneycomb() && isHardwareAccelerated()) {
        getViewTreeObserver().addOnGlobalLayoutListener(getGlobalLayoutListener());
        getViewTreeObserver().addOnScrollChangedListener(getScrollChangedListener());
    }
}
 
Example 17
Source File: BlurFilter.java    From ImageCropRotateFilter with MIT License 4 votes vote down vote up
@Override
public void createFilter(Resources res) {
    mIntrinsic = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    mIntrinsic.setInput(mInPixelsAllocation);
    mIntrinsic.setRadius(mRadius);
}
 
Example 18
Source File: BlurDrawable.java    From BlurView with Apache License 2.0 4 votes vote down vote up
private void initRenderScript(Context context) {
    mRenderScript = RenderScript.create(context);
    mBlurScript = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));
    blurRadius(mBlurRadius);
    sizeDivider(8);
}
 
Example 19
Source File: RenderscriptBlurringMachine.java    From fogger with Apache License 2.0 4 votes vote down vote up
private ScriptIntrinsicBlur createBlurringScript(int radius, RenderScript rs, Allocation input) {
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius);
    script.setInput(input);
    return script;
}
 
Example 20
Source File: RenderScriptBlur.java    From EtsyBlur with Apache License 2.0 4 votes vote down vote up
public RenderScriptBlur(@NonNull Context context, @NonNull BlurConfig blurConfig) {
    super(blurConfig);
    rs = RenderScript.create(context);
    scriptBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
}