Mega Code Archive

 
Categories / Java Tutorial / 2D Graphics
 

Determining If an Image Has Transparent Pixels

import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.PixelGrabber; import javax.swing.ImageIcon; public class Main {   public static void main(String[] argv) throws Exception {     Image image = new ImageIcon("a.png").getImage();     if (image instanceof BufferedImage) {       BufferedImage bimage = (BufferedImage) image;       System.out.println(bimage.getColorModel().hasAlpha());     }     PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);     try {       pg.grabPixels();     } catch (InterruptedException e) {     }     ColorModel cm = pg.getColorModel();     System.out.println(cm.hasAlpha());   } }