android.renderscript.Element Java Examples

The following examples show how to use android.renderscript.Element. 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: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap four(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicColorMatrix colorMatrix4 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix4.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    0.3f, 0f, 0f, 0f,
                    0f, 0.65f, 0f, 0f,
                    0f, 0f, 0.49f, 0f,
                    0f, 0f, 0f, 1f


            }));
    colorMatrix4.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #2
Source File: BitmapBlurHelper.java    From AroundCircleView with Apache License 2.0 6 votes vote down vote up
/**
 * 模糊函数
 * @param context
 * @param sentBitmap
 * @param radius
 * @return
 */
public static Bitmap doBlur(Context context, Bitmap sentBitmap, float radius) {
    if(sentBitmap==null) return null;
    if (radius <= 0 || radius > 25) radius = 25f;//范围在1-25之间
    if (radius<=6&& Build.VERSION.SDK_INT > 16) {//经测试,radius大于6后,fastBlur效率更高,并且RenderScript在api11以上使用
        Bitmap bitmap = Bitmap.createScaledBitmap(sentBitmap, sentBitmap.getWidth()/SCALE,sentBitmap.getHeight()/SCALE,false);//先缩放图片,增加模糊速度
        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);
        rs.destroy();
        return bitmap;
    }else{//快速模糊
        return fastBlur(sentBitmap,radius);
    }
}
 
Example #3
Source File: YuvToRgb.java    From unity-android-native-camera with MIT License 6 votes vote down vote up
private void createAllocations(RenderScript rs) {

        final int width = mInputSize.getWidth();
        final int height = mInputSize.getHeight();

        mOutBufferInt = new int[width * height];

        Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs));
        yuvTypeBuilder.setX(width);
        yuvTypeBuilder.setY(height);
        yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888);
        mInputAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(),
                Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);

        Type rgbType = Type.createXY(rs, Element.RGBA_8888(rs), width, height);
        Type intType = Type.createXY(rs, Element.U32(rs), width, height);

        mOutputAllocation = Allocation.createTyped(rs, rgbType,
                Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);
        mOutputAllocationInt = Allocation.createTyped(rs, intType,
                Allocation.USAGE_SCRIPT);
    }
 
Example #4
Source File: RenderscriptConvolutionActivity.java    From android-graphics-demo with Apache License 2.0 6 votes vote down vote up
private Bitmap convolve(Bitmap original, float[] coefficients) {
  Bitmap bitmap = Bitmap.createBitmap(
      original.getWidth(), original.getHeight(),
      Bitmap.Config.ARGB_8888);

  RenderScript rs = RenderScript.create(this);

  Allocation allocIn = Allocation.createFromBitmap(rs, original);
  Allocation allocOut = Allocation.createFromBitmap(rs, bitmap);

  ScriptIntrinsicConvolve3x3 convolution
      = ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs));
  convolution.setInput(allocIn);
  convolution.setCoefficients(coefficients);
  convolution.forEach(allocOut);

  allocOut.copyTo(bitmap);

  rs.destroy();

  return bitmap;
}
 
Example #5
Source File: BlurTransform.java    From android-tv-leanback with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, bitmap, 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(20);

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

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

    bitmap.recycle();

    return blurredBitmap;
}
 
Example #6
Source File: TflitePlugin.java    From flutter_tflite with MIT License 6 votes vote down vote up
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {
  // https://stackoverflow.com/a/36409748
  RenderScript rs = RenderScript.create(context);
  ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));

  Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
  Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

  Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
  Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);

  in.copyFrom(nv21);

  yuvToRgbIntrinsic.setInput(in);
  yuvToRgbIntrinsic.forEach(out);
  return out;
}
 
