Getting the Color Model of an Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

Getting the Color Model of an Image

Demo Code

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

public class Main {
  // This method returns the color model of an image
  public static ColorModel getColorModel(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
      BufferedImage bimage = (BufferedImage) image;
      return bimage.getColorModel();
    }/*  w w  w.  j  av  a2s  .  co  m*/

    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
    }
    ColorModel cm = pg.getColorModel();
    return cm;
  }
}

Related Tutorials