小记:高斯模糊的方法类。

  1 package org.loader.liteplayer.ui;
  2
  3 import org.loader.liteplayer.application.App;
  4
  5 import android.graphics.Bitmap;
  6 import android.graphics.Canvas;
  7 import android.graphics.Matrix;
  8 import android.graphics.Paint;
  9 import android.graphics.drawable.shapes.Shape;
 10
 11 /**
 12  * liteplayer by loader
 13  * @author qibin
 14  */
 15 public class PlayBgShape extends Shape {
 16     /** 水平方向模糊度 */
 17     private static float hRadius = 5;
 18     /** 竖直方向模糊度 */
 19     private static float vRadius = 5;
 20     /** 模糊迭代度 */
 21     private static int iterations = 5;
 22
 23     private Bitmap mBitmap;
 24
 25     public PlayBgShape(Bitmap bmp) {
 26         mBitmap = boxBlurFilter(bmp);
 27         mBitmap = Bitmap.createScaledBitmap(mBitmap, App.sScreenWidth, App.sScreenHeight + 50, true);
 28     }
 29
 30     /**
 31      * 高斯模糊
 32      */
 33     public Bitmap boxBlurFilter(Bitmap bmp) {
 34         int width = bmp.getWidth();
 35         int height = bmp.getHeight();
 36         int[] inPixels = new int[width * height];
 37         int[] outPixels = new int[width * height];
 38         Bitmap bitmap = Bitmap.createBitmap(width, height,
 39                 Bitmap.Config.ARGB_8888);
 40         bmp.getPixels(inPixels, 0, width, 0, 0, width, height);
 41         for (int i = 0; i < iterations; i++) {
 42             blur(inPixels, outPixels, width, height, hRadius);
 43             blur(outPixels, inPixels, height, width, vRadius);
 44         }
 45         blurFractional(inPixels, outPixels, width, height, hRadius);
 46         blurFractional(outPixels, inPixels, height, width, vRadius);
 47         bitmap.setPixels(inPixels, 0, width, 0, 0, width, height);
 48         return bitmap;
 49     }
 50
 51     public void blur(int[] in, int[] out, int width, int height, float radius) {
 52         int widthMinus1 = width - 1;
 53         int r = (int) radius;
 54         int tableSize = 2 * r + 1;
 55         int divide[] = new int[256 * tableSize];
 56
 57         for (int i = 0; i < 256 * tableSize; i++)
 58             divide[i] = i / tableSize;
 59
 60         int inIndex = 0;
 61
 62         for (int y = 0; y < height; y++) {
 63             int outIndex = y;
 64             int ta = 0, tr = 0, tg = 0, tb = 0;
 65
 66             for (int i = -r; i <= r; i++) {
 67                 int rgb = in[inIndex + clamp(i, 0, width - 1)];
 68                 ta += (rgb >> 24) & 0xff;
 69                 tr += (rgb >> 16) & 0xff;
 70                 tg += (rgb >> 8) & 0xff;
 71                 tb += rgb & 0xff;
 72             }
 73
 74             for (int x = 0; x < width; x++) {
 75                 out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16)
 76                         | (divide[tg] << 8) | divide[tb];
 77
 78                 int i1 = x + r + 1;
 79                 if (i1 > widthMinus1)
 80                     i1 = widthMinus1;
 81                 int i2 = x - r;
 82                 if (i2 < 0)
 83                     i2 = 0;
 84                 int rgb1 = in[inIndex + i1];
 85                 int rgb2 = in[inIndex + i2];
 86
 87                 ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff);
 88                 tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16;
 89                 tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8;
 90                 tb += (rgb1 & 0xff) - (rgb2 & 0xff);
 91                 outIndex += height;
 92             }
 93             inIndex += width;
 94         }
 95     }
 96
 97     public void blurFractional(int[] in, int[] out, int width, int height,
 98             float radius) {
 99         radius -= (int) radius;
100         float f = 1.0f / (1 + 2 * radius);
101         int inIndex = 0;
102
103         for (int y = 0; y < height; y++) {
104             int outIndex = y;
105
106             out[outIndex] = in[0];
107             outIndex += height;
108             for (int x = 1; x < width - 1; x++) {
109                 int i = inIndex + x;
110                 int rgb1 = in[i - 1];
111                 int rgb2 = in[i];
112                 int rgb3 = in[i + 1];
113
114                 int a1 = (rgb1 >> 24) & 0xff;
115                 int r1 = (rgb1 >> 16) & 0xff;
116                 int g1 = (rgb1 >> 8) & 0xff;
117                 int b1 = rgb1 & 0xff;
118                 int a2 = (rgb2 >> 24) & 0xff;
119                 int r2 = (rgb2 >> 16) & 0xff;
120                 int g2 = (rgb2 >> 8) & 0xff;
121                 int b2 = rgb2 & 0xff;
122                 int a3 = (rgb3 >> 24) & 0xff;
123                 int r3 = (rgb3 >> 16) & 0xff;
124                 int g3 = (rgb3 >> 8) & 0xff;
125                 int b3 = rgb3 & 0xff;
126                 a1 = a2 + (int) ((a1 + a3) * radius);
127                 r1 = r2 + (int) ((r1 + r3) * radius);
128                 g1 = g2 + (int) ((g1 + g3) * radius);
129                 b1 = b2 + (int) ((b1 + b3) * radius);
130                 a1 *= f;
131                 r1 *= f;
132                 g1 *= f;
133                 b1 *= f;
134                 out[outIndex] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;
135                 outIndex += height;
136             }
137             out[outIndex] = in[width - 1];
138             inIndex += width;
139         }
140     }
141
142     public int clamp(int x, int a, int b) {
143         return (x < a) ? a : (x > b) ? b : x;
144     }
145
146     @Override
147     public void draw(Canvas canvas, Paint paint) {
148 //        Matrix matrix = new Matrix();
149 //        matrix.postScale(App.sScreenWidth / mBitmap.getWidth(), App.sScreenHeight / mBitmap.getHeight());
150         canvas.drawBitmap(mBitmap, new Matrix(), paint);
151         canvas.drawARGB(150, 128, 128, 128);
152     }
153 }
时间: 2024-08-16 03:14:39

