Android一秒实现高斯模糊特效马赛克特技

很多时候我们深夜看些电影 偶尔会看些这样的。

充满马赛克的电影,当你云雨一番的时候,看着旁边的卫生纸的时候,有没有想安卓能不能实现类似这样的功能呢。

下面我就带大家来做这种模糊功能类似马赛克。

首先做这个之前,我们需要先找一张让人脸红的照片来给他打上马赛克。

 下面我们就要辣手摧花了。

首先我们先把 一个高斯模糊 算法工具类 导入,置于这个算法是怎么算的,我不懂,你们也别浪费时间,反正也不搞不懂。

FastBlur.java

package com.example.gaosi;


import android.graphics.Bitmap;

/**
 * 高斯模糊。图片背景变成毛玻璃
 */
public class FastBlur {

    public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {

        // Stack Blur v1.0 from
        // http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
        //
        // Java Author: Mario Klingemann <mario at="" quasimondo.com="">
        // http://incubator.quasimondo.com
        // created Feburary 29, 2004
        // Android port : Yahel Bouaziz <yahel at="" kayenko.com="">
        // http://www.kayenko.com
        // ported april 5th, 2012

        // This is a compromise between Gaussian Blur and Box blur
        // It creates much better looking blurs than Box Blur, but is
        // 7x faster than my Gaussian Blur implementation.
        //
        // I called it Stack Blur because this describes best how this
        // filter works internally: it creates a kind of moving stack
        // of colors whilst scanning through the image. Thereby it
        // just has to add one new block of color to the right side
        // of the stack and remove the leftmost color. The remaining
        // colors on the topmost layer of the stack are either added on
        // or reduced by one, depending on if they are on the right or
        // on the left side of the stack.
        //
        // If you are using this algorithm in your code please add
        // the following line:
        //
        // Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>

        Bitmap bitmap;
        if (canReuseInBitmap) {
            bitmap = sentBitmap;
        } else {
            bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
        }

        if (radius < 1) {
            return (null);
        }

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        int[] pix = new int[w * h];
        bitmap.getPixels(pix, 0, w, 0, 0, w, h);

        int wm = w - 1;
        int hm = h - 1;
        int wh = w * h;
        int div
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android中可以使用RenderScript库实现高斯模糊效果,同时结合GradientDrawable实现渐变效果。具体实现步骤如下: 1.在build.gradle文件中添加RenderScript支持: ``` defaultConfig { // ... renderscriptTargetApi 19 renderscriptSupportModeEnabled true } ``` 2.创建RenderScript对象: ``` RenderScript rs = RenderScript.create(context); ``` 3.创建高斯模糊脚本: ``` ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); ``` 4.设置高斯模糊半径: ``` blurScript.setRadius(radius); ``` 其中radius是半径值,越大模糊效果越强。 5.将Bitmap转换为RenderScript的Allocation对象: ``` Allocation input = Allocation.createFromBitmap(rs, bitmap); ``` 6.创建一个输出Allocation对象: ``` Allocation output = Allocation.createTyped(rs, input.getType()); ``` 7.在RenderScript中处理图像: ``` blurScript.setInput(input); blurScript.forEach(output); ``` 8.将处理后的图像从Allocation对象中复制到Bitmap中: ``` output.copyTo(bitmap); ``` 9.使用GradientDrawable创建渐变背景: ``` GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {startColor, endColor}); ``` 其中startColor和endColor是起始和结束颜色。 10.将Bitmap设置为View的背景: ``` view.setBackground(new BitmapDrawable(context.getResources(), bitmap)); ``` 完整的代码示例: ``` RenderScript rs = RenderScript.create(context); ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); blurScript.setRadius(radius); Allocation input = Allocation.createFromBitmap(rs, bitmap); Allocation output = Allocation.createTyped(rs, input.getType()); blurScript.setInput(input); blurScript.forEach(output); output.copyTo(bitmap); GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {startColor, endColor}); view.setBackground(new BitmapDrawable(context.getResources(), bitmap)); ``` 注意:以上代码仅为示例,具体实现可能需要根据项目需求进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值