android.renderscript.RenderScript Java Examples

The following examples show how to use android.renderscript.RenderScript. 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 OnionCamera with MIT License 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    preview = (TextureView) findViewById(R.id.preview);
    overlay = (TextureView) findViewById(R.id.overlay);

    filter = new Filter(RenderScript.create(this));
    preview.setSurfaceTextureListener(this);
    overlay.setSurfaceTextureListener(filter);

    preview.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            filter.toggleBlending();
        }
    });
}
 
Example #2
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 twelve(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 colorMatrix12 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix12.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    .309f, .409f, .309f, 0f,
                    .609f, .309f, .409f, 0f,
                    0.42f, .42f, .2f, 0f,
                    0f, 0f, 0f, 1f


            }));
    colorMatrix12.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #3
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 #4
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 #5
Source File: MainActivity.java    From rscnn with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    rs = RenderScript.create(this);
    try {
        AssetManager assetManager = getAssets();
        String[] fileList = assetManager.list(modelPath);
        if (fileList.length != 0){
            detector = new MobileNetSSD(rs, assetManager, modelPath);
        }
        else {
            String modelDir = Environment.getExternalStorageDirectory().getPath() + "/" + modelPath;
            detector = new MobileNetSSD(rs, null, modelDir);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    setContentView(R.layout.activity_main);
}
 
Example #6
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 eleven(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 colorMatrix11 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix11.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    1.72814708519562f, -0.412104992562475f, 0.541145007437525f, 0f,
                    0.289378264402959f, 1.18835534216106f, -1.17637173559704f, 0f,
                    -1.01752534959858f, 0.223749650401417f, 1.63522672815952f, 0f,
                    0f, 0f, 0f, 1f


            }));
    colorMatrix11.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #7
Source File: ActivityThread.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
    if (Process.isIsolated()) {
        // Isolated processes aren't going to do UI.
        return;
    }
    try {
        int uid = Process.myUid();
        String[] packages = getPackageManager().getPackagesForUid(uid);

        // If there are several packages in this application we won't
        // initialize the graphics disk caches 
        if (packages != null && packages.length == 1) {
            HardwareRenderer.setupDiskCache(cacheDir);
            RenderScript.setupDiskCache(cacheDir);
        }
    } catch (RemoteException e) {
        // Ignore
    }
}
 
Example #8
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 #9
Source File: BitmapUtil.java    From a with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 高斯模糊
 */
public static Bitmap stackBlur(Bitmap srcBitmap) {
    if (srcBitmap == null) return null;
    RenderScript rs = RenderScript.create(MApplication.getInstance());
    Bitmap blurredBitmap = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);

    //分配用于渲染脚本的内存
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    //加载我们想要使用的特定脚本的实例。
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    //设置模糊半径
    script.setRadius(8);

    //启动 ScriptIntrinsicBlur
    script.forEach(output);

    //将输出复制到模糊的位图
    output.copyTo(blurredBitmap);

    return blurredBitmap;
}
 
Example #10
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 #11
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 #12
Source File: RSGaussianBlurTransformation.java    From picasso-transformations with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    if (Build.VERSION.SDK_INT < 17) {
        return source;
    }
    
    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, source, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(mRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(source);
    return source;
}
 
Example #13
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 #14
Source File: BlurUtils.java    From Decor with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Bitmap blurBitmap(Bitmap src, float blurRadius, Context context) {
    RenderScript rs = RenderScript.create(context);
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap blurredBitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), conf);

    final Allocation input = Allocation.createFromBitmap(rs, src, 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(blurRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(blurredBitmap);
    return blurredBitmap;
}
 
Example #15
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 #16
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 #17
Source File: UriGlideRenderer.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 #18
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 #19
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 #20
Source File: BlurBuilder.java    From Audinaut with GNU General Public License v3.0 6 votes vote down vote up
private static Bitmap blur_real(Context context, 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(context);
    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 #21
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 #22
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 #23
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 #24
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 one(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 colorMatrix1=ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix1.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    -0.33f, -0.33f, -0.33f, 1.0f,
                    -0.59f, -0.59f, -0.59f, 1.0f,
                    -0.11f, -0.11f, -0.11f, 1.0f,
                    1.0f, 1.0f, 1.0f, 1.0f
            }));
    colorMatrix1.forEach(inputAllocation,outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
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 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 #26
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 #27
Source File: FindRegion.java    From style-transfer with Apache License 2.0 5 votes vote down vote up
Allocation allocFloat2(float[] p, RenderScript rs) {
    Type.Builder builderF32_2 = new Type.Builder(rs, Element.F32_2(rs));
    builderF32_2.setX(p.length / 2);
    Allocation ret = Allocation.createTyped(rs, builderF32_2.create());
    ret.copyFrom(p);
    return ret;
}
 
Example #28
Source File: GaussianBlur.java    From AndroidUI with MIT License 5 votes vote down vote up
/**
 * 通过RenderScript进行图片模糊
 * @param bkg       需要模糊的bitmap
 * @param radius    模糊半径,RenderScript规定范围为[1,25]
 * @param view      显示模糊图片的ImageView
 * @param context   上下文
 * @return          消耗时间,单位毫秒(ms)
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static long blurByRenderScript(Bitmap bkg,int radius, ImageView view,Context context)
{
    long startMs = System.currentTimeMillis();
    float scaleFactor = 8;

    int width = (int)(view.getMeasuredWidth()/scaleFactor);
    int height = (int)(view.getMeasuredHeight()/scaleFactor);

    Bitmap overlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.scale(1 / scaleFactor, 1 / scaleFactor);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bkg, 0, 0, paint);

    RenderScript rs = RenderScript.create(context);

    Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
    blur.setInput(overlayAlloc);
    blur.setRadius(radius);
    blur.forEach(overlayAlloc);
    overlayAlloc.copyTo(overlay);

    view.setImageBitmap(overlay);

    rs.destroy();

    return System.currentTimeMillis() - startMs;
}
 
Example #29
Source File: RxImageTool.java    From RxTools-master with Apache License 2.0 5 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, float radius) {
    if (isEmptyBitmap(src)) return null;
    RenderScript rs = null;
    try {
        rs = RenderScript.create(RxTool.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));
        if (radius > 25) {
            radius = 25.0f;
        } else if (radius <= 0) {
            radius = 1.0f;
        }
        blurScript.setInput(input);
        blurScript.setRadius(radius);
        blurScript.forEach(output);
        output.copyTo(src);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return src;
}
 
Example #30
Source File: TwitterCoverListView.java    From TwitterCover-Android with MIT License 5 votes vote down vote up
public Bitmap renderScriptBlur(Bitmap bitmap, int radius) {
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    RenderScript rs = RenderScript.create(mContext);

    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
    blurScript.setRadius(radius);
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);
    allOut.copyTo(outBitmap);
    rs.destroy();
    return outBitmap;
}