Java checks if image has transparency

Is it possible to check if a png image has transparency in Java? I need to convert all png images to jpg if the png image does not contain transparency. Is there a way in Java to check this?

+6
source share
1 answer

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

BufferedImage img = ImageIO.read(/* from somewhere */); if (img.getColorModel().hasAlpha()) { // img has alpha channel } else { // no alpha channel } 

Please note: this code only detects images saved with the alpha channel. Images with an alpha channel can still be completely opaque (i.e. alpha = 1 for all pixels).

+13
source

Source: https://habr.com/ru/post/913604/


All Articles