2 Replies Latest reply on Jul 23, 2010 6:55 PM by 843802

    how to get Bit Depth property of an existing image

      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
          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
            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 = readers.next();
                    int bitDepth = reader.getImageTypes(0).next()
                    return bitDepth;
                }finally {