简体   繁体   中英

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

I am currently programming a game using LWJGL and 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. However, when I printed out all of the alphas from the BufferedImage being loaded using ImageIO.read(), all of the alphas were 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.

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. Is this true? If so, is there another way to load PNG files using Java? (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.

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. Otherwise, Java will for God knows what reason discard the given alpha and instead just use 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).

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 .

There might be an issue in converting it to a texture in LWJGL however.

For example:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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