图片实现高斯玻璃效果

来源:互联网 发布:淘宝网购物压缩袋 编辑:程序博客网 时间:2024/05/17 02:45

最近做项目需要一个背景图,可是能找到的背景图都会影响美观,因此决定使用高斯玻璃效果。

上我使用的代码:

public class ImageUtil {    public  static Bitmap blurBitmap(Bitmap bitmap){        //Let's create an empty bitmap with the same size of the bitmap we want to blur        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);        //Instantiate a new Renderscript        RenderScript rs = RenderScript.create(MyApplication.getContext());        //Create an Intrinsic Blur Script using the Renderscript        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);        //Set the radius of the blur        blurScript.setRadius(25.f);        //Perform the Renderscript        blurScript.setInput(allIn);        blurScript.forEach(allOut);        //Copy the final bitmap created by the out Allocation to the outBitmap        allOut.copyTo(outBitmap);        //recycle the original bitmap        bitmap.recycle();        //After finishing everything, we destroy the Renderscript.        rs.destroy();        return outBitmap;    }}
然后使用glide框架加载。可是glide没办法加载bitmap类,所以使用了其他方法将bitmap转换成byte[]。
下面我我将我使用的一个工具类献上,这也是我从一位网友那里获得的。如果这位朋友觉得侵权了,请跟我联系,我会立即删除。
public class ImageDispose {    /**     * @param 将图片内容解析成字节数组     * @param inStream     * @return byte[]     * @throws Exception     */    public static byte[] readStream(InputStream inStream) throws Exception {        byte[] buffer = new byte[1024];        int len = -1;        ByteArrayOutputStream outStream = new ByteArrayOutputStream();        while ((len = inStream.read(buffer)) != -1) {            outStream.write(buffer, 0, len);        }        byte[] data = outStream.toByteArray();        outStream.close();        inStream.close();        return data;    }    /**     * @param 将字节数组转换为ImageView可调用的Bitmap对象     * @param bytes     * @param opts     * @return Bitmap     */    public static Bitmap getPicFromBytes(byte[] bytes,                                         BitmapFactory.Options opts) {        if (bytes != null)            if (opts != null)                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,                        opts);            else                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);        return null;    }    /**     * @param 图片缩放     * @param bitmap 对象     * @param w 要缩放的宽度     * @param h 要缩放的高度     * @return newBmp 新 Bitmap对象     */    public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){        int width = bitmap.getWidth();        int height = bitmap.getHeight();        Matrix matrix = new Matrix();        float scaleWidth = ((float) w / width);        float scaleHeight = ((float) h / height);        matrix.postScale(scaleWidth, scaleHeight);        Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,                matrix, true);        return newBmp;    }    /**     * 把Bitmap转Byte     */    public static byte[] Bitmap2Bytes(Bitmap bm){        ByteArrayOutputStream baos = new ByteArrayOutputStream();        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);        return baos.toByteArray();    }    /**     * 把字节数组保存为一个文件     */    public static File getFileFromBytes(byte[] b, String outputFile) {        BufferedOutputStream stream = null;        File file = null;        try {            file = new File(outputFile);            FileOutputStream fstream = new FileOutputStream(file);            stream = new BufferedOutputStream(fstream);            stream.write(b);        } catch (Exception e) {            e.printStackTrace();        } finally {            if (stream != null) {                try {                    stream.close();                } catch (IOException e1) {                    e1.printStackTrace();                }            }        }        return file;    }}

通过上面的步骤就可以很完美的做到这一点。给大家上一下效果图。