Java编写png文件使一切透明

标签 java arrays image graphics bufferedimage

我当前正在尝试将 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;
}

这是基本颜色:

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() 等看看它们是否正确。

关于Java编写png文件使一切透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36490377/

相关文章:

javascript - 如何通过迭代对象数组来删除某些项目?

python - PIL png 调整为较小的分辨率使文件大小更大

java - 可以使用 ThreadLocal 来存储请求的 Locale 吗?

java - 如何从 .bat 进程获取输出以显示在 JTextPane 中?

arrays - 对指针和双指针的特定用户案例的误解

css - Magento 目录图像在悬停时发生变化

r - 更改 image.plot 中的图例刻度

Java Streams - 有效地对排序流上的项目进行分组

java - 如何从java中的base64编码字体字符串创建字体文件

ios - 搜索处于事件状态时 didSelectRowAt indexPath 函数不起作用