Java check if an image has transparency

graphics, java, transparency

Solution

You can check if the image's color model includes an alpha channel:

BufferedImage img = ImageIO.read(/* from somewhere */);

if (img.getColorModel().hasAlpha()) {
    // img has alpha channel
} else {
    // no alpha channel
}

Note that This code only detects images that have been saved with alpha channel. Images with an alpha channel may still be fully opaque (i.e. alpha = 1 for all pixels).

Problem

Is it possible to check if png image has transparency in Java? I need to convert all png images to jpg if png image doesn't contain transparency. Is there method in Java to check this?

Original source