Example #7
Source File: BlurBitmapUtil.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
public static Drawable createBlurredImageFromBitmap(Bitmap bitmap, Context context, int inSampleSize) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        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 Allocation input = Allocation.createFromBitmap(rs, blurTemplate);
        final Allocation output = Allocation.createTyped(rs, input.getType());
        final ScriptIntrinsicBlur script;
        script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setRadius(8f);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(blurTemplate);
        return new BitmapDrawable(context.getResources(), blurTemplate);
    }
    return null;
}
 
Example #8
Source File: BlurTransform.java    From android-tv-leanback with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, bitmap, 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(20);

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

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

    bitmap.recycle();

    return blurredBitmap;
}
 
Example #9
Source File: Blur.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) {
    final 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);

    sentBitmap.recycle();
    rs.destroy();
    input.destroy();
    output.destroy();
    script.destroy();

    return bitmap;
}
 
Example #10
Source File: ImageUtils.java    From PrivacyStreams with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blur(UQI uqi, Bitmap image) {
    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 rs = RenderScript.create(uqi.getContext());
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example #11
Source File: ImageUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * renderScript模糊图片
 * <p>API大于17</p>
 *
 * @param src     源图片
 * @param radius  模糊半径(0...25)
 * @return 模糊后的图片
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(Bitmap src, @FloatRange(from = 0, to = 25, fromInclusive = false) float radius) {
    if (isEmptyBitmap(src)) return null;
    RenderScript rs = null;
    try {
        rs = RenderScript.create(Utils.getContext());
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, src, Allocation.MipmapControl.MIPMAP_NONE, Allocation
                .USAGE_SCRIPT);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blurScript.setInput(input);
        blurScript.setRadius(radius);
        blurScript.forEach(output);
        output.copyTo(src);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return src;
}
 
Example #12
Source File: BlurImageView.java    From BlurImageView with MIT License 6 votes vote down vote up
private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) {

        int width  = Math.round(smallBitmap.getWidth() * defaultBitmapScale);
        int height = Math.round(smallBitmap.getHeight() * defaultBitmapScale);

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

        RenderScript renderScript = RenderScript.create(getContext());
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        Allocation tmpIn = Allocation.createFromBitmap(renderScript, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);
        theIntrinsic.setRadius(radius);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);

        return outputBitmap;
    }
 
Example #13
Source File: CoolBGView.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
/**
 * 获取模糊的图片
 *
 * @param context 上下文对象
 * @param bitmap  传入的bitmap图片
 */
private Bitmap blurBitmap(Context context, Bitmap bitmap) {
    //用需要创建高斯模糊bitmap创建一个空的bitmap
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    // 初始化Renderscript,该类提供了RenderScript context,创建其他RS类之前必须先创建这个类,其控制RenderScript的初始化,资源管理及释放
    RenderScript rs = RenderScript.create(context);
    // 创建一个模糊效果的RenderScript的工具对象
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。
    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
    //设定模糊度(注:radius最大只能设置25f)
    blurScript.setRadius(BLUR_RADIUS);
    // 设置blurScript对象的输入内存
    blurScript.setInput(allIn);
    // 将输出数据保存到输出内存中
    blurScript.forEach(allOut);
    // 将数据填充到Allocation中
    allOut.copyTo(outBitmap);
    rs.destroy();
    return outBitmap;
}
 
Example #14
Source File: VMBlur.java    From VMLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * 使用 RenderScript 模糊图片
 * PS:此方法只能在 SDK API 17 以上使用
 *
 * @param context 上下文对象
 * @param bitmap 需要模糊的bitmap
 * @param scale 模糊的比例因数
 * @param radius 模糊半径
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap rsBlurBitmp(Context context, Bitmap bitmap, int scale, float radius) {
    // 创建一个新的压缩过的 Bitmap
    Bitmap overlay = VMBitmap.compressBitmapByScale(bitmap, scale);

    RenderScript rs = null;
    try {
        rs = RenderScript.create(context);
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, overlay, 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(radius);
        blur.forEach(output);
        output.copyTo(overlay);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return overlay;
}
 
Example #15
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap three(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicColorMatrix colorMatrix3 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix3.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    0f, 0f, 0f, 0f,
                    0f, 0.78f, 0f, 0f,
                    0f, 0f, 1f, 0f,
                    0f, 0f, 0f, 1f,
            }));
    colorMatrix3.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #16
