Java check if an image has transparency

后端 未结 1 598
情话喂你
情话喂你 2020-12-19 03:30

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

相关标签:
1条回答
  • 2020-12-19 03:44

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

    0 讨论(0)
提交回复
热议问题