[英]How can i find out where a BufferedImage has Alpha in Java?
我有一個BuferredImage和一個boolean [] []數組。 我想將數組設置為true,其中圖像是完全透明的。
就像是:
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)方法,但我不確定它是否包含alpha值或如何提取它。
誰能幫我? 謝謝!
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);
}
}
嘗試這個:
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;
}
}
}
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.