Java tutorial
//package com.java2s; /** * Copyright (c) 2015 Dennis Lang (LanDen Labs) landenlabs@gmail.com * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the * following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial * portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @author Dennis Lang (3/21/2015) * @see http://landenlabs.com * */ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.renderscript.Allocation; import android.renderscript.Element; import android.renderscript.RSRuntimeException; import android.renderscript.RenderScript; import android.renderscript.ScriptIntrinsicBlur; public class Main { /** * Create bitmap with blur effect. * * @param context * application context * @param resId * bitmap resource id * @param radius * the radius of the blur. Supported range 0 < radius <= 25 * @return bitmap that has been added blur effect */ public static Bitmap createBlurBitmap(Context context, int resId, int radius) { return addBlurEffect(context, BitmapFactory.decodeResource(context.getResources(), resId), radius); } /** * Create bitmap with blur effect. * * @param context * application context * @param bitmap * PNG bitmap to convert * @param radius * the radius of the blur. Supported range 0 < radius <= 25 * @return bitmap that has been added blur effect */ public static Bitmap createBlurBitmap(Context context, Bitmap bitmap, int radius) { try { if (radius <= 25) { return addBlurEffect(context, bitmap, radius); } else { Bitmap blurred = bitmap; int pass = radius / 8; for (int idx = 0; idx < pass; idx++) { blurred = addBlurEffect(context, blurred, 8); } return blurred; } } catch (RSRuntimeException rsex) { rsex.printStackTrace(); return bitmap; } } /** * Create bitmap with blur effect. Use support render script. * * @param context * application context * @param bitmap * PNG bitmap to convert * @param radius * the radius of the blur. Supported range 0 < radius <= 25 * @return bitmap that has been added blur effect */ private static Bitmap addBlurEffect(Context context, Bitmap bitmap, int radius) { if ((radius < 0) || (radius > 25)) { throw new IllegalArgumentException("Blur radius must be in range [0,25]"); } if (null == bitmap) { return null; } /** * Support render script library has bug with blur effect on down * versions than <code>Build.VERSION_CODES.JELLY_BEAN</code>. To fix * this bug need convert bitmap format to * <code>Bitmap.Config.ARGB_8888</code>. */ if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN) { bitmap = convertBitmapFormatToARGB888(bitmap); } Bitmap blurBitmap = bitmap.copy(bitmap.getConfig(), true); 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(blurBitmap); return blurBitmap; } /** * Convert bitmap format to <code>Bitmap.Config.ARGB_8888</code>. * * @param img * JPEG bitmap to convert * @return {@link Bitmap} in ARGB8888 format */ public static Bitmap convertBitmapFormatToARGB888(Bitmap img) { int numPixels = img.getWidth() * img.getHeight(); int[] pixels = new int[numPixels]; // Get JPEG pixels. Each int is the color values for one pixel. img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight()); // Create a Bitmap of the appropriate format. Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888); // Set RGB pixels. result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight()); return result; } }