圖片處理--羽化特效


float mSize = 0.5f;
  public Bitmap render(Bitmap bitmap)
  {
    if(bitmap == null)return null;

    final int SIZE = 32768;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int ratio = width >height ? height * SIZE /width : width * SIZE/height;//這里有額外*2^15 用於放大比率;之后的比率使用時需要右移15位,或者/2^15.

    int cx = width>>1;
    int cy = height>>1;
    int max = cx*cx + cy*cy;
    int min = (int)(max *(1-mSize));
    int diff= max -min;// ===>>    int diff = (int)(max * mSize);
int[] pixels = new int[width * height]; bitmap.getPixels(pixels ,0 , width , 0 , 0 , width , height); for(int i=0 ; i<height ; i++) { for(int j=0 ; j<width ; j++) { int pixel = pixels[i*width +j]; int r = (pixel & 0x00ff0000)>>16; int g = (pixel & 0x0000ff00)>>8; int b = (pixel & 0x000000ff); int dx = cx - j; int dy = cy - i; if(width > height) { dx= (dx*ratio)>>15; } else { dy = (dy * ratio)>>15; } int dstSq = dx*dx + dy*dy; float v = ((float) dstSq / diff)*255; r = (int)(r +v); g = (int)(g +v); b = (int)(b +v); r = (r>255 ? 255 : (r<0? 0 : r)); g = (g>255 ? 255 : (g<0? 0 : g)); b = (b>255 ? 255 : (b<0? 0 : b)); pixels[i*width +j] = (pixel & 0xff000000) + (r<<16)+ (g<<8) +b; } } return Bitmap.createBitmap(pixels ,width , height , Config.ARGB_8888); }
在PHOTOSHOP里,羽化就是使你選定范圍的圖邊緣達到朦朧的效果。 

羽化值越大,朦朧范圍越寬,羽化值越小,朦朧范圍越窄。可根據你想留下圖的大小來調節。
算法分析:
1、通過對rgb值增加額外的V值實現朦朧效果
2、通過控制V值的大小實現范圍控制。
3、V  = 255 * 當前點Point距中點距離的平方s1 / (頂點距中點的距離平方 *mSize)s2;
4、s1 有根據 ratio 修正 dx dy值。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM