use of android.support.v8.renderscript.RenderScript in project BlurDialogFragment by tvbarthel.
the class RenderScriptBlurHelper method doBlur.
/**
* 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;
}
use of android.support.v8.renderscript.RenderScript in project ListenerMusicPlayer by hefuyicoder.
the class ImageUtil method createBlurredImageFromBitmap.
public static Drawable createBlurredImageFromBitmap(Bitmap bitmap, Context context, int inSampleSize) {
RenderScript rs = RenderScript.create(context);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options);
final android.support.v8.renderscript.Allocation input = android.support.v8.renderscript.Allocation.createFromBitmap(rs, blurTemplate);
final android.support.v8.renderscript.Allocation output = android.support.v8.renderscript.Allocation.createTyped(rs, input.getType());
final android.support.v8.renderscript.ScriptIntrinsicBlur script = android.support.v8.renderscript.ScriptIntrinsicBlur.create(rs, android.support.v8.renderscript.Element.U8_4(rs));
script.setRadius(4f);
script.setInput(input);
script.forEach(output);
output.copyTo(blurTemplate);
return new BitmapDrawable(context.getResources(), blurTemplate);
}
use of android.support.v8.renderscript.RenderScript in project T-MVP by north2016.
the class BlurTransformation method transform.
/**
* Transforms the given {@link Bitmap} based on the given dimensions and returns the transformed
* result.
* <p/>
* <p>
* The provided Bitmap, toTransform, should not be recycled or returned to the pool. Glide will automatically
* recycle and/or reuse toTransform if the transformation returns a different Bitmap. Similarly implementations
* should never recycle or return Bitmaps that are returned as the result of this method. Recycling or returning
* the provided and/or the returned Bitmap to the pool will lead to a variety of runtime exceptions and drawing
* errors. See #408 for an example. If the implementation obtains and discards intermediate Bitmaps, they may
* safely be returned to the BitmapPool and/or recycled.
* </p>
* <p/>
* <p>
* outWidth and outHeight will never be {@link Target#SIZE_ORIGINAL}, this
* class converts them to be the size of the Bitmap we're going to transform before calling this method.
* </p>
*
* @param pool A {@link BitmapPool} that can be used to obtain and
* return intermediate {@link Bitmap}s used in this transformation. For every
* {@link Bitmap} obtained from the pool during this transformation, a
* {@link Bitmap} must also be returned.
* @param toTransform The {@link Bitmap} to transform.
* @param outWidth The ideal width of the transformed bitmap (the transformed width does not need to match exactly).
* @param outHeight The ideal height of the transformed bitmap (the transformed heightdoes not need to match
*/
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
boolean needScaled = mSampling == DEFAULT_SAMPLING;
int originWidth = toTransform.getWidth();
int originHeight = toTransform.getHeight();
int width, height;
if (needScaled) {
width = originWidth;
height = originHeight;
} else {
width = (int) (originWidth / mSampling);
height = (int) (originHeight / mSampling);
}
//find a re-use bitmap
Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
if (mSampling != DEFAULT_SAMPLING) {
canvas.scale(1 / mSampling, 1 / mSampling);
}
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
PorterDuffColorFilter filter = new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP);
paint.setColorFilter(filter);
canvas.drawBitmap(toTransform, 0, 0, paint);
// TIPS: Glide will take care of returning our original Bitmap to the BitmapPool for us,
// we needn't to recycle it.
// toTransform.recycle(); <--- Just for tips. by Ligboy
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();
if (needScaled) {
return bitmap;
} else {
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, originWidth, originHeight, true);
bitmap.recycle();
return scaled;
}
}
use of android.support.v8.renderscript.RenderScript in project remusic by aa112901.
the class ImageUtils method createBlurredImageFromBitmap.
public static Drawable createBlurredImageFromBitmap(Bitmap bitmap, Context context, int inSampleSize) {
RenderScript rs = RenderScript.create(context);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options);
final android.support.v8.renderscript.Allocation input = android.support.v8.renderscript.Allocation.createFromBitmap(rs, blurTemplate);
final android.support.v8.renderscript.Allocation output = android.support.v8.renderscript.Allocation.createTyped(rs, input.getType());
final android.support.v8.renderscript.ScriptIntrinsicBlur script = android.support.v8.renderscript.ScriptIntrinsicBlur.create(rs, android.support.v8.renderscript.Element.U8_4(rs));
script.setRadius(8f);
script.setInput(input);
script.forEach(output);
output.copyTo(blurTemplate);
return new BitmapDrawable(context.getResources(), blurTemplate);
}
use of android.support.v8.renderscript.RenderScript in project Timber by naman14.
the class ImageUtils method createBlurredImageFromBitmap.
public static Drawable createBlurredImageFromBitmap(Bitmap bitmap, Context context, int inSampleSize) {
RenderScript rs = RenderScript.create(context);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options);
final android.support.v8.renderscript.Allocation input = android.support.v8.renderscript.Allocation.createFromBitmap(rs, blurTemplate);
final android.support.v8.renderscript.Allocation output = android.support.v8.renderscript.Allocation.createTyped(rs, input.getType());
final android.support.v8.renderscript.ScriptIntrinsicBlur script = android.support.v8.renderscript.ScriptIntrinsicBlur.create(rs, android.support.v8.renderscript.Element.U8_4(rs));
script.setRadius(8f);
script.setInput(input);
script.forEach(output);
output.copyTo(blurTemplate);
return new BitmapDrawable(context.getResources(), blurTemplate);
}
Aggregations