2011-11-21 65 views
2

我一直在嘗試在android中實現框模糊算法。 該代碼似乎很好,但試圖應用時,模糊圖像中的某些區域在模糊的照片上有大的黃色和白色污跡。 任何人都可以幫助我找出我做錯了什麼? 感謝:Android框模糊算法

這裏是我有:

public static Bitmap boxBlur(Bitmap bmp, int range) { 
    assert (range & 1) == 0 : "Range must be odd."; 

    Bitmap blurred = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), 
      Config.ARGB_8888); 
    Canvas c = new Canvas(blurred); 

    int w = bmp.getWidth(); 
    int h = bmp.getHeight(); 

    int[] pixels = new int[bmp.getWidth() * bmp.getHeight()]; 
    bmp.getPixels(pixels, 0, w, 0, 0, w, h); 

    boxBlurHorizontal(pixels, w, h, range/2); 
    boxBlurVertical(pixels, w, h, range/2); 

    c.drawBitmap(pixels, 0, w, 0.0F, 0.0F, w, h, true, null); 

    return blurred; 
} 

private static void boxBlurHorizontal(int[] pixels, int w, int h, 
     int halfRange) { 
    int index = 0; 
    int[] newColors = new int[w]; 

    for (int y = 0; y < h; y++) { 
     int hits = 0; 
     long r = 0; 
     long g = 0; 
     long b = 0; 
     for (int x = -halfRange; x < w; x++) { 
      int oldPixel = x - halfRange - 1; 
      if (oldPixel >= 0) { 
       int color = pixels[index + oldPixel]; 
       if (color != 0) { 
        r -= Color.red(color); 
        g -= Color.green(color); 
        b -= Color.blue(color); 
       } 
       hits--; 
      } 

      int newPixel = x + halfRange; 
      if (newPixel < w) { 
       int color = pixels[index + newPixel]; 
       if (color != 0) { 
        r += Color.red(color); 
        g += Color.green(color); 
        b += Color.blue(color); 
       } 
       hits++; 
      } 

      if (x >= 0) { 
       newColors[x] = Color.argb(0xFF, (byte) (r/hits), 
         (byte) (g/hits), (byte) (b/hits)); 
      } 
     } 

     for (int x = 0; x < w; x++) { 
      pixels[index + x] = newColors[x]; 
     } 

     index += w; 
    } 
} 

private static void boxBlurVertical(int[] pixels, int w, int h, 
     int halfRange) { 

    int[] newColors = new int[h]; 
    int oldPixelOffset = -(halfRange + 1) * w; 
    int newPixelOffset = (halfRange) * w; 

    for (int x = 0; x < w; x++) { 
     int hits = 0; 
     long r = 0; 
     long g = 0; 
     long b = 0; 
     int index = -halfRange * w + x; 
     for (int y = -halfRange; y < h; y++) { 
      int oldPixel = y - halfRange - 1; 
      if (oldPixel >= 0) { 
       int color = pixels[index + oldPixelOffset]; 
       if (color != 0) { 
        r -= Color.red(color); 
        g -= Color.green(color); 
        b -= Color.blue(color); 
       } 
       hits--; 
      } 

      int newPixel = y + halfRange; 
      if (newPixel < h) { 
       int color = pixels[index + newPixelOffset]; 
       if (color != 0) { 
        r += Color.red(color); 
        g += Color.green(color); 
        b += Color.blue(color); 
       } 
       hits++; 
      } 

      if (y >= 0) { 
       newColors[y] = Color.argb(0xFF, (byte) (r/hits), 
         (byte) (g/hits), (byte) (b/hits)); 
      } 

      index += w; 
     } 

     for (int y = 0; y < h; y++) { 
      pixels[y * w + x] = newColors[y]; 
     } 
    } 
} 
+0

發現問題 - 行: newColors [X] = Color.argb(0xFF時,(**字節**)(R /次點擊), (** *字節*)(g/hit),(** byte **)(b/hit)); 我將平均值轉換爲字節,它們應該是整數。 將其更改爲: newColors [x] = Color.argb(0xFF,(** int **)(r/hit), (** int **)(g/hit),(** int ** )(b/hit)); – saarraz1

+0

將它作爲答案添加可能會很好,然後接受它只是爲了解決問題。 –

回答

3

發現問題!
線:
newColors[x] = Color.argb(0xFF, (byte) (r/hits), (byte) (g/hits), (byte) (b/hits));

我轉換的平均值,以字節,其中他們應該已經整數。
它更改爲:

newColors[x] = Color.argb(0xFF, (int) (r/hits), (int) (g/hits), (int) (b/hits));