简体   繁体   English

ImageIO.read()是否保持PNG文件的透明度?

[英]Does ImageIO.read() keep transparency from PNG files?

I am currently programming a game using LWJGL and Java. 我目前正在使用LWJGL和Java编写游戏。 So far, everything is going well however textures do not seem to be keeping their transparency. 到目前为止,一切进展顺利,但是纹理似乎并没有保持其透明度。 At first, I thought I was setting something wrong in OpenGL. 起初,我以为我在OpenGL中设置了错误。 However, when I printed out all of the alphas from the BufferedImage being loaded using ImageIO.read(), all of the alphas were 255; 但是,当我从使用ImageIO.read()加载的BufferedImage中打印出所有Alpha时,所有Alpha均为255;因此, meaning there was no transparency even though the PNG file I was loading clearly had transparency in it (I checked the transparency in Paint.NET and made sure I was saving the image correctly and I was, and even verified it with someone else to make sure of it.) As a final check as well, I set the alpha of all of the black pixels manually in the images when loading them to make sure my transparency in OpenGL was working and it was. 这意味着即使我正在加载的PNG文件显然具有透明性,也没有透明性(我检查了Paint.NET中的透明性,并确保我正确保存了图像,而且我确实这样做了,甚至与其他人进行了验证以确保同样,作为最后检查,我在加载图像时在图像中手动设置了所有黑色像素的alpha值,以确保我在OpenGL中的透明度正常工作。

I was told by someone that ImageIO.read(), while supporting PNG files, does not support transparency in them and either defaults to an opaque black or white. 有人告诉我,ImageIO.read()虽然支持PNG文件,但不支持其中的透明度,并且默认为不透明的黑色或白色。 Is this true? 这是真的? If so, is there another way to load PNG files using Java? 如果是这样,还有另一种使用Java加载PNG文件的方法吗? (If you would like me to post some code, just let me know through the comments and I will try to edit the answer to only include the code needed.) (如果您希望我发布一些代码,请通过评论让我知道,我将尝试编辑答案以仅包括所需的代码。)

EDIT: Per request of MadProgrammer, here is a link to one of the images I am having problems with: https://i.imgur.com/4Vzriem.png The image in question is part of a flickering animation on the menu screen. 编辑:根据MadProgrammer的请求,这是指向我遇到问题的图像之一的链接: https ://i.imgur.com/4Vzriem.png有问题的图像是菜单屏幕上闪烁的动画的一部分。

EDIT #2: The problem was that when using the Color constructor, the fourth parameter (after the red, green, blue, and alpha) you must specify true in order for the transparency/alpha to be preserved. 编辑#2:问题是,当使用Color构造函数时,第四个参数(在红色,绿色,蓝色和alpha之后)必须指定true才能保留透明度/ alpha。 Otherwise, Java will for God knows what reason discard the given alpha and instead just use 1.0F. 否则,对于上帝而言,Java会知道是什么原因丢弃了给定的alpha,而是仅使用1.0F。

So, the short answer is, yes, ImageIO supports PNG transparency in most common PNG formats (I've personally not run it one which doesn't work, but occasionally people post questions stating that it doesn't for a image, but they never post the image for testing). 因此,简短的答案是,是的, ImageIO支持大多数常见的PNG格式的PNG透明性(我个人没有运行过不起作用的PNG透明性,但偶尔人们会提出疑问,指出它不适用于图像,但是他们请勿发布图片进行测试)。

So, I took you image, dumped into some test code... 所以,我为您拍摄了图像,并转入了一些测试代码...

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setBackground(Color.RED);
            add(new JLabel(new ImageIcon(ImageIO.read(new File("/Users/shanewhitehead/Downloads/4Vzriem.png")))));
        }

    }

}

Fired up an image editor and compared the results... 启动图像编辑器并比较结果...

比较方式

So, based on the image editor, the PNG seems to be rendered just fine in Java using ImageIO . 因此,基于图像编辑器,似乎可以使用ImageIO在Java中很好地渲染PNG。

There might be an issue in converting it to a texture in LWJGL however. 但是,在LWJGL中将其转换为纹理可能会出现问题。

For example: 例如:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM