2012-05-02 16 views
6

Mam tablicę BuferredImage i boolean [] []. Chcę ustawić tablicę na true tam, gdzie obraz jest całkowicie przezroczysty.Jak mogę się dowiedzieć, gdzie BufferedImage ma Alpha w Javie?

Coś jak:

for(int x = 0; x < width; x++) { 
    for(int y = 0; y < height; y++) { 
     alphaArray[x][y] = bufferedImage.getAlpha(x, y) == 0; 
    } 
} 

Ale metoda metoda getAlpha (x, y) nie istnieje, i nie mogę znaleźć nic innego mogę użyć. Istnieje metoda getRGB (x, y), ale nie jestem pewien, czy zawiera ona wartość alfa lub jak ją wyodrębnić.

Czy ktoś może mi pomóc? Dziękujemy!

+0

To pytanie może pomóc: http://stackoverflow.com/questions/221830/set-bufferedimage-alpha-mask- in-java –

Odpowiedz

6
public static boolean isAlpha(BufferedImage image, int x, int y) 
{ 
    return image.getRBG(x, y) & 0xFF000000 == 0xFF000000; 
} 
for(int x = 0; x < width; x++) 
{ 
    for(int y = 0; y < height; y++) 
    { 
     alphaArray[x][y] = isAlpha(bufferedImage, x, y); 
    } 
} 
+0

To jest czyste i wydajne, ale logika tej funkcji jest cofnięta. Zgodnie z javadoc w [Color] (http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html) "wartość alpha 1.0 lub 255 oznacza, że ​​kolor jest całkowicie nieprzezroczystość i wartość alfa 0 lub 0.0 oznacza, że ​​kolor jest całkowicie przezroczysty. " Ta funkcja zwraca wartość true, jeśli bity alfa mają wartość 255, co oznacza, że ​​piksel jest nieprzezroczysty. – Fr33dan

2

Spróbuj tego:

Raster raster = bufferedImage.getAlphaRaster(); 
    if (raster != null) { 
     int[] alphaPixel = new int[raster.getNumBands()]; 
     for (int x = 0; x < raster.getWidth(); x++) { 
      for (int y = 0; y < raster.getHeight(); y++) { 
       raster.getPixel(x, y, alphaPixel); 
       alphaArray[x][y] = alphaPixel[0] == 0x00; 
      } 
     } 
    } 
1
public boolean isAlpha(BufferedImage image, int x, int y) { 
    Color pixel = new Color(image.getRGB(x, y), true); 
    return pixel.getAlpha() > 0; //or "== 255" if you prefer 
} 
Powiązane problemy