Determining If an Image Has Transparent Pixels - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Pixel

Introduction

This method returns true if the specified image has transparent pixels

Demo Code

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;

public class Main {
  public static boolean hasAlpha(Image image) {
    if (image instanceof BufferedImage) {
      BufferedImage bimage = (BufferedImage) image;
      return bimage.getColorModel().hasAlpha();
    }//from   www . j  a  va2  s .co m
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
    }
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
  }
}

Related Tutorials