Java检查图像是否具有透明度
是否可以检查png图像在Java中是否具有透明度?如果png图像不包含透明度,我需要将所有png图像转换为jpg。Java中是否有方法检查此问题?您可以检查图像的颜色模型是否包含alpha通道:Java检查图像是否具有透明度,java,graphics,transparency,Java,Graphics,Transparency,是否可以检查png图像在Java中是否具有透明度?如果png图像不包含透明度,我需要将所有png图像转换为jpg。Java中是否有方法检查此问题?您可以检查图像的颜色模型是否包含alpha通道: BufferedImage img = ImageIO.read(/* from somewhere */); if (img.getColorModel().hasAlpha()) { // img has alpha channel } else { // no alpha cha
BufferedImage img = ImageIO.read(/* from somewhere */);
if (img.getColorModel().hasAlpha()) {
// img has alpha channel
} else {
// no alpha channel
}
请注意,此代码仅检测使用alpha通道保存的图像。具有alpha通道的图像可能仍然是完全不透明的(即,对于所有像素,alpha=1) @dacwe没有。完全不透明的颜色的alpha等于最大值,即1。完全透明的颜色的alpha=0。