android圖片特效處理之模糊效果
阿新 • • 發佈:2019-02-18
這篇將講到圖片特效處理的模糊效果。跟前面一樣是對畫素點進行處理,演算法是通用的,但耗時會更長,至於為什麼,看了下面的程式碼你就會明白。
E畫素點的GB值類似,delta的取值貌似沒有規定值,可以自己設定任意值,但要想達到效果,能設的值很少,下面圖片是值為16的效果。
處理效果:
演算法:
一、簡單演算法:將畫素點周圍八個點包括自身一共九個點的RGB值分別相加後平均,作為當前畫素點的RGB值,即可實現效果。
舉例:
ABC
DEF
GHI
假如當前點是E,那麼會有:
[java] view plaincopyprint?- E.r = (A.r + B.r + C.r + D.r + E.r + F.r + G.r + H.r + I.r) /9// r表示的是E畫素點RGB值的R值
E畫素點的GB值類似。
二、採用高斯模糊:
高斯矩陣:
[java] view plaincopyprint?- int[] gauss = newint[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
舉例:(還是上面的九個點)
假如當前點是E,那麼會有:
- int delta = 16;
- E.r =( A.r * gauss[0
處理效果:
原圖片:
處理後:
兩種處理方式的程式碼:
[java] view plaincopyprint?- /**
- * 模糊效果
- * @param bmp
- * @return
- */
- private Bitmap blurImage(Bitmap bmp)
- {
- int width = bmp.getWidth();
- int height = bmp.getHeight();
- Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
- int pixColor = 0;
- int newR = 0;
- int newG = 0;
- int newB = 0;
- int newColor = 0;
- int[][] colors = newint[9][3];
- for (int i = 1, length = width - 1; i < length; i++)
- {
- for (int k = 1, len = height - 1; k < len; k++)
- {
- for (int m = 0; m < 9; m++)
- {
- int s = 0;
- int p = 0;
- switch(m)
- {
- case0:
- s = i - 1;
- p = k - 1;
- break;
- case1:
- s = i;
- p = k - 1;
- break;
- case2:
- s = i + 1;
- p = k - 1;
- break;
- case3:
- s = i + 1;
- p = k;
- break;
- case4:
- s = i + 1;
- p = k + 1;
- break;
- case5:
- s = i;
- p = k + 1;
- break;
- case6:
- s = i - 1;
- p = k + 1;
- break;
- case7:
- s = i - 1;
- p = k;
- break;
- case8:
- s = i;
- p = k;
- }
- pixColor = bmp.getPixel(s, p);
- colors[m][0] = Color.red(pixColor);
- colors[m][1] = Color.green(pixColor);
- colors[m][2] = Color.blue(pixColor);
- }
- for (int m = 0; m < 9; m++)
- {
- newR += colors[m][0];
- newG += colors[m][1];
- newB += colors[m][2];
- }
- newR = (int) (newR / 9F);
- newG = (int) (newG / 9F);
- newB = (int) (newB / 9F);
- newR = Math.min(255, Math.max(0, newR));
- newG = Math.min(255, Math.max(0, newG));
- newB = Math.min(255, Math.max(0, newB));
- newColor = Color.argb(255, newR, newG, newB);
- bitmap.setPixel(i, k, newColor);
- newR = 0;
- newG = 0;
- newB = 0;
- }
- }
- return bitmap;
- }
- /**
- * 柔化效果(高斯模糊)(優化後比上面快三倍)
- * @param bmp
- * @return
- */
- private Bitmap blurImageAmeliorate(Bitmap bmp)
- {
- long start = System.currentTimeMillis();
- // 高斯矩陣
- int[] gauss = newint[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
- int width = bmp.getWidth();
- int height = bmp.getHeight();
- Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
- int pixR = 0;
- int pixG = 0;
- int pixB = 0;
- int pixColor = 0;
- int newR = 0;
- int newG = 0;
- int newB = 0;
- int delta = 16; // 值越小圖片會越亮,越大則越暗
- int idx = 0;
- int[] pixels = newint[width * height];
- bmp.getPixels(pixels, 0, width, 0, 0, width, height);
- for (int i = 1, length = height - 1; i < length; i++)
- {
- for (int k = 1, len = width - 1; k < len; k++)
- {
- idx = 0;
- for (int m = -1; m <= 1; m++)
- {
- for (int n = -1; n <= 1; n++)
- {
- pixColor = pixels[(i + m) * width + k + n];
- pixR = Color.red(pixColor);
- pixG = Color.green(pixColor);
- pixB = Color.blue(pixColor);
- newR = newR + (int) (pixR * gauss[idx]);
- newG = newG + (int) (pixG * gauss[idx]);
- newB = newB + (int) (pixB * gauss[idx]);
- idx++;
- }
- }
- newR /= delta;
- newG /= delta;
- newB /= delta;
- newR = Math.min(255, Math.max(0, newR));
- newG = Math.min(255, Math.max(0, newG));
- newB = Math.min(255, Math.max(0, newB));
- pixels[i * width + k] = Color.argb(255, newR, newG, newB);
- newR = 0;
- newG = 0;
- newB = 0;
- }
- }
- bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
- long end = System.currentTimeMillis();
- Log.d("may", "used time="+(end - start));
- return bitmap;
- }
在優化後的程式碼中要注意了,pixels陣列不能超過規定的大小,也就是說圖片的尺寸不能太大,否則會棧記憶體溢位。