码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-02-18 17:41:06      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

  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 }

 

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

标签:

原文地址:http://www.cnblogs.com/labixiaoxin/p/5198698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!