小记:高斯模糊的方法类。的相关文章

IOS上传图片方法类

IOS上传图片方法类 iPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下 // // RequestPostUploadHelper.h // demodes // // Created by 张浩 on 13-5-8. // Copyright (c) 2013年 张浩. All rights reserved. // #import <Foundation/Foundation.h> @interface RequestPostUploadHelper

iOS方法类:CGAffineTransform

iOS方法类:CGAffineTransform的使用大概 CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放.旋转和平移操作: 另称放射变换矩阵,可参照线性代数的矩阵实现方式0.0 这里附上的CGAffineTransform官方文档: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGAffineTr

idea live template高级知识, 进阶(给方法,类,js方法添加注释)

为了解决用一个命令(宏)给方法,类,js方法添加注释,经过几天的研究.终于得到结果了. 实现的效果如下: 给Java中的method添加方法: /** * * @Method : addMenu * @Description : * @param menu : * @return : cn.yiyizuche.common.base.ResultMsg * @author : Rush.D.Xzj * @CreateDate : 2017-06-12 星期一 18:17:42 * */publi

C#导出数据到Excel通用的方法类

导出数据到Excel通用的方法类,请应对需求自行修改. 资源下载列表 using System.Data; using System.IO; namespace IM.Common.Tools { public class Export { public string Encoding = "UTF-8"; System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; public void Ec

php操作oracle的方法类集全

在网上开始找php中操作oracle的方法类~ 果然找到一个用php+oracle制作email表以及插入查询的教程,赶忙点开来看,从头到尾仔细的看了一遍,还没开始操作,便觉得收获很大了.地址在此:http://www.alixixi.com/program/a/2008050731615.shtml#replay. http://blog.163.com/[email protected]/blog/static/27712393201131815035122/        (博客校园) 摘

使用maven打包添加带有主方法类的信息

一,准备工具 1,请确定在此之前你所有的Java配置以及maven都已经正确配置 二,说明 当我们初次使用maven时,我们在pom.xml所在的目录(也就是项目的根目录)执行 mvn clean package 进行打包项目时,我们在项目输出目录(target)会看到我们指定类型的包,一般来说 maven 默认是 Jar 包,这个时候你 cmd 到该目录,执行 java -jar your-jar-pakage-name.jar,会出现找不到主方法入口的错误: 此时maven告诉我们找不到主方

struts2中取得session的公共方法类

该公共方法类一般建立在com.xxx.util包下面 package com.dgh.util; import java.util.Map; import org.apache.struts2.interceptor.SessionAware; /** * * 获得session * @author wangcunhuazi */ public class BaseAction implements SessionAware { protected Map<String,Object> ses

链式编程:泛型实现的扩展方法类

序言 本文分享一个用链式编程思想和泛型实现的扩展方法类,用于减少代码量,并提供更为清晰的业务逻辑代码表达. 概念 链式编程:将多个业务逻辑(方法)通过“.”(点号)串联起来的一种代码风格,形似链条,故称链式编程.核心思想在于每个方法均返回自身实例. 泛型:可以理解为是一个类的“篮子“”,只要符合约束的类均可以放置在该“篮子”里面. 扩展方法:向现有类添加方法. 根据泛型和扩展方法的特点,泛型+扩展方法实现了向所有符合约束的“类”添加方法,可减少重复代码量. (.Net语言提供了这么优雅的特性,刚

工程中properties文件处理方法类

功能:properties文件加载.取得key对应的值 1 import java.util.ResourceBundle; 2 3 /** 4 * 工程中properties文件处理方法类 5 */ 6 public class ConfigHolder { 7 private static ResourceBundle bundle; 8 9 /** 10 * 加载properties文件 11 */ 12 private static void loadConfig() { 13 if(b