簡體   English   中英

Java編寫PNG文件使一切透明

[英]Java writing png file makes everything transparent

我目前正在嘗試將BufferedImage保存到png文件:

for(int x = left.getBlockX(); x < (left.getBlockX() + height); x++){
                for(int z = left.getBlockZ(); z < (left.getBlockZ() + width); z++){
                    pixels[i] = getBasicColor(new Location(left.getWorld(), x, left.getBlockY(), z));
                    i++;
                }
            }

這是getBasicColor函數:

@SuppressWarnings("deprecation")
public static int getBasicColor(Location location){
    if(location.getBlock().getType().equals(Material.WOOL)){
        Byte data = location.getBlock().getData();
        for(BasicColor basicColor : BasicColor.values()){   
            if(data.equals(basicColor.getDyeColor().getData())){
                int rgb = 65536 * basicColor.getRed() + 256 * basicColor.getGreen() + basicColor.getBlue();
                System.out.println(rgb);
                return rgb;
            }
        }
    }
    return 0;
}

這是BasicColors:

public enum BasicColor {

WHITE (255,255,255, DyeColor.WHITE),
BLACK (0,0,0, DyeColor.BLACK),
BLUE (0,0,255, DyeColor.BLUE),
CYAN (0, 255, 255, DyeColor.CYAN),
DARK_GRAY (169,169,169, DyeColor.GRAY),
GRAY (128,128,128, DyeColor.GRAY),
SILVER (192,192,192, DyeColor.SILVER),
GREEN (0,128,0, DyeColor.GREEN),
MAGENTA (255,0,255, DyeColor.MAGENTA),
ORANGE (255, 165, 0, DyeColor.ORANGE),
PINK (255,192,203, DyeColor.PINK),
RED (255, 0, 0, DyeColor.RED),
YELLOW (255,255,0, DyeColor.YELLOW);

private final int red, blue, green;
private final DyeColor color;

private BasicColor(int red, int green, int blue, DyeColor color){
    this.red = red;
    this.green = green;
    this.blue = blue;
    this.color = color;
}

public Integer getRed(){
    return red;
}

public Integer getBlue(){
    return blue;
}

public Integer getGreen(){
    return green;
}

public DyeColor getDyeColor(){
    return color;
}

}

每當我嘗試使用以下代碼保存文件時:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            WritableRaster raster = (WritableRaster) image.getData();
            raster.setPixels(0, 0, width, height, pixels);
            image.setData(raster);
            try {
                ImageIO.write(image, "png", new File(name));
            } catch (IOException e) {
                e.printStackTrace();
            }

我遇到了問題,因為沒有正確的顏色,而是獲得了高度透明的顏色或根本沒有顏色。

感謝您的任何幫助,

盧卡斯

假設像素設置正確,請使用以下命令:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, pixels, 0, width);

要檢查像素看起來是否想要在某些組件中繪制圖像。 如果看起來不好,則說明您的顏色存在問題。 打印出basicColor.getRed()等,以確保它們是正確的。

我不確定代碼的上下文,但是x不應更改為寬度,y不應更改為高度嗎?

for(int x = left.getBlockX(); x < (left.getBlockX() + width); x++){
    for(int z = left.getBlockZ(); z < (left.getBlockZ() + height); z++){
        pixels[i] = getBasicColor(new Location(left.getWorld(), x, left.getBlockY(), z));
                i++;
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM