This discussion is archived
2 Replies Latest reply: Jul 23, 2010 11:55 AM by 843802 RSS

how to get Bit Depth property of an existing image

843802 Newbie
Currently Being Moderated
hi All,
I am trying to fetch Bit Depth property of an existing image . I write following code for that but it shows exception for set bit depth first but I want just fetch existing bit depth of an image.
I am little bit confused plz help me.

public class GetPixelColor 
  public static void main(String args[]) throws IOException{
   // File file= new File("C:\\glassfishv3\\glassfish\\domains\\domain1\\applications\\intranet\\intranetImages\\KASD-wert-uyty-Test\\KASD-wert-uyty-Test_01_L.jpg");
    File file=new File("D:\\Image\\bandw.jpg");
    BufferedImage image =;
    PNGEncodeParam Pne=PNGEncodeParam.getDefaultEncodeParam(image);
   System.out.println("Bir Dept is="+Pne.getBitDepth());
and after executing I got following error
Exception in thread "main" java.lang.IllegalStateException: Grayscale bit depth has not been set.
     at com.src.GetPixelColor.main(
Plz help me....

Edited by: Prashants on Jul 22, 2010 5:10 AM
  • 1. This Thread is now moved
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Note: This thread was originally posted in the [Java 2D|] forum, but moved to this forum for closer topic alignment.
  • 2. Re: how to get Bit Depth property of an existing image
    843802 Newbie
    Currently Being Moderated
    public static int getBitDepth(File f) throws IOException {
        ImageInputStream in = ImageIO.createImageInputStream(f);
        if(in == null) {
            throw new IOException("Can't create ImageInputStream!");
            Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
            ImageReader reader;
            if(!readers.hasNext()) {
                throw new IOException("Can't read image format!");
            }else {
                reader =;
            int bitDepth = reader.getImageTypes(0).next()
            return bitDepth;
        }finally {