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.
import com.sun.media.jai.codec.PNGEncodeParam;

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 = ImageIO.read(file);
    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.sun.media.jai.codec.PNGEncodeParam.getBitDepth(PNGEncodeParam.java:548)
     at com.src.GetPixelColor.main(GetPixelColor.java:19)
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|http://forums.sun.com/forum.jspa?forumID=20] 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!");
        }
    
        try{
            Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
    
            ImageReader reader;
            if(!readers.hasNext()) {
                throw new IOException("Can't read image format!");
            }else {
                reader = readers.next();
            }
            reader.setInput(in,true,true);
            int bitDepth = reader.getImageTypes(0).next()
                    .getColorModel().getPixelSize();
            reader.dispose();
            return bitDepth;
        }finally {
            in.close();
        }
    }