2012-05-02 1 views
6

저는 BuferredImage와 부울 [] [] 배열을 가지고 있습니다. 이미지가 완전히 투명 할 때 배열을 true로 설정하려고합니다. 같은BufferedImage에서 Java로 알파가있는 위치를 어떻게 알 수 있습니까?

뭔가 :

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

그러나 getAlpha (x, y)의 방법이 존재하지 않는

가, 그리고 내가 사용할 수있는 뭔가를 찾을 수 없습니다. getRGB (x, y) 메서드가 있지만 알파 값이 포함되어 있는지 또는 추출 방법이 확실하지 않습니다.

아무도 도와 줄 수 있습니까? 감사합니다.

+0

이 질문에 도움이 될 수 : http://stackoverflow.com/questions/221830/set-bufferedimage-alpha-mask- in-java –

답변

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

이것은 깨끗하고 효율적이지만이 함수의 논리는 거꾸로되어 있습니다. [색상] (http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html)의 javadoc에 따르면 "1.0 또는 255의 알파 값은 색상이 완전하다는 것을 의미합니다. 불투명하고 0 또는 0.0의 알파 값은 색상이 완전히 투명 함을 의미합니다. " 알파 비트가 255이면 픽셀이 불투명하다는 것을 의미하는 경우이 함수는 true를 반환합니다. – Fr33dan

2

이 시도 :

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 
}