Source File: SwipeBackHelper.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static Bitmap blur(Context context, Bitmap source, float radius){
    RenderScript renderScript =  RenderScript.create(context);

    final Allocation input = Allocation.createFromBitmap(renderScript, source);
    final Allocation output = Allocation.createTyped(renderScript,input.getType());

    ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(
            renderScript,
            Element.U8_4(renderScript)
    );
    scriptIntrinsicBlur.setInput(input);
    scriptIntrinsicBlur.setRadius(radius);
    scriptIntrinsicBlur.forEach(output);
    output.copyTo(source);
    renderScript.destroy();

    return source;
}
 
Example #17
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap five(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final  ScriptIntrinsicColorMatrix colorMatrix5 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix5.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    -0.359705309629158f, 0.377252728606377f, 0.663841667303255f, 0f,
                    1.56680818833214f, 0.456668209492391f, 1.12613917506705f, 0f,
                    -0.147102878702981f, 0.226079061901232f, -0.729980842370303f, 0f,
                    0f, 0f, 0f, 1f
            }));
    colorMatrix5.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #18
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap seven(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final  ScriptIntrinsicColorMatrix colorMatrix7 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix7.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    1.22994596833595f, 0.0209523774645382f, 0.383244054685119f, 0f,
                    0.450138899443543f, 1.18737418804171f, -0.106933249401007f, 0f
                    - 0.340084867779496f, 0.131673434493755f, 1.06368919471589f, 0f,
                    0f, 0f, 0f,
                    11.91f, 11.91f, 11.91f, 0f}));
    colorMatrix7.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #19
Source File: Blur.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) {
    final 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);

    sentBitmap.recycle();
    rs.destroy();
    input.destroy();
    output.destroy();
    script.destroy();

    return bitmap;
}
 
Example #20
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap nine(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final  ScriptIntrinsicColorMatrix colorMatrix9 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix9.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    -2f, -1f, 1f, -2f,
                    0f, -2f, 0f, 1f,
                    0f, 0f, -1f, 1f,
                    0f, 0f, 0f, 1f
            }));
    colorMatrix9.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #21
Source File: UriGlideRenderer.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
@RequiresApi(17)
private static @NonNull Bitmap blur(Bitmap bitmap, Context context) {
  Point  previewSize = scaleKeepingAspectRatio(new Point(bitmap.getWidth(), bitmap.getHeight()), PREVIEW_DIMENSION_LIMIT);
  Point  blurSize    = scaleKeepingAspectRatio(new Point(previewSize.x / 2, previewSize.y / 2 ), MAX_BLUR_DIMENSION);
  Bitmap small       = BitmapUtil.createScaledBitmap(bitmap, blurSize.x, blurSize.y);

  Log.d(TAG, "Bitmap: " + bitmap.getWidth() + "x" + bitmap.getHeight() + ", Blur: " + blurSize.x + "x" + blurSize.y);

  RenderScript        rs     = RenderScript.create(context);
  Allocation          input  = Allocation.createFromBitmap(rs, small);
  Allocation          output = Allocation.createTyped(rs, input.getType());
  ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

  script.setRadius(25f);
  script.setInput(input);
  script.forEach(output);

  Bitmap blurred = Bitmap.createBitmap(small.getWidth(), small.getHeight(), small.getConfig());
  output.copyTo(blurred);
  return blurred;
}
 
Example #22
Source File: BlurTransform.java    From android-tv-leanback with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, bitmap, 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(20);

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

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

    bitmap.recycle();

    return blurredBitmap;
}
 
