9

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?

newbie
  • 24,286
  • 80
  • 201
  • 301

1 Answers1

17

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).

Joni
  • 108,737
  • 14
  • 143
  • 193
  • @dacwe Nope. A fully opaque color has alpha equal to the maximum, which is 1. A fully transparent color would have alpha = 0. – Joni Apr 19 '12 at 08:30