2013-05-22 56 views
2

我正在寫一些函數在位圖上添加噪聲效果。我發現類似的問題:Add noise effect to a drawingandroid噪聲對位圖的影響

Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);

BitmapShader shader = new BitmapShader (bitmap, TileMode.REPEAT, TileMode.REPEAT); 

    Paint paint = new Paint(); 
    paint.setShader(shader); 

    Canvas c = new Canvas(outputBitmap); 
    c.drawBitmap(bitmap, 0, 0, paint); 

我應該如何添加顏色過濾器來獲得這樣的結果?你能提供簡單的代碼嗎?

回答

3

我建議使用這段代碼。

public static final int COLOR_MIN = 0x00; 
public static final int COLOR_MAX = 0xFF; 

public static Bitmap applyFleaEffect(Bitmap source) { 
    // get image size 
    int width = source.getWidth(); 
    int height = source.getHeight(); 
    int[] pixels = new int[width * height]; 
    // get pixel array from source 
    source.getPixels(pixels, 0, width, 0, 0, width, height); 
    // a random object 
    Random random = new Random(); 

    int index = 0; 
    // iteration through pixels 
    for(int y = 0; y < height; ++y) { 
     for(int x = 0; x < width; ++x) { 
      // get current index in 2D-matrix 
      index = y * width + x; 
      // get random color 
      int randColor = Color.rgb(random.nextInt(COLOR_MAX), 
        random.nextInt(COLOR_MAX), random.nextInt(COLOR_MAX)); 
      // OR 
      pixels[index] |= randColor; 
     } 
    } 
    // output bitmap 
    Bitmap bmOut = Bitmap.createBitmap(width, height, source.getConfig()); 
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height); 
    return bmOut; 
} 

歡迎。

+1

感謝此代碼 – roomtek

+0

@roomtek歡迎兄弟.. –