Java Code Examples for android.renderscript.RenderScript#create()

The following examples show how to use android.renderscript.RenderScript#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: 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 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 sixteen(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 colorMatrix14 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix14.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {

                    1.27488526960083f, -0.228511311848763f,   0.441088688151237f,  0,
                    0.323664244263542f,  0.955140825713134f,  -0.705935755736458f,  0,
                    -0.698549513864371f, 0.173370486135629f ,  1.16484706758522f  ,0,
                    0,0,0,1


            }));
    colorMatrix14.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 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 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: 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 6
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 7
Source File: CameraSource.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 6 votes vote down vote up
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {
    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        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);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        yuvToRgbIntrinsic.setInput(in);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        yuvToRgbIntrinsic.forEach(out);
    }
    return out;
}
 
Example 8
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 six(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 colorMatrix6 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix6.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    1.2f, 0.1f, 0.2f, 0.7f,

                    0.7f, 1f, 0f, -0.5f,
                    -0.7f, 0.2f, 0.5f, 1.3f,
                    0, -0.1f, 0f, 0.9f
            }));
    colorMatrix6.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example 9
Source File: ViewPagerAdapter.java    From Simple-Image-Blur with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) {

    try {
        smallBitmap = RGB565toARGB888(smallBitmap);
    } catch (Exception e) {
        e.printStackTrace();
    }


    Bitmap bitmap = Bitmap.createBitmap(
            smallBitmap.getWidth(), smallBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);

    RenderScript renderScript = RenderScript.create(context);

    Allocation blurInput = Allocation.createFromBitmap(renderScript, smallBitmap);
    Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
            Element.U8_4(renderScript));
    blur.setInput(blurInput);
    blur.setRadius(radius); // radius must be 0 < r <= 25
    blur.forEach(blurOutput);

    blurOutput.copyTo(bitmap);
    renderScript.destroy();

    return bitmap;

}
 
Example 10
Source File: ImageJpegPlugin.java    From image_jpeg with MIT License 5 votes vote down vote up
public Bitmap blurImage(Bitmap bm, int blur, int blurZoom) {
    if (blur > 0 && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        RenderScript rs = RenderScript.create(mRegistrar.activity());
        ScriptIntrinsicBlur _blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        int w = bm.getWidth();
        int h = bm.getHeight();
        Bitmap _temp;
        if (blurZoom == 0) {
            _temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        } else {
            int tw = w / blurZoom;
            int th = h / blurZoom;
            bm = Bitmap.createScaledBitmap(bm, tw, th, false);
            _temp = Bitmap.createBitmap(tw, th, Bitmap.Config.ARGB_8888);
        }
        Allocation _in = Allocation.createFromBitmap(rs, bm);
        Allocation _out = Allocation.createFromBitmap(rs, _temp);
        _blur.setRadius(Math.min(25.0f, (((float) blur) / 100.0f) * 25.0f)); // 0 ~ 25
        _blur.setInput(_in);
        _blur.forEach(_out);
        _out.copyTo(_temp);
        rs.destroy();
        if (blurZoom == 0)
            return Bitmap.createBitmap(_temp);
        else
            return Bitmap.createScaledBitmap(_temp, w, h, true);
    } else
        return bm;
}
 
Example 11
Source File: TicketView.java    From TicketView with Apache License 2.0 5 votes vote down vote up
private void generateShadow() {
    if (isJellyBeanAndAbove() && !isInEditMode()) {
        if (mShadowBlurRadius == 0f) return;

        if (mShadow == null) {
            mShadow = Bitmap.createBitmap(getWidth(), getHeight(), ALPHA_8);
        } else {
            mShadow.eraseColor(TRANSPARENT);
        }
        Canvas c = new Canvas(mShadow);
        c.drawPath(mPath, mShadowPaint);
        if (mShowBorder) {
            c.drawPath(mPath, mShadowPaint);
        }
        RenderScript rs = RenderScript.create(getContext());
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8(rs));
        Allocation input = Allocation.createFromBitmap(rs, mShadow);
        Allocation output = Allocation.createTyped(rs, input.getType());
        blur.setRadius(mShadowBlurRadius);
        blur.setInput(input);
        blur.forEach(output);
        output.copyTo(mShadow);
        input.destroy();
        output.destroy();
        blur.destroy();
    }
}
 
Example 12
Source File: BlurKit.java    From blurkit-android with MIT License 5 votes vote down vote up
public static void init(Context context) {
    if (instance != null) {
        return;
    }

    instance = new BlurKit();
    rs = RenderScript.create(context.getApplicationContext());
}
 
Example 13
Source File: HdrViewfinderActivity.java    From android-HdrViewfinder with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    rootView = findViewById(R.id.panels);

    mPreviewView = (FixedAspectSurfaceView) findViewById(R.id.preview);
    mPreviewView.getHolder().addCallback(this);
    mPreviewView.setGestureListener(this, mViewListener);

    Button helpButton = (Button) findViewById(R.id.help_button);
    helpButton.setOnClickListener(mHelpButtonListener);

    mModeText = (TextView) findViewById(R.id.mode_label);
    mEvenExposureText = (TextView) findViewById(R.id.even_exposure);
    mOddExposureText = (TextView) findViewById(R.id.odd_exposure);
    mAutoExposureText = (TextView) findViewById(R.id.auto_exposure);

    mUiHandler = new Handler(Looper.getMainLooper());

    mRS = RenderScript.create(this);

    // When permissions are revoked the app is restarted so onCreate is sufficient to check for
    // permissions core to the Activity's functionality.
    if (!checkCameraPermissions()) {
        requestCameraPermissions();
    } else {
        findAndOpenCamera();
    }
}
 
Example 14
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;
}
 
Example 15
Source File: CaptionedImageView.java    From auid2 with Apache License 2.0 4 votes vote down vote up
private void updateBlur() {
    if (!(mDrawable instanceof BitmapDrawable)) {
        return;
    }
    final int textViewHeight = mTextView.getHeight();
    final int imageViewHeight = mImageView.getHeight();
    if (textViewHeight == 0 || imageViewHeight == 0) {
        return;
    }

    // Get the Bitmap
    final BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
    final Bitmap originalBitmap = bitmapDrawable.getBitmap();

    // Determine the size of the TextView compared to the height of the ImageView
    final float ratio = (float) textViewHeight / imageViewHeight;

    // Calculate the height as a ratio of the Bitmap
    final int height = (int) (ratio * originalBitmap.getHeight());
    final int width = originalBitmap.getWidth();
    final String blurKey = getBlurKey(width);
    Bitmap newBitmap = BitmapUtils.getBitmap(blurKey);
    if (newBitmap != null) {
        mImageView.setImageBitmap(newBitmap);
        return;
    }

    // The y position is the number of pixels height represents from the bottom of the Bitmap
    final int y = originalBitmap.getHeight() - height;

    TraceCompat.beginSection("BLUR - createBitmaps");
    final Bitmap portionToBlur = Bitmap.createBitmap(originalBitmap, 0, y, originalBitmap.getWidth(), height);
    final Bitmap blurredBitmap = Bitmap.createBitmap(portionToBlur.getWidth(), height, Bitmap.Config.ARGB_8888);
    TraceCompat.endSection();

    // Use RenderScript to blur the pixels
    TraceCompat.beginSection("BLUR - RenderScript");
    RenderScript rs = RenderScript.create(getContext());
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    TraceCompat.beginSection("BLUR - RenderScript Allocation");
    Allocation tmpIn = Allocation.createFromBitmap(rs, portionToBlur);
    // Fix internal trace that isn't ended
    TraceCompat.endSection();
    Allocation tmpOut = Allocation.createFromBitmap(rs, blurredBitmap);
    // Fix internal trace that isn't ended
    TraceCompat.endSection();
    TraceCompat.endSection();
    theIntrinsic.setRadius(25f);
    theIntrinsic.setInput(tmpIn);
    TraceCompat.beginSection("BLUR - RenderScript forEach");
    theIntrinsic.forEach(tmpOut);
    TraceCompat.endSection();
    TraceCompat.beginSection("BLUR - RenderScript copyTo");
    tmpOut.copyTo(blurredBitmap);
    TraceCompat.endSection();
    new Canvas(blurredBitmap).drawColor(mScrimColor);
    TraceCompat.endSection();

    // Create the new bitmap using the old plus the blurred portion and display it
    TraceCompat.beginSection("BLUR - Finalize image");
    newBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
    final Canvas canvas = new Canvas(newBitmap);
    canvas.drawBitmap(blurredBitmap, 0, y, new Paint());
    BitmapUtils.cacheBitmap(blurKey, newBitmap);
    mTextView.setBackground(null);
    mImageView.setImageBitmap(newBitmap);
    TraceCompat.endSection();
}
 
Example 16
Source File: RSRender.java    From LiveBlurListView with Apache License 2.0 4 votes vote down vote up
public RSRender(Context context) {
	rs = RenderScript.create(context);
}
 
Example 17
Source File: GlideBlurTransform.java    From GankGirl with GNU Lesser General Public License v2.1 4 votes vote down vote up
public GlideBlurTransform(Context context) {
    super( context );

    rs = RenderScript.create( context );
}
 
Example 18
Source File: BlurTransformation.java    From Music-Player with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    int sampling;
    if (this.sampling == 0) {
        sampling = ImageUtil.calculateInSampleSize(toTransform.getWidth(), toTransform.getHeight(), 100);
    } else {
        sampling = this.sampling;
    }

    int width = toTransform.getWidth();
    int height = toTransform.getHeight();
    int scaledWidth = width / sampling;
    int scaledHeight = height / sampling;

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

    Canvas canvas = new Canvas(out);
    canvas.scale(1 / (float) sampling, 1 / (float) sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(toTransform, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= 17) {
        try {
            final RenderScript rs = RenderScript.create(context.getApplicationContext());
            final Allocation input = Allocation.createFromBitmap(rs, out, 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(out);

            rs.destroy();

            return out;

        } catch (RSRuntimeException e) {
            // on some devices RenderScript.create() throws: android.support.v8.renderscript.RSRuntimeException: Error loading libRSSupport library
            if (BuildConfig.DEBUG) e.printStackTrace();
        }
    }

    return StackBlur.blur(out, blurRadius);
}
 
Example 19
Source File: StackRenderScript.java    From Blur with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private StackRenderScript(Context context){
    mRenderScript = RenderScript.create(context);
    mBlurScript = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));
}
 
Example 20
Source File: BlurTransformation.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    int sampling;
    if (this.sampling == 0) {
        sampling = ImageUtil.calculateInSampleSize(toTransform.getWidth(), toTransform.getHeight(), 100);
    } else {
        sampling = this.sampling;
    }

    int width = toTransform.getWidth();
    int height = toTransform.getHeight();
    int scaledWidth = width / sampling;
    int scaledHeight = height / sampling;

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

    Canvas canvas = new Canvas(out);
    canvas.scale(1 / (float) sampling, 1 / (float) sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(toTransform, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= 17) {
        try {
            final RenderScript rs = RenderScript.create(context.getApplicationContext());
            final Allocation input = Allocation.createFromBitmap(rs, out, 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(out);

            rs.destroy();

            return out;

        } catch (RSRuntimeException e) {
            // on some devices RenderScript.create() throws: android.support.v8.renderscript.RSRuntimeException: Error loading libRSSupport library
            if (BuildConfig.DEBUG) e.printStackTrace();
        }
    }

    return StackBlur.blur(out, blurRadius);
}