Example #23
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap thirteen(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicConvolve3x3 convolve1 = ScriptIntrinsicConvolve3x3.create(renderScript, Element.U8_4(renderScript));
    convolve1.setInput(inputAllocation);
    convolve1.setCoefficients(new float[]
            {
                    -2, -1, 0,
                    -1, 1, 1,
                    0, 1, 2
            });
    convolve1.forEach(outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #24
Source File: BlurTransform.java    From android-tv-leanback with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, bitmap, 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(20);

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

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

    bitmap.recycle();

    return blurredBitmap;
}
 
Example #25
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap fifteen(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicColorMatrix colorMatrix13 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix13.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {

                    2.10279132254252f,  -0.298212630531356f,       0.42128146417712f,   0f,
                    0.222897572029231f,     1.68701190285368f,     -0.883421304780577f,   0f,
                    -0.765688894571747f,   0.171200727677677f,    2.02213984060346f,     0f,
                    0                          ,  0                 ,0                ,1f


            }));
    colorMatrix13.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #26
Source File: Utils.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * Applies a blur to a {@link Bitmap}.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap fastBlur(Context mContext, Bitmap sentBitmap, final int radius) {
    if (null == rs) {
        rs = RenderScript.create(mContext);
    }

    final Allocation in = Allocation.createFromBitmap(rs, sentBitmap,
            Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    final Allocation out = Allocation.createTyped(rs, in.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius);
    script.setInput(in);
    script.forEach(out);
    out.copyTo(sentBitmap);
    return (sentBitmap);
}
 
Example #27
Source File: BlurTransformation.java    From TuentiTV with Apache License 2.0 6 votes vote down vote up
@Override public Bitmap transform(Bitmap source) {
  Bitmap original = source;
  Bitmap blurred;
  blurred = Bitmap.createBitmap(original);

  RenderScript rs = RenderScript.create(context);

  Allocation input =
      Allocation.createFromBitmap(rs, original, Allocation.MipmapControl.MIPMAP_FULL,
          Allocation.USAGE_SCRIPT);
  Allocation output = Allocation.createTyped(rs, input.getType());

  ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
  script.setInput(input);
  script.setRadius(RADIUS);
  script.forEach(output);

  output.copyTo(blurred);
  source.recycle();
  return blurred;
}
 
Example #28
Source File: FindRegion.java    From style-transfer with Apache License 2.0 6 votes vote down vote up
public Rect findMatch(ScriptC_find_region findRegion, RenderScript mRs, Bitmap image) {
    long time = System.nanoTime();

    Allocation border_coords;

    border_coords = allocFloat2(mPointsXY, mRs);
    Allocation aImage = Allocation.createFromBitmap(mRs, image);
    Allocation ret = Allocation.createSized(mRs, Element.I32_2(mRs), 1);

    findRegion.invoke_findRegion(border_coords, aImage, image.getWidth(), image.getHeight(), ret);

    int[] mina = new int[2];
    ret.copyTo(mina);
    mCutOffsetX = mina[0];
    mCutOffsetY = mina[1];
    Log.v(TAG, "New best location = " + mCutOffsetX + ", " + mCutOffsetY);
    Log.v(TAG, "Time to find replacement= " + (System.nanoTime() - time) / 1E6f + "ms");

    return mRoiBounds;
}
 
Example #29
Source File: Utils.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
public static Bitmap blurBitmap(Context context, Bitmap bmp, float radius) {
    Bitmap out = Bitmap.createBitmap(bmp);
    RenderScript rs = RenderScript.create(context);
    radius = Math.min(Math.max(radius, 0), 25);

    Allocation input = Allocation.createFromBitmap(
            rs, bmp, MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());

    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);
    script.setRadius(radius);
    script.forEach(output);

    output.copyTo(out);

    rs.destroy();
    return out;
}
 
Example #30
Source File: BlurTransform.java    From android-tv-leanback with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, bitmap, 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(20);

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

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

    bitmap.recycle();

    return blurredBitmap;
}