android圖片特效處理之光暈效果

來源:互聯網
上載者:User

標籤:use   chm   ret   net   color   get   log   ring   current   

這篇將講到圖片特效處理的圖片光暈效果。跟前面一樣是對像素點進行處理,本篇實現的思路可參見android影像處理系列之九--圖片特效處理之二-模糊效果和android影像處理系列之十三--圖片特效處理之六-光照效果。實現的效果是圓圈之內圖片像素點不變,圓圈之外的點做模糊處理。所以用到了模糊效果和光照效果裡面的是否是在圓圈內的演算法,可以說是上面提到的兩篇的效果的組合。

下面看:

原圖:



光暈效果看得不是很明顯,模糊強度不夠,但是還能明顯看到圖片中有一個圓圈,圈內地區要比圈外地區看得清楚一點(MM的左右臉就可以看到效果)。處理效果不是很理想,在此只能拋磚引玉。下面貼代碼:

 

[java] view plain copy
  1. /** 
  2.      * 光暈效果 
  3.      * @param bmp 
  4.      * @param x 光暈中心點在bmp中的x座標 
  5.      * @param y 光暈中心點在bmp中的y座標 
  6.      * @param r 光暈的半徑 
  7.      * @return 
  8.      */  
  9.     public Bitmap halo(Bitmap bmp, int x, int y, float r)  
  10.     {  
  11.         long start = System.currentTimeMillis();  
  12.         // 高斯矩陣  
  13.         int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };  
  14.           
  15.         int width = bmp.getWidth();  
  16.         int height = bmp.getHeight();  
  17.         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  18.           
  19.         int pixR = 0;  
  20.         int pixG = 0;  
  21.         int pixB = 0;  
  22.           
  23.         int pixColor = 0;  
  24.           
  25.         int newR = 0;  
  26.         int newG = 0;  
  27.         int newB = 0;  
  28.           
  29.         int delta = 18; // 值越小圖片會越亮,越大則越暗  
  30.           
  31.         int idx = 0;  
  32.         int[] pixels = new int[width * height];  
  33.         bmp.getPixels(pixels, 0, width, 0, 0, width, height);  
  34.         for (int i = 1, length = height - 1; i < length; i++)  
  35.         {  
  36.             for (int k = 1, len = width - 1; k < len; k++)  
  37.             {  
  38.                 idx = 0;  
  39.                 int distance = (int) (Math.pow(k - x, 2) + Math.pow(i - y, 2));  
  40.                 // 不是中心地區的點做模糊處理  
  41.                 if (distance > r * r)  
  42.                 {  
  43.                     for (int m = -1; m <= 1; m++)  
  44.                     {  
  45.                         for (int n = -1; n <= 1; n++)  
  46.                         {  
  47.                             pixColor = pixels[(i + m) * width + k + n];  
  48.                             pixR = Color.red(pixColor);  
  49.                             pixG = Color.green(pixColor);  
  50.                             pixB = Color.blue(pixColor);  
  51.                               
  52.                             newR = newR + (int) (pixR * gauss[idx]);  
  53.                             newG = newG + (int) (pixG * gauss[idx]);  
  54.                             newB = newB + (int) (pixB * gauss[idx]);  
  55.                             idx++;  
  56.                         }  
  57.                     }  
  58.                       
  59.                     newR /= delta;  
  60.                     newG /= delta;  
  61.                     newB /= delta;  
  62.                       
  63.                     newR = Math.min(255, Math.max(0, newR));  
  64.                     newG = Math.min(255, Math.max(0, newG));  
  65.                     newB = Math.min(255, Math.max(0, newB));  
  66.                       
  67.                     pixels[i * width + k] = Color.argb(255, newR, newG, newB);  
  68.                       
  69.                     newR = 0;  
  70.                     newG = 0;  
  71.                     newB = 0;  
  72.                 }  
  73.             }  
  74.         }  
  75.           
  76.         bitmap.setPixels(pixels, 0, width, 0, 0, width, height);  
  77.         long end = System.currentTimeMillis();  
  78.         Log.d("may", "used time="+(end - start));  
  79.         return bitmap;  
  80.     }  

 

android圖片特效處理之光暈效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.