Java检查图像是否具有透明度

new*_*bie 7 java graphics transparency

是否有可能检查png图像是否在Java中具有透明性?如果png图像不包含透明度,我需要将所有png图像转换为jpg.是否有Java方法来检查这个?

Jon*_*oni 13

您可以检查图像的颜色模型是否包含Alpha通道:

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

if (img.getColorModel().hasAlpha()) {
    // img has alpha channel
} else {
    // no alpha channel
}
Run Code Online (Sandbox Code Playgroud)

请注意,此代码仅检测已使用Alpha通道保存的图像.具有Alpha通道的图像可能仍然是完全不透明的(即,对于所有像素,alpha = 1).