2011-11-25 78 views
4

我有一個方法與透明度位圖替換特定的顏色與透明度

public Bitmap createTransparentBitmapFromBitmap(Bitmap bitmap, 
      int replaceThisColor) { 
     if (bitmap != null) { 
      int picw = bitmap.getWidth(); 
      int pich = bitmap.getHeight(); 
      int[] pix = new int[picw * pich]; 
      bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich); 

      int sr = (replaceThisColor >> 16) & 0xff; 
      int sg = (replaceThisColor >> 8) & 0xff; 
      int sb = replaceThisColor & 0xff; 

      for (int y = 0; y < pich; y++) { 
      for (int x = 0; x < picw; x++) { 
       int index = y * picw + x; 
      /* int r = (pix[index] >> 16) & 0xff; 
       int g = (pix[index] >> 8) & 0xff; 
       int b = pix[index] & 0xff;*/ 

       if (pix[index] == replaceThisColor) { 

       if(x<topLeftHole.x) topLeftHole.x = x; 
       if(y<topLeftHole.y) topLeftHole.y = y; 
       if(x>bottomRightHole.x) bottomRightHole.x = x; 
       if(y>bottomRightHole.y)bottomRightHole.y = y; 

       pix[index] = Color.TRANSPARENT; 
       } else { 
       //break; 
       } 
      } 
      } 

      Bitmap bm = Bitmap.createBitmap(pix, picw, pich, 
       Bitmap.Config.ARGB_8888); 

      return bm; 
     } 
     return null; 
     } 

其所謂像這樣

backgroundBitmap = createTransparentBitmapFromBitmap(backgroundBitmap , Color.argb(255,255,255, 0)); 

更換一種顏色的像素相同的顏色我有一個PNG文件,我想要透明的洞。問題是它只取代部分顏色不是全部。 查看截圖https://docs.google.com/document/d/18aH43sFmsuuRu0QNfMTD1zek8sqWwH_pTauFofDZeIw/edit

+0

鏈接給我一個未命名的文檔。你確定你已經建立了共享和不正確的? –

+0

我剛剛編輯它,現在應該是好的 – tsukimi

回答

4

它看起來像你的圖片中有JPEG文物。如果僅以無損格式保存圖片,檢測確切的顏色纔會真正起作用。 PNG是一種無損格式,但是在繪製圓後,您是否將圖片的中間版本保存爲JPEG(或其他有損格式)?

另外;只是一個小竅門:你不需要兩個循環,只需要在一個for循環中遍歷數組。

編輯:這個新的黃色看起來像是由反鋸齒造成的。由於這個原因,圓的邊緣並不是你正在尋找的確切顏色,你的代碼會錯過它們。解決此問題的唯一方法是在繪製圓時關閉消除鋸齒功能。當然,這樣你的透明孔也不會得到很好的抗鋸齒邊緣。

如果你想這樣做,你可能不得不使用一個單獨的孔掩模(一個JPEG的顏色和一個8位PNG的透明應該是一個相當有效的組合 - 哦,我多麼希望有一個

+0

馬蒂,謝謝你的回覆。該圖像是一個.jpg,我現在保存爲.png,透明度更好。它仍然有一些黃色顯示,我更新了谷歌文檔,最後一張圖片。無論如何要改善,所以沒有或少黃色顯示。 – tsukimi

+0

也許我可以對這個顏色+10或其他東西有一些寬容。我不是真的遵循你的面具建議,你可以給更多的細節,平面設計不是我的強項。 – tsukimi

+0

@tsukimi:雖然 –

2

非常好,我需要這個,在Web瀏覽器隨時都可這種圖像格式),那麼除了使用我改進了一下,代碼:

public static Bitmap repleceIntervalColor(Bitmap bitmap,int redStart,int redEnd,int greenStart, int greenEnd,int blueStart, int blueEnd,int colorNew) { 
    if (bitmap != null) { 
     int picw = bitmap.getWidth(); 
     int pich = bitmap.getHeight(); 
     int[] pix = new int[picw * pich]; 
     bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich); 
     for (int y = 0; y < pich; y++) { 
      for (int x = 0; x < picw; x++) { 
       int index = y * picw + x; 
        if (
         ((Color.red(pix[index]) >= redStart)&&(Color.red(pix[index]) <= redEnd))&& 
         ((Color.green(pix[index]) >= greenStart)&&(Color.green(pix[index]) <= greenEnd))&& 
         ((Color.blue(pix[index]) >= blueStart)&&(Color.blue(pix[index]) <= blueEnd)) 
        ){ 
         pix[index] = colorNew; 
        } 
       } 
      } 
     Bitmap bm = Bitmap.createBitmap(pix, picw, pich,Bitmap.Config.ARGB_8888); 
     return bm; 
    } 
    return null; 
} 
+1

它會增加找到足夠淡黃色的雜散像素的機率,它也是移除選定顏色和快速移動的最佳解決方案。非常感謝 。 